http.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. package semconv // import "go.opentelemetry.io/otel/semconv/v1.4.0"
  15. import (
  16. "net/http"
  17. "go.opentelemetry.io/otel/attribute"
  18. "go.opentelemetry.io/otel/codes"
  19. "go.opentelemetry.io/otel/semconv/internal"
  20. "go.opentelemetry.io/otel/trace"
  21. )
  22. // HTTP scheme attributes.
  23. var (
  24. HTTPSchemeHTTP = HTTPSchemeKey.String("http")
  25. HTTPSchemeHTTPS = HTTPSchemeKey.String("https")
  26. )
  27. var sc = &internal.SemanticConventions{
  28. EnduserIDKey: EnduserIDKey,
  29. HTTPClientIPKey: HTTPClientIPKey,
  30. HTTPFlavorKey: HTTPFlavorKey,
  31. HTTPHostKey: HTTPHostKey,
  32. HTTPMethodKey: HTTPMethodKey,
  33. HTTPRequestContentLengthKey: HTTPRequestContentLengthKey,
  34. HTTPRouteKey: HTTPRouteKey,
  35. HTTPSchemeHTTP: HTTPSchemeHTTP,
  36. HTTPSchemeHTTPS: HTTPSchemeHTTPS,
  37. HTTPServerNameKey: HTTPServerNameKey,
  38. HTTPStatusCodeKey: HTTPStatusCodeKey,
  39. HTTPTargetKey: HTTPTargetKey,
  40. HTTPURLKey: HTTPURLKey,
  41. HTTPUserAgentKey: HTTPUserAgentKey,
  42. NetHostIPKey: NetHostIPKey,
  43. NetHostNameKey: NetHostNameKey,
  44. NetHostPortKey: NetHostPortKey,
  45. NetPeerIPKey: NetPeerIPKey,
  46. NetPeerNameKey: NetPeerNameKey,
  47. NetPeerPortKey: NetPeerPortKey,
  48. NetTransportIP: NetTransportIP,
  49. NetTransportOther: NetTransportOther,
  50. NetTransportTCP: NetTransportTCP,
  51. NetTransportUDP: NetTransportUDP,
  52. NetTransportUnix: NetTransportUnix,
  53. }
  54. // NetAttributesFromHTTPRequest generates attributes of the net
  55. // namespace as specified by the OpenTelemetry specification for a
  56. // span. The network parameter is a string that net.Dial function
  57. // from standard library can understand.
  58. func NetAttributesFromHTTPRequest(network string, request *http.Request) []attribute.KeyValue {
  59. return sc.NetAttributesFromHTTPRequest(network, request)
  60. }
  61. // EndUserAttributesFromHTTPRequest generates attributes of the
  62. // enduser namespace as specified by the OpenTelemetry specification
  63. // for a span.
  64. func EndUserAttributesFromHTTPRequest(request *http.Request) []attribute.KeyValue {
  65. return sc.EndUserAttributesFromHTTPRequest(request)
  66. }
  67. // HTTPClientAttributesFromHTTPRequest generates attributes of the
  68. // http namespace as specified by the OpenTelemetry specification for
  69. // a span on the client side.
  70. func HTTPClientAttributesFromHTTPRequest(request *http.Request) []attribute.KeyValue {
  71. return sc.HTTPClientAttributesFromHTTPRequest(request)
  72. }
  73. // HTTPServerMetricAttributesFromHTTPRequest generates low-cardinality attributes
  74. // to be used with server-side HTTP metrics.
  75. func HTTPServerMetricAttributesFromHTTPRequest(serverName string, request *http.Request) []attribute.KeyValue {
  76. return sc.HTTPServerMetricAttributesFromHTTPRequest(serverName, request)
  77. }
  78. // HTTPServerAttributesFromHTTPRequest generates attributes of the
  79. // http namespace as specified by the OpenTelemetry specification for
  80. // a span on the server side. Currently, only basic authentication is
  81. // supported.
  82. func HTTPServerAttributesFromHTTPRequest(serverName, route string, request *http.Request) []attribute.KeyValue {
  83. return sc.HTTPServerAttributesFromHTTPRequest(serverName, route, request)
  84. }
  85. // HTTPAttributesFromHTTPStatusCode generates attributes of the http
  86. // namespace as specified by the OpenTelemetry specification for a
  87. // span.
  88. func HTTPAttributesFromHTTPStatusCode(code int) []attribute.KeyValue {
  89. return sc.HTTPAttributesFromHTTPStatusCode(code)
  90. }
  91. // SpanStatusFromHTTPStatusCode generates a status code and a message
  92. // as specified by the OpenTelemetry specification for a span.
  93. func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
  94. return internal.SpanStatusFromHTTPStatusCode(code)
  95. }
  96. // SpanStatusFromHTTPStatusCodeAndSpanKind generates a status code and a message
  97. // as specified by the OpenTelemetry specification for a span.
  98. // Exclude 4xx for SERVER to set the appropriate status.
  99. func SpanStatusFromHTTPStatusCodeAndSpanKind(code int, spanKind trace.SpanKind) (codes.Code, string) {
  100. return internal.SpanStatusFromHTTPStatusCodeAndSpanKind(code, spanKind)
  101. }