2020-05-27 03:06:13 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package semconv // import "go.opentelemetry.io/otel/semconv"
|
2020-05-27 03:06:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-10 18:17:09 +02:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2020-05-27 03:06:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NetAttributesFromHTTPRequest generates attributes of the net
|
|
|
|
// namespace as specified by the OpenTelemetry specification for a
|
|
|
|
// span. The network parameter is a string that net.Dial function
|
|
|
|
// from standard library can understand.
|
2020-08-18 05:25:03 +02:00
|
|
|
func NetAttributesFromHTTPRequest(network string, request *http.Request) []label.KeyValue {
|
|
|
|
attrs := []label.KeyValue{}
|
2020-05-27 03:06:13 +02:00
|
|
|
|
|
|
|
switch network {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
attrs = append(attrs, NetTransportTCP)
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
attrs = append(attrs, NetTransportUDP)
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
attrs = append(attrs, NetTransportIP)
|
|
|
|
case "unix", "unixgram", "unixpacket":
|
|
|
|
attrs = append(attrs, NetTransportUnix)
|
|
|
|
default:
|
|
|
|
attrs = append(attrs, NetTransportOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerName, peerIP, peerPort := "", "", 0
|
|
|
|
{
|
|
|
|
hostPart := request.RemoteAddr
|
|
|
|
portPart := ""
|
|
|
|
if idx := strings.LastIndex(hostPart, ":"); idx >= 0 {
|
|
|
|
hostPart = request.RemoteAddr[:idx]
|
|
|
|
portPart = request.RemoteAddr[idx+1:]
|
|
|
|
}
|
|
|
|
if hostPart != "" {
|
|
|
|
if ip := net.ParseIP(hostPart); ip != nil {
|
|
|
|
peerIP = ip.String()
|
|
|
|
} else {
|
|
|
|
peerName = hostPart
|
|
|
|
}
|
|
|
|
|
|
|
|
if portPart != "" {
|
|
|
|
numPort, err := strconv.ParseUint(portPart, 10, 16)
|
|
|
|
if err == nil {
|
|
|
|
peerPort = (int)(numPort)
|
|
|
|
} else {
|
|
|
|
peerName, peerIP = "", ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if peerName != "" {
|
|
|
|
attrs = append(attrs, NetPeerNameKey.String(peerName))
|
|
|
|
}
|
|
|
|
if peerIP != "" {
|
|
|
|
attrs = append(attrs, NetPeerIPKey.String(peerIP))
|
|
|
|
}
|
|
|
|
if peerPort != 0 {
|
|
|
|
attrs = append(attrs, NetPeerPortKey.Int(peerPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
hostIP, hostName, hostPort := "", "", 0
|
|
|
|
for _, someHost := range []string{request.Host, request.Header.Get("Host"), request.URL.Host} {
|
|
|
|
hostPart := ""
|
|
|
|
if idx := strings.LastIndex(someHost, ":"); idx >= 0 {
|
|
|
|
strPort := someHost[idx+1:]
|
|
|
|
numPort, err := strconv.ParseUint(strPort, 10, 16)
|
|
|
|
if err == nil {
|
|
|
|
hostPort = (int)(numPort)
|
|
|
|
}
|
|
|
|
hostPart = someHost[:idx]
|
|
|
|
} else {
|
|
|
|
hostPart = someHost
|
|
|
|
}
|
|
|
|
if hostPart != "" {
|
|
|
|
ip := net.ParseIP(hostPart)
|
|
|
|
if ip != nil {
|
|
|
|
hostIP = ip.String()
|
|
|
|
} else {
|
|
|
|
hostName = hostPart
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
hostPort = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hostIP != "" {
|
|
|
|
attrs = append(attrs, NetHostIPKey.String(hostIP))
|
|
|
|
}
|
|
|
|
if hostName != "" {
|
|
|
|
attrs = append(attrs, NetHostNameKey.String(hostName))
|
|
|
|
}
|
|
|
|
if hostPort != 0 {
|
|
|
|
attrs = append(attrs, NetHostPortKey.Int(hostPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndUserAttributesFromHTTPRequest generates attributes of the
|
|
|
|
// enduser namespace as specified by the OpenTelemetry specification
|
|
|
|
// for a span.
|
2020-08-18 05:25:03 +02:00
|
|
|
func EndUserAttributesFromHTTPRequest(request *http.Request) []label.KeyValue {
|
2020-05-27 03:06:13 +02:00
|
|
|
if username, _, ok := request.BasicAuth(); ok {
|
2020-08-18 05:25:03 +02:00
|
|
|
return []label.KeyValue{EnduserIDKey.String(username)}
|
2020-05-27 03:06:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 03:53:16 +02:00
|
|
|
// HTTPClientAttributesFromHTTPRequest generates attributes of the
|
2020-05-27 03:06:13 +02:00
|
|
|
// http namespace as specified by the OpenTelemetry specification for
|
2020-05-27 03:53:16 +02:00
|
|
|
// a span on the client side.
|
2020-08-18 05:25:03 +02:00
|
|
|
func HTTPClientAttributesFromHTTPRequest(request *http.Request) []label.KeyValue {
|
|
|
|
attrs := []label.KeyValue{}
|
2020-05-27 03:06:13 +02:00
|
|
|
|
2020-05-27 03:53:16 +02:00
|
|
|
if request.Method != "" {
|
|
|
|
attrs = append(attrs, HTTPMethodKey.String(request.Method))
|
|
|
|
} else {
|
|
|
|
attrs = append(attrs, HTTPMethodKey.String(http.MethodGet))
|
2020-05-27 03:06:13 +02:00
|
|
|
}
|
2020-05-27 03:53:16 +02:00
|
|
|
|
|
|
|
attrs = append(attrs, HTTPUrlKey.String(request.URL.String()))
|
|
|
|
|
|
|
|
return append(attrs, httpCommonAttributesFromHTTPRequest(request)...)
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
func httpCommonAttributesFromHTTPRequest(request *http.Request) []label.KeyValue {
|
|
|
|
attrs := []label.KeyValue{}
|
2020-06-26 21:19:57 +02:00
|
|
|
if ua := request.UserAgent(); ua != "" {
|
|
|
|
attrs = append(attrs, HTTPUserAgentKey.String(ua))
|
|
|
|
}
|
2020-08-05 22:24:28 +02:00
|
|
|
if request.ContentLength > 0 {
|
|
|
|
attrs = append(attrs, HTTPRequestContentLengthKey.Int64(request.ContentLength))
|
|
|
|
}
|
2020-06-26 21:19:57 +02:00
|
|
|
|
|
|
|
return append(attrs, httpBasicAttributesFromHTTPRequest(request)...)
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
func httpBasicAttributesFromHTTPRequest(request *http.Request) []label.KeyValue {
|
2020-08-05 22:24:28 +02:00
|
|
|
// as these attributes are used by HTTPServerMetricAttributesFromHTTPRequest, they should be low-cardinality
|
2020-08-18 05:25:03 +02:00
|
|
|
attrs := []label.KeyValue{}
|
2020-05-27 03:53:16 +02:00
|
|
|
|
2020-05-27 03:06:13 +02:00
|
|
|
if request.TLS != nil {
|
|
|
|
attrs = append(attrs, HTTPSchemeHTTPS)
|
|
|
|
} else {
|
|
|
|
attrs = append(attrs, HTTPSchemeHTTP)
|
|
|
|
}
|
2020-05-27 03:53:16 +02:00
|
|
|
|
2020-05-27 03:06:13 +02:00
|
|
|
if request.Host != "" {
|
|
|
|
attrs = append(attrs, HTTPHostKey.String(request.Host))
|
|
|
|
}
|
2020-05-27 03:53:16 +02:00
|
|
|
|
2020-05-27 03:06:13 +02:00
|
|
|
flavor := ""
|
|
|
|
if request.ProtoMajor == 1 {
|
|
|
|
flavor = fmt.Sprintf("1.%d", request.ProtoMinor)
|
|
|
|
} else if request.ProtoMajor == 2 {
|
|
|
|
flavor = "2"
|
|
|
|
}
|
|
|
|
if flavor != "" {
|
|
|
|
attrs = append(attrs, HTTPFlavorKey.String(flavor))
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:19:57 +02:00
|
|
|
// HTTPServerMetricAttributesFromHTTPRequest generates low-cardinality attributes
|
|
|
|
// to be used with server-side HTTP metrics.
|
2020-08-18 05:25:03 +02:00
|
|
|
func HTTPServerMetricAttributesFromHTTPRequest(serverName string, request *http.Request) []label.KeyValue {
|
|
|
|
attrs := []label.KeyValue{}
|
2020-06-26 21:19:57 +02:00
|
|
|
if serverName != "" {
|
|
|
|
attrs = append(attrs, HTTPServerNameKey.String(serverName))
|
|
|
|
}
|
|
|
|
return append(attrs, httpBasicAttributesFromHTTPRequest(request)...)
|
|
|
|
}
|
|
|
|
|
2020-05-27 03:53:16 +02:00
|
|
|
// HTTPServerAttributesFromHTTPRequest generates attributes of the
|
|
|
|
// http namespace as specified by the OpenTelemetry specification for
|
|
|
|
// a span on the server side. Currently, only basic authentication is
|
|
|
|
// supported.
|
2020-08-18 05:25:03 +02:00
|
|
|
func HTTPServerAttributesFromHTTPRequest(serverName, route string, request *http.Request) []label.KeyValue {
|
|
|
|
attrs := []label.KeyValue{
|
2020-05-27 03:53:16 +02:00
|
|
|
HTTPMethodKey.String(request.Method),
|
|
|
|
HTTPTargetKey.String(request.RequestURI),
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverName != "" {
|
|
|
|
attrs = append(attrs, HTTPServerNameKey.String(serverName))
|
|
|
|
}
|
|
|
|
if route != "" {
|
|
|
|
attrs = append(attrs, HTTPRouteKey.String(route))
|
|
|
|
}
|
|
|
|
if values, ok := request.Header["X-Forwarded-For"]; ok && len(values) > 0 {
|
|
|
|
attrs = append(attrs, HTTPClientIPKey.String(values[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(attrs, httpCommonAttributesFromHTTPRequest(request)...)
|
|
|
|
}
|
|
|
|
|
2020-05-27 03:06:13 +02:00
|
|
|
// HTTPAttributesFromHTTPStatusCode generates attributes of the http
|
|
|
|
// namespace as specified by the OpenTelemetry specification for a
|
|
|
|
// span.
|
2020-08-18 05:25:03 +02:00
|
|
|
func HTTPAttributesFromHTTPStatusCode(code int) []label.KeyValue {
|
|
|
|
attrs := []label.KeyValue{
|
2020-05-27 03:06:13 +02:00
|
|
|
HTTPStatusCodeKey.Int(code),
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
type codeRange struct {
|
|
|
|
fromInclusive int
|
|
|
|
toInclusive int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r codeRange) contains(code int) bool {
|
|
|
|
return r.fromInclusive <= code && code <= r.toInclusive
|
|
|
|
}
|
|
|
|
|
|
|
|
var validRangesPerCategory = map[int][]codeRange{
|
|
|
|
1: {
|
|
|
|
{http.StatusContinue, http.StatusEarlyHints},
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
{http.StatusOK, http.StatusAlreadyReported},
|
|
|
|
{http.StatusIMUsed, http.StatusIMUsed},
|
|
|
|
},
|
|
|
|
3: {
|
|
|
|
{http.StatusMultipleChoices, http.StatusUseProxy},
|
|
|
|
{http.StatusTemporaryRedirect, http.StatusPermanentRedirect},
|
|
|
|
},
|
|
|
|
4: {
|
|
|
|
{http.StatusBadRequest, http.StatusTeapot}, // yes, teapot is so useful…
|
|
|
|
{http.StatusMisdirectedRequest, http.StatusUpgradeRequired},
|
|
|
|
{http.StatusPreconditionRequired, http.StatusTooManyRequests},
|
|
|
|
{http.StatusRequestHeaderFieldsTooLarge, http.StatusRequestHeaderFieldsTooLarge},
|
|
|
|
{http.StatusUnavailableForLegalReasons, http.StatusUnavailableForLegalReasons},
|
|
|
|
},
|
|
|
|
5: {
|
|
|
|
{http.StatusInternalServerError, http.StatusLoopDetected},
|
|
|
|
{http.StatusNotExtended, http.StatusNetworkAuthenticationRequired},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpanStatusFromHTTPStatusCode generates a status code and a message
|
|
|
|
// as specified by the OpenTelemetry specification for a span.
|
|
|
|
func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
|
2020-10-05 20:36:03 +02:00
|
|
|
spanCode, valid := func() (codes.Code, bool) {
|
2020-05-27 03:06:13 +02:00
|
|
|
category := code / 100
|
|
|
|
ranges, ok := validRangesPerCategory[category]
|
|
|
|
if !ok {
|
2020-10-05 20:36:03 +02:00
|
|
|
return codes.Error, false
|
2020-05-27 03:06:13 +02:00
|
|
|
}
|
|
|
|
ok = false
|
|
|
|
for _, crange := range ranges {
|
|
|
|
ok = crange.contains(code)
|
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
2020-10-05 20:36:03 +02:00
|
|
|
return codes.Error, false
|
2020-05-27 03:06:13 +02:00
|
|
|
}
|
|
|
|
if category > 0 && category < 4 {
|
2020-10-05 20:36:03 +02:00
|
|
|
return codes.Unset, true
|
2020-05-27 03:06:13 +02:00
|
|
|
}
|
2020-10-05 20:36:03 +02:00
|
|
|
return codes.Error, true
|
2020-05-27 03:06:13 +02:00
|
|
|
}()
|
2020-10-05 20:36:03 +02:00
|
|
|
if !valid {
|
2020-05-27 03:06:13 +02:00
|
|
|
return spanCode, fmt.Sprintf("Invalid HTTP status code %d", code)
|
|
|
|
}
|
|
|
|
return spanCode, fmt.Sprintf("HTTP status code: %d", code)
|
|
|
|
}
|