|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package httpconv provides OpenTelemetry semantic convetions for the net/http |
| 16 | +// package from the standard library. |
| 17 | +package httpconv // import "go.opentelemetry.io/otel/semconv/v1.15.0/httpconv" |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "go.opentelemetry.io/otel/attribute" |
| 23 | + "go.opentelemetry.io/otel/codes" |
| 24 | + "go.opentelemetry.io/otel/semconv/internal/v2" |
| 25 | + semconv "go.opentelemetry.io/otel/semconv/v1.15.0" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + nc = &internal.NetConv{ |
| 30 | + NetHostNameKey: semconv.NetHostNameKey, |
| 31 | + NetHostPortKey: semconv.NetHostPortKey, |
| 32 | + NetPeerNameKey: semconv.NetPeerNameKey, |
| 33 | + NetPeerPortKey: semconv.NetPeerPortKey, |
| 34 | + NetSockPeerAddrKey: semconv.NetSockPeerAddrKey, |
| 35 | + NetSockPeerPortKey: semconv.NetSockPeerPortKey, |
| 36 | + NetTransportOther: semconv.NetTransportOther, |
| 37 | + NetTransportTCP: semconv.NetTransportTCP, |
| 38 | + NetTransportUDP: semconv.NetTransportUDP, |
| 39 | + NetTransportInProc: semconv.NetTransportInProc, |
| 40 | + } |
| 41 | + |
| 42 | + hc = &internal.HTTPConv{ |
| 43 | + NetConv: nc, |
| 44 | + |
| 45 | + EnduserIDKey: semconv.EnduserIDKey, |
| 46 | + HTTPClientIPKey: semconv.HTTPClientIPKey, |
| 47 | + HTTPFlavorKey: semconv.HTTPFlavorKey, |
| 48 | + HTTPMethodKey: semconv.HTTPMethodKey, |
| 49 | + HTTPRequestContentLengthKey: semconv.HTTPRequestContentLengthKey, |
| 50 | + HTTPResponseContentLengthKey: semconv.HTTPResponseContentLengthKey, |
| 51 | + HTTPRouteKey: semconv.HTTPRouteKey, |
| 52 | + HTTPSchemeHTTP: semconv.HTTPSchemeHTTP, |
| 53 | + HTTPSchemeHTTPS: semconv.HTTPSchemeHTTPS, |
| 54 | + HTTPStatusCodeKey: semconv.HTTPStatusCodeKey, |
| 55 | + HTTPTargetKey: semconv.HTTPTargetKey, |
| 56 | + HTTPURLKey: semconv.HTTPURLKey, |
| 57 | + HTTPUserAgentKey: semconv.HTTPUserAgentKey, |
| 58 | + } |
| 59 | +) |
| 60 | + |
| 61 | +// ClientResponse returns attributes for an HTTP response received by a client |
| 62 | +// from a server. It will return the following attributes if the related values |
| 63 | +// are defined in resp: "http.status.code", "http.response_content_length". |
| 64 | +// |
| 65 | +// This does not add all OpenTelemetry required attributes for an HTTP event, |
| 66 | +// it assumes ClientRequest was used to create the span with a complete set of |
| 67 | +// attributes. If a complete set of attributes can be generated using the |
| 68 | +// request contained in resp. For example: |
| 69 | +// |
| 70 | +// append(ClientResponse(resp), ClientRequest(resp.Request)...) |
| 71 | +func ClientResponse(resp http.Response) []attribute.KeyValue { |
| 72 | + return hc.ClientResponse(resp) |
| 73 | +} |
| 74 | + |
| 75 | +// ClientRequest returns attributes for an HTTP request made by a client. The |
| 76 | +// following attributes are always returned: "http.url", "http.flavor", |
| 77 | +// "http.method", "net.peer.name". The following attributes are returned if the |
| 78 | +// related values are defined in req: "net.peer.port", "http.user_agent", |
| 79 | +// "http.request_content_length", "enduser.id". |
| 80 | +func ClientRequest(req *http.Request) []attribute.KeyValue { |
| 81 | + return hc.ClientRequest(req) |
| 82 | +} |
| 83 | + |
| 84 | +// ClientStatus returns a span status code and message for an HTTP status code |
| 85 | +// value received by a client. |
| 86 | +func ClientStatus(code int) (codes.Code, string) { |
| 87 | + return hc.ClientStatus(code) |
| 88 | +} |
| 89 | + |
| 90 | +// ServerRequest returns attributes for an HTTP request received by a server. |
| 91 | +// The following attributes are always returned: "http.method", "http.scheme", |
| 92 | +// "http.flavor", "http.target", "net.host.name". The following attributes are |
| 93 | +// returned if they related values are defined in req: "net.host.port", |
| 94 | +// "net.sock.peer.addr", "net.sock.peer.port", "http.user_agent", "enduser.id", |
| 95 | +// "http.client_ip". |
| 96 | +func ServerRequest(req *http.Request) []attribute.KeyValue { |
| 97 | + return hc.ServerRequest(req) |
| 98 | +} |
| 99 | + |
| 100 | +// ServerStatus returns a span status code and message for an HTTP status code |
| 101 | +// value returned by a server. Status codes in the 400-499 range are not |
| 102 | +// returned as errors. |
| 103 | +func ServerStatus(code int) (codes.Code, string) { |
| 104 | + return hc.ServerStatus(code) |
| 105 | +} |
| 106 | + |
| 107 | +// RequestHeader returns the contents of h as attributes. |
| 108 | +// |
| 109 | +// Instrumentation should require an explicit configuration of which headers to |
| 110 | +// captured and then prune what they pass here. Including all headers can be a |
| 111 | +// security risk - explicit configuration helps avoid leaking sensitive |
| 112 | +// information. |
| 113 | +// |
| 114 | +// The User-Agent header is already captured in the http.user_agent attribute |
| 115 | +// from ClientRequest and ServerRequest. Instrumentation may provide an option |
| 116 | +// to capture that header here even though it is not recommended. Otherwise, |
| 117 | +// instrumentation should filter that out of what is passed. |
| 118 | +func RequestHeader(h http.Header) []attribute.KeyValue { |
| 119 | + return hc.RequestHeader(h) |
| 120 | +} |
| 121 | + |
| 122 | +// ResponseHeader returns the contents of h as attributes. |
| 123 | +// |
| 124 | +// Instrumentation should require an explicit configuration of which headers to |
| 125 | +// captured and then prune what they pass here. Including all headers can be a |
| 126 | +// security risk - explicit configuration helps avoid leaking sensitive |
| 127 | +// information. |
| 128 | +// |
| 129 | +// The User-Agent header is already captured in the http.user_agent attribute |
| 130 | +// from ClientRequest and ServerRequest. Instrumentation may provide an option |
| 131 | +// to capture that header here even though it is not recommended. Otherwise, |
| 132 | +// instrumentation should filter that out of what is passed. |
| 133 | +func ResponseHeader(h http.Header) []attribute.KeyValue { |
| 134 | + return hc.ResponseHeader(h) |
| 135 | +} |
0 commit comments