@@ -12,10 +12,36 @@ import (
1212
1313 "go.opentelemetry.io/otel/attribute"
1414 "go.opentelemetry.io/otel/metric"
15+ )
16+
17+ import (
1518 semconv "go.opentelemetry.io/otel/semconv/v1.39.0"
1619 "go.opentelemetry.io/otel/semconv/v1.39.0/httpconv"
1720)
1821
22+ /*
23+ Working principles for tracing/metering HTTP requests/responses with extractor.
24+
25+ 1. Create a Values struct per request served to hold extracted values from Request and Response `v := Values{}`.
26+ 1.1. If your middleware needs to support default/custom values (server name/port, client addr etc) this is good place
27+ to set them.
28+ 2. Extract request values from the HTTP request to Values struct `err := v.ExtractRequest(request)`. These extracted values
29+ are used to start then span and when metrics are recorded at the end of the request (response side)
30+ 3. Create the new span.
31+ 3.1. Attributes for span can be acquired from extracted values with `attr := v.SpanStartAttributes()`
32+ 3.2. If your middleware needs to support additional start attributes, add them to attributes from previous step.
33+ 4. Execute next handler in chain.
34+ 5. Determine if the request / response ended with an error
35+ 6. Extract response values from HTTP response to Values struct
36+ 6.1. Determine the response status that was sent to the client `v.HTTPResponseStatusCode = ?`
37+ 6.2. Extract response body size `v.HTTPResponseBodySize = ?`
38+ 7. Attributes from response can be acquired from extracted values with `attr := v.SpanEndAttributes()`
39+ 7.1. If your middleware needs to support additional end attributes, add them to attributes.
40+ 8. Create a RecordValues struct for metrics recording `iv := RecordValues{...}`.
41+ 9. Record metrics with `m.Record(ctx, iv)`
42+ 10. End the span with `span.End(ctx)`
43+ */
44+
1945// Metrics holds a standard set of OpenTelemetry global request/response metrics
2046type Metrics struct {
2147 // requestDurationHistogram (`http.server.request.duration`) is the duration of HTTP server requests.
0 commit comments