| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/semconv/server.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
// Package semconv provides OpenTelemetry semantic convention types and |
| 8 |
// functionality. |
| 9 |
package semconv // import "go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux/internal/semconv" |
| 10 |
|
| 11 |
import ( |
| 12 |
"context" |
| 13 |
"fmt" |
| 14 |
"net/http" |
| 15 |
"slices" |
| 16 |
"strings" |
| 17 |
"sync" |
| 18 |
"time" |
| 19 |
|
| 20 |
"go.opentelemetry.io/otel/attribute" |
| 21 |
"go.opentelemetry.io/otel/codes" |
| 22 |
"go.opentelemetry.io/otel/metric" |
| 23 |
"go.opentelemetry.io/otel/semconv/v1.40.0" |
| 24 |
"go.opentelemetry.io/otel/semconv/v1.40.0/httpconv" |
| 25 |
) |
| 26 |
|
| 27 |
type RequestTraceAttrsOpts struct { |
| 28 |
// If set, this is used as value for the "http.client_ip" attribute. |
| 29 |
HTTPClientIP string |
| 30 |
} |
| 31 |
|
| 32 |
type ResponseTelemetry struct { |
| 33 |
StatusCode int |
| 34 |
ReadBytes int64 |
| 35 |
ReadError error |
| 36 |
WriteBytes int64 |
| 37 |
WriteError error |
| 38 |
} |
| 39 |
|
| 40 |
type HTTPServer struct { |
| 41 |
requestBodySizeHistogram httpconv.ServerRequestBodySize |
| 42 |
responseBodySizeHistogram httpconv.ServerResponseBodySize |
| 43 |
requestDurationHistogram httpconv.ServerRequestDuration |
| 44 |
} |
| 45 |
|
| 46 |
func NewHTTPServer(meter metric.Meter) HTTPServer { |
| 47 |
server := HTTPServer{} |
| 48 |
|
| 49 |
var err error |
| 50 |
server.requestBodySizeHistogram, err = httpconv.NewServerRequestBodySize(meter) |
| 51 |
handleErr(err) |
| 52 |
|
| 53 |
server.responseBodySizeHistogram, err = httpconv.NewServerResponseBodySize(meter) |
| 54 |
handleErr(err) |
| 55 |
|
| 56 |
server.requestDurationHistogram, err = httpconv.NewServerRequestDuration( |
| 57 |
meter, |
| 58 |
metric.WithExplicitBucketBoundaries( |
| 59 |
0.005, 0.01, 0.025, 0.05, 0.075, 0.1, |
| 60 |
0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, |
| 61 |
), |
| 62 |
) |
| 63 |
handleErr(err) |
| 64 |
return server |
| 65 |
} |
| 66 |
|
| 67 |
// Status returns a span status code and message for an HTTP status code |
| 68 |
// value returned by a server. Status codes in the 400-499 range are not |
| 69 |
// returned as errors. |
| 70 |
func (n HTTPServer) Status(code int) (codes.Code, string) { |
| 71 |
if code < 100 || code >= 600 { |
| 72 |
return codes.Error, fmt.Sprintf("Invalid HTTP status code %d", code) |
| 73 |
} |
| 74 |
if code >= 500 { |
| 75 |
return codes.Error, "" |
| 76 |
} |
| 77 |
return codes.Unset, "" |
| 78 |
} |
| 79 |
|
| 80 |
// RequestTraceAttrs returns trace attributes for an HTTP request received by a |
| 81 |
// server. |
| 82 |
// |
| 83 |
// The server must be the primary server name if it is known. For example this |
| 84 |
// would be the ServerName directive |
| 85 |
// (https://httpd.apache.org/docs/2.4/mod/core.html#servername) for an Apache |
| 86 |
// server, and the server_name directive |
| 87 |
// (http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name) for an |
| 88 |
// nginx server. More generically, the primary server name would be the host |
| 89 |
// header value that matches the default virtual host of an HTTP server. It |
| 90 |
// should include the host identifier and if a port is used to route to the |
| 91 |
// server that port identifier should be included as an appropriate port |
| 92 |
// suffix. |
| 93 |
// |
| 94 |
// If the primary server name is not known, server should be an empty string. |
| 95 |
// The req Host will be used to determine the server instead. |
| 96 |
func (n HTTPServer) RequestTraceAttrs(server string, req *http.Request, opts RequestTraceAttrsOpts) []attribute.KeyValue { |
| 97 |
count := 3 // ServerAddress, Method, Scheme |
| 98 |
|
| 99 |
var host string |
| 100 |
var p int |
| 101 |
if server == "" { |
| 102 |
host, p = SplitHostPort(req.Host) |
| 103 |
} else { |
| 104 |
// Prioritize the primary server name. |
| 105 |
host, p = SplitHostPort(server) |
| 106 |
if p < 0 { |
| 107 |
_, p = SplitHostPort(req.Host) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
hostPort := requiredHTTPPort(req.TLS != nil, p) |
| 112 |
if hostPort > 0 { |
| 113 |
count++ |
| 114 |
} |
| 115 |
|
| 116 |
method, methodOriginal := n.method(req.Method) |
| 117 |
if methodOriginal != (attribute.KeyValue{}) { |
| 118 |
count++ |
| 119 |
} |
| 120 |
|
| 121 |
scheme := n.scheme(req.TLS != nil) |
| 122 |
|
| 123 |
peer, peerPort := SplitHostPort(req.RemoteAddr) |
| 124 |
if peer != "" { |
| 125 |
// The Go HTTP server sets RemoteAddr to "IP:port", this will not be a |
| 126 |
// file-path that would be interpreted with a sock family. |
| 127 |
count++ |
| 128 |
if peerPort > 0 { |
| 129 |
count++ |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
useragent := req.UserAgent() |
| 134 |
if useragent != "" { |
| 135 |
count++ |
| 136 |
} |
| 137 |
|
| 138 |
// For client IP, use, in order: |
| 139 |
// 1. The value passed in the options |
| 140 |
// 2. The value in the X-Forwarded-For header |
| 141 |
// 3. The peer address |
| 142 |
clientIP := opts.HTTPClientIP |
| 143 |
if clientIP == "" { |
| 144 |
clientIP = serverClientIP(req.Header.Get("X-Forwarded-For")) |
| 145 |
if clientIP == "" { |
| 146 |
clientIP = peer |
| 147 |
} |
| 148 |
} |
| 149 |
if clientIP != "" { |
| 150 |
count++ |
| 151 |
} |
| 152 |
|
| 153 |
if req.URL != nil && req.URL.Path != "" { |
| 154 |
count++ |
| 155 |
} |
| 156 |
|
| 157 |
protoName, protoVersion := netProtocol(req.Proto) |
| 158 |
if protoName != "" && protoName != "http" { |
| 159 |
count++ |
| 160 |
} |
| 161 |
if protoVersion != "" { |
| 162 |
count++ |
| 163 |
} |
| 164 |
|
| 165 |
route := httpRoute(req.Pattern) |
| 166 |
if route != "" { |
| 167 |
count++ |
| 168 |
} |
| 169 |
|
| 170 |
attrs := make([]attribute.KeyValue, 0, count) |
| 171 |
attrs = append(attrs, |
| 172 |
semconv.ServerAddress(host), |
| 173 |
method, |
| 174 |
scheme, |
| 175 |
) |
| 176 |
|
| 177 |
if hostPort > 0 { |
| 178 |
attrs = append(attrs, semconv.ServerPort(hostPort)) |
| 179 |
} |
| 180 |
if methodOriginal != (attribute.KeyValue{}) { |
| 181 |
attrs = append(attrs, methodOriginal) |
| 182 |
} |
| 183 |
|
| 184 |
if peer, peerPort := SplitHostPort(req.RemoteAddr); peer != "" { |
| 185 |
// The Go HTTP server sets RemoteAddr to "IP:port", this will not be a |
| 186 |
// file-path that would be interpreted with a sock family. |
| 187 |
attrs = append(attrs, semconv.NetworkPeerAddress(peer)) |
| 188 |
if peerPort > 0 { |
| 189 |
attrs = append(attrs, semconv.NetworkPeerPort(peerPort)) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if useragent != "" { |
| 194 |
attrs = append(attrs, semconv.UserAgentOriginal(useragent)) |
| 195 |
} |
| 196 |
|
| 197 |
if clientIP != "" { |
| 198 |
attrs = append(attrs, semconv.ClientAddress(clientIP)) |
| 199 |
} |
| 200 |
|
| 201 |
if req.URL != nil && req.URL.Path != "" { |
| 202 |
attrs = append(attrs, semconv.URLPath(req.URL.Path)) |
| 203 |
} |
| 204 |
|
| 205 |
if protoName != "" && protoName != "http" { |
| 206 |
attrs = append(attrs, semconv.NetworkProtocolName(protoName)) |
| 207 |
} |
| 208 |
if protoVersion != "" { |
| 209 |
attrs = append(attrs, semconv.NetworkProtocolVersion(protoVersion)) |
| 210 |
} |
| 211 |
|
| 212 |
if route != "" { |
| 213 |
attrs = append(attrs, n.Route(route)) |
| 214 |
} |
| 215 |
|
| 216 |
return attrs |
| 217 |
} |
| 218 |
|
| 219 |
func (s HTTPServer) NetworkTransportAttr(network string) []attribute.KeyValue { |
| 220 |
attr := semconv.NetworkTransportPipe |
| 221 |
switch network { |
| 222 |
case "tcp", "tcp4", "tcp6": |
| 223 |
attr = semconv.NetworkTransportTCP |
| 224 |
case "udp", "udp4", "udp6": |
| 225 |
attr = semconv.NetworkTransportUDP |
| 226 |
case "unix", "unixgram", "unixpacket": |
| 227 |
attr = semconv.NetworkTransportUnix |
| 228 |
} |
| 229 |
|
| 230 |
return []attribute.KeyValue{attr} |
| 231 |
} |
| 232 |
|
| 233 |
type ServerMetricData struct { |
| 234 |
ServerName string |
| 235 |
ResponseSize int64 |
| 236 |
|
| 237 |
MetricData |
| 238 |
MetricAttributes |
| 239 |
} |
| 240 |
|
| 241 |
type MetricAttributes struct { |
| 242 |
Req *http.Request |
| 243 |
StatusCode int |
| 244 |
Route string |
| 245 |
AdditionalAttributes []attribute.KeyValue |
| 246 |
} |
| 247 |
|
| 248 |
type MetricData struct { |
| 249 |
RequestSize int64 |
| 250 |
RequestDuration time.Duration |
| 251 |
} |
| 252 |
|
| 253 |
var ( |
| 254 |
metricRecordOptionPool = &sync.Pool{ |
| 255 |
New: func() any { |
| 256 |
return &[]metric.RecordOption{} |
| 257 |
}, |
| 258 |
} |
| 259 |
) |
| 260 |
|
| 261 |
func (n HTTPServer) RecordMetrics(ctx context.Context, md ServerMetricData) { |
| 262 |
attributes := n.MetricAttributes(md.ServerName, md.Req, md.StatusCode, md.Route, md.AdditionalAttributes) |
| 263 |
o := metric.WithAttributeSet(attribute.NewSet(attributes...)) |
| 264 |
recordOpts := metricRecordOptionPool.Get().(*[]metric.RecordOption) |
| 265 |
*recordOpts = append(*recordOpts, o) |
| 266 |
n.requestBodySizeHistogram.Inst().Record(ctx, md.RequestSize, *recordOpts...) |
| 267 |
n.responseBodySizeHistogram.Inst().Record(ctx, md.ResponseSize, *recordOpts...) |
| 268 |
n.requestDurationHistogram.Inst().Record(ctx, durationToSeconds(md.RequestDuration), o) |
| 269 |
*recordOpts = (*recordOpts)[:0] |
| 270 |
metricRecordOptionPool.Put(recordOpts) |
| 271 |
} |
| 272 |
|
| 273 |
func (n HTTPServer) method(method string) (attribute.KeyValue, attribute.KeyValue) { |
| 274 |
if method == "" { |
| 275 |
return semconv.HTTPRequestMethodGet, attribute.KeyValue{} |
| 276 |
} |
| 277 |
if attr, ok := methodLookup[method]; ok { |
| 278 |
return attr, attribute.KeyValue{} |
| 279 |
} |
| 280 |
|
| 281 |
orig := semconv.HTTPRequestMethodOriginal(method) |
| 282 |
if attr, ok := methodLookup[strings.ToUpper(method)]; ok { |
| 283 |
return attr, orig |
| 284 |
} |
| 285 |
return semconv.HTTPRequestMethodGet, orig |
| 286 |
} |
| 287 |
|
| 288 |
func (n HTTPServer) scheme(https bool) attribute.KeyValue { //nolint:revive // ignore linter |
| 289 |
if https { |
| 290 |
return semconv.URLScheme("https") |
| 291 |
} |
| 292 |
return semconv.URLScheme("http") |
| 293 |
} |
| 294 |
|
| 295 |
// ResponseTraceAttrs returns trace attributes for telemetry from an HTTP |
| 296 |
// response. |
| 297 |
// |
| 298 |
// If any of the fields in the ResponseTelemetry are not set the attribute will |
| 299 |
// be omitted. |
| 300 |
func (n HTTPServer) ResponseTraceAttrs(resp ResponseTelemetry) []attribute.KeyValue { |
| 301 |
var count int |
| 302 |
|
| 303 |
if resp.ReadBytes > 0 { |
| 304 |
count++ |
| 305 |
} |
| 306 |
if resp.WriteBytes > 0 { |
| 307 |
count++ |
| 308 |
} |
| 309 |
if resp.StatusCode > 0 { |
| 310 |
count++ |
| 311 |
} |
| 312 |
|
| 313 |
attributes := make([]attribute.KeyValue, 0, count) |
| 314 |
|
| 315 |
if resp.ReadBytes > 0 { |
| 316 |
attributes = append(attributes, |
| 317 |
semconv.HTTPRequestBodySize(int(resp.ReadBytes)), |
| 318 |
) |
| 319 |
} |
| 320 |
if resp.WriteBytes > 0 { |
| 321 |
attributes = append(attributes, |
| 322 |
semconv.HTTPResponseBodySize(int(resp.WriteBytes)), |
| 323 |
) |
| 324 |
} |
| 325 |
if resp.StatusCode > 0 { |
| 326 |
attributes = append(attributes, |
| 327 |
semconv.HTTPResponseStatusCode(resp.StatusCode), |
| 328 |
) |
| 329 |
} |
| 330 |
|
| 331 |
return attributes |
| 332 |
} |
| 333 |
|
| 334 |
// Route returns the attribute for the route. |
| 335 |
func (n HTTPServer) Route(route string) attribute.KeyValue { |
| 336 |
return semconv.HTTPRoute(route) |
| 337 |
} |
| 338 |
|
| 339 |
func (n HTTPServer) MetricAttributes(server string, req *http.Request, statusCode int, route string, additionalAttributes []attribute.KeyValue) []attribute.KeyValue { |
| 340 |
num := len(additionalAttributes) + 3 |
| 341 |
var host string |
| 342 |
var p int |
| 343 |
if server == "" { |
| 344 |
host, p = SplitHostPort(req.Host) |
| 345 |
} else { |
| 346 |
// Prioritize the primary server name. |
| 347 |
host, p = SplitHostPort(server) |
| 348 |
if p < 0 { |
| 349 |
_, p = SplitHostPort(req.Host) |
| 350 |
} |
| 351 |
} |
| 352 |
hostPort := requiredHTTPPort(req.TLS != nil, p) |
| 353 |
if hostPort > 0 { |
| 354 |
num++ |
| 355 |
} |
| 356 |
protoName, protoVersion := netProtocol(req.Proto) |
| 357 |
if protoName != "" { |
| 358 |
num++ |
| 359 |
} |
| 360 |
if protoVersion != "" { |
| 361 |
num++ |
| 362 |
} |
| 363 |
|
| 364 |
if statusCode > 0 { |
| 365 |
num++ |
| 366 |
} |
| 367 |
if route == "" && req.Pattern != "" { |
| 368 |
route = httpRoute(req.Pattern) |
| 369 |
} |
| 370 |
if route != "" { |
| 371 |
num++ |
| 372 |
} |
| 373 |
|
| 374 |
attributes := slices.Grow(additionalAttributes, num) |
| 375 |
attributes = append(attributes, |
| 376 |
semconv.HTTPRequestMethodKey.String(standardizeHTTPMethod(req.Method)), |
| 377 |
n.scheme(req.TLS != nil), |
| 378 |
semconv.ServerAddress(host)) |
| 379 |
|
| 380 |
if hostPort > 0 { |
| 381 |
attributes = append(attributes, semconv.ServerPort(hostPort)) |
| 382 |
} |
| 383 |
if protoName != "" { |
| 384 |
attributes = append(attributes, semconv.NetworkProtocolName(protoName)) |
| 385 |
} |
| 386 |
if protoVersion != "" { |
| 387 |
attributes = append(attributes, semconv.NetworkProtocolVersion(protoVersion)) |
| 388 |
} |
| 389 |
|
| 390 |
if statusCode > 0 { |
| 391 |
attributes = append(attributes, semconv.HTTPResponseStatusCode(statusCode)) |
| 392 |
} |
| 393 |
|
| 394 |
if route != "" { |
| 395 |
attributes = append(attributes, semconv.HTTPRoute(route)) |
| 396 |
} |
| 397 |
return attributes |
| 398 |
} |