2023-09-07 19:55:58 -04:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-11-01 09:10:09 -04:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2023-09-07 19:55:58 -04:00
|
|
|
|
2023-10-13 15:38:21 -04:00
|
|
|
package otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/felixge/httpsnoop"
|
|
|
|
|
2024-11-01 09:10:09 -04:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconv"
|
2023-12-12 10:28:35 -05:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconvutil"
|
2023-09-07 19:55:58 -04:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
// middleware is an http middleware which wraps the next handler in a span.
|
|
|
|
type middleware struct {
|
2023-09-07 19:55:58 -04:00
|
|
|
operation string
|
2023-10-13 15:38:21 -04:00
|
|
|
server string
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
tracer trace.Tracer
|
|
|
|
meter metric.Meter
|
|
|
|
propagators propagation.TextMapPropagator
|
|
|
|
spanStartOptions []trace.SpanStartOption
|
|
|
|
readEvent bool
|
|
|
|
writeEvent bool
|
|
|
|
filters []Filter
|
|
|
|
spanNameFormatter func(string, *http.Request) string
|
2023-10-13 15:38:21 -04:00
|
|
|
publicEndpoint bool
|
|
|
|
publicEndpointFn func(*http.Request) bool
|
2024-11-01 09:10:09 -04:00
|
|
|
|
|
|
|
traceSemconv semconv.HTTPServer
|
|
|
|
requestBytesCounter metric.Int64Counter
|
|
|
|
responseBytesCounter metric.Int64Counter
|
|
|
|
serverLatencyMeasure metric.Float64Histogram
|
2023-09-07 19:55:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultHandlerFormatter(operation string, _ *http.Request) string {
|
|
|
|
return operation
|
|
|
|
}
|
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
// NewHandler wraps the passed handler in a span named after the operation and
|
|
|
|
// enriches it with metrics.
|
2023-09-07 19:55:58 -04:00
|
|
|
func NewHandler(handler http.Handler, operation string, opts ...Option) http.Handler {
|
2023-12-12 10:28:35 -05:00
|
|
|
return NewMiddleware(operation, opts...)(handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMiddleware returns a tracing and metrics instrumentation middleware.
|
|
|
|
// The handler returned by the middleware wraps a handler
|
|
|
|
// in a span named after the operation and enriches it with metrics.
|
|
|
|
func NewMiddleware(operation string, opts ...Option) func(http.Handler) http.Handler {
|
|
|
|
h := middleware{
|
2023-09-07 19:55:58 -04:00
|
|
|
operation: operation,
|
2024-11-01 09:10:09 -04:00
|
|
|
|
|
|
|
traceSemconv: semconv.NewHTTPServer(),
|
2023-09-07 19:55:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultOpts := []Option{
|
|
|
|
WithSpanOptions(trace.WithSpanKind(trace.SpanKindServer)),
|
|
|
|
WithSpanNameFormatter(defaultHandlerFormatter),
|
|
|
|
}
|
|
|
|
|
|
|
|
c := newConfig(append(defaultOpts, opts...)...)
|
|
|
|
h.configure(c)
|
|
|
|
h.createMeasures()
|
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.serveHTTP(w, r, next)
|
|
|
|
})
|
|
|
|
}
|
2023-09-07 19:55:58 -04:00
|
|
|
}
|
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
func (h *middleware) configure(c *config) {
|
2023-09-07 19:55:58 -04:00
|
|
|
h.tracer = c.Tracer
|
|
|
|
h.meter = c.Meter
|
|
|
|
h.propagators = c.Propagators
|
|
|
|
h.spanStartOptions = c.SpanStartOptions
|
|
|
|
h.readEvent = c.ReadEvent
|
|
|
|
h.writeEvent = c.WriteEvent
|
|
|
|
h.filters = c.Filters
|
|
|
|
h.spanNameFormatter = c.SpanNameFormatter
|
2023-10-13 15:38:21 -04:00
|
|
|
h.publicEndpoint = c.PublicEndpoint
|
|
|
|
h.publicEndpointFn = c.PublicEndpointFn
|
|
|
|
h.server = c.ServerName
|
2023-09-07 19:55:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
func (h *middleware) createMeasures() {
|
2024-11-01 09:10:09 -04:00
|
|
|
var err error
|
|
|
|
h.requestBytesCounter, err = h.meter.Int64Counter(
|
|
|
|
serverRequestSize,
|
2024-02-26 11:50:23 -05:00
|
|
|
metric.WithUnit("By"),
|
2024-11-01 09:10:09 -04:00
|
|
|
metric.WithDescription("Measures the size of HTTP request messages."),
|
2024-02-26 11:50:23 -05:00
|
|
|
)
|
2023-09-07 19:55:58 -04:00
|
|
|
handleErr(err)
|
|
|
|
|
2024-11-01 09:10:09 -04:00
|
|
|
h.responseBytesCounter, err = h.meter.Int64Counter(
|
|
|
|
serverResponseSize,
|
2024-02-26 11:50:23 -05:00
|
|
|
metric.WithUnit("By"),
|
2024-11-01 09:10:09 -04:00
|
|
|
metric.WithDescription("Measures the size of HTTP response messages."),
|
2024-02-26 11:50:23 -05:00
|
|
|
)
|
2023-09-07 19:55:58 -04:00
|
|
|
handleErr(err)
|
|
|
|
|
2024-11-01 09:10:09 -04:00
|
|
|
h.serverLatencyMeasure, err = h.meter.Float64Histogram(
|
|
|
|
serverDuration,
|
2024-02-26 11:50:23 -05:00
|
|
|
metric.WithUnit("ms"),
|
2024-11-01 09:10:09 -04:00
|
|
|
metric.WithDescription("Measures the duration of inbound HTTP requests."),
|
2024-02-26 11:50:23 -05:00
|
|
|
)
|
2023-09-07 19:55:58 -04:00
|
|
|
handleErr(err)
|
|
|
|
}
|
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
// serveHTTP sets up tracing and calls the given next http.Handler with the span
|
|
|
|
// context injected into the request context.
|
|
|
|
func (h *middleware) serveHTTP(w http.ResponseWriter, r *http.Request, next http.Handler) {
|
2023-09-07 19:55:58 -04:00
|
|
|
requestStartTime := time.Now()
|
|
|
|
for _, f := range h.filters {
|
|
|
|
if !f(r) {
|
|
|
|
// Simply pass through to the handler if a filter rejects the request
|
2023-12-12 10:28:35 -05:00
|
|
|
next.ServeHTTP(w, r)
|
2023-09-07 19:55:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:38:21 -04:00
|
|
|
ctx := h.propagators.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
|
|
|
|
opts := []trace.SpanStartOption{
|
2024-11-01 09:10:09 -04:00
|
|
|
trace.WithAttributes(h.traceSemconv.RequestTraceAttrs(h.server, r)...),
|
2023-10-13 15:38:21 -04:00
|
|
|
}
|
2024-11-01 09:10:09 -04:00
|
|
|
|
2023-10-13 15:38:21 -04:00
|
|
|
opts = append(opts, h.spanStartOptions...)
|
|
|
|
if h.publicEndpoint || (h.publicEndpointFn != nil && h.publicEndpointFn(r.WithContext(ctx))) {
|
|
|
|
opts = append(opts, trace.WithNewRoot())
|
|
|
|
// Linking incoming span context if any for public endpoint.
|
|
|
|
if s := trace.SpanContextFromContext(ctx); s.IsValid() && s.IsRemote() {
|
|
|
|
opts = append(opts, trace.WithLinks(trace.Link{SpanContext: s}))
|
|
|
|
}
|
|
|
|
}
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
tracer := h.tracer
|
|
|
|
|
|
|
|
if tracer == nil {
|
|
|
|
if span := trace.SpanFromContext(r.Context()); span.SpanContext().IsValid() {
|
|
|
|
tracer = newTracer(span.TracerProvider())
|
|
|
|
} else {
|
|
|
|
tracer = newTracer(otel.GetTracerProvider())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, span := tracer.Start(ctx, h.spanNameFormatter(h.operation, r), opts...)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
readRecordFunc := func(int64) {}
|
|
|
|
if h.readEvent {
|
|
|
|
readRecordFunc = func(n int64) {
|
|
|
|
span.AddEvent("read", trace.WithAttributes(ReadBytesKey.Int64(n)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var bw bodyWrapper
|
2023-10-13 15:38:21 -04:00
|
|
|
// if request body is nil or NoBody, we don't want to mutate the body as it
|
|
|
|
// will affect the identity of it in an unforeseeable way because we assert
|
|
|
|
// ReadCloser fulfills a certain interface and it is indeed nil or NoBody.
|
|
|
|
if r.Body != nil && r.Body != http.NoBody {
|
2023-09-07 19:55:58 -04:00
|
|
|
bw.ReadCloser = r.Body
|
|
|
|
bw.record = readRecordFunc
|
|
|
|
r.Body = &bw
|
|
|
|
}
|
|
|
|
|
|
|
|
writeRecordFunc := func(int64) {}
|
|
|
|
if h.writeEvent {
|
|
|
|
writeRecordFunc = func(n int64) {
|
|
|
|
span.AddEvent("write", trace.WithAttributes(WroteBytesKey.Int64(n)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:38:21 -04:00
|
|
|
rww := &respWriterWrapper{
|
|
|
|
ResponseWriter: w,
|
|
|
|
record: writeRecordFunc,
|
|
|
|
ctx: ctx,
|
|
|
|
props: h.propagators,
|
|
|
|
statusCode: http.StatusOK, // default status code in case the Handler doesn't write anything
|
|
|
|
}
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
// Wrap w to use our ResponseWriter methods while also exposing
|
|
|
|
// other interfaces that w may implement (http.CloseNotifier,
|
|
|
|
// http.Flusher, http.Hijacker, http.Pusher, io.ReaderFrom).
|
|
|
|
|
|
|
|
w = httpsnoop.Wrap(w, httpsnoop.Hooks{
|
|
|
|
Header: func(httpsnoop.HeaderFunc) httpsnoop.HeaderFunc {
|
|
|
|
return rww.Header
|
|
|
|
},
|
|
|
|
Write: func(httpsnoop.WriteFunc) httpsnoop.WriteFunc {
|
|
|
|
return rww.Write
|
|
|
|
},
|
|
|
|
WriteHeader: func(httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
|
|
|
|
return rww.WriteHeader
|
|
|
|
},
|
2024-11-01 09:10:09 -04:00
|
|
|
Flush: func(httpsnoop.FlushFunc) httpsnoop.FlushFunc {
|
|
|
|
return rww.Flush
|
|
|
|
},
|
2023-09-07 19:55:58 -04:00
|
|
|
})
|
|
|
|
|
2024-11-01 09:10:09 -04:00
|
|
|
labeler, found := LabelerFromContext(ctx)
|
|
|
|
if !found {
|
|
|
|
ctx = ContextWithLabeler(ctx, labeler)
|
|
|
|
}
|
2023-09-07 19:55:58 -04:00
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
2023-09-07 19:55:58 -04:00
|
|
|
|
2024-11-01 09:10:09 -04:00
|
|
|
span.SetStatus(semconv.ServerStatus(rww.statusCode))
|
|
|
|
span.SetAttributes(h.traceSemconv.ResponseTraceAttrs(semconv.ResponseTelemetry{
|
|
|
|
StatusCode: rww.statusCode,
|
|
|
|
ReadBytes: bw.read.Load(),
|
|
|
|
ReadError: bw.err,
|
|
|
|
WriteBytes: rww.written,
|
|
|
|
WriteError: rww.err,
|
|
|
|
})...)
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
// Add metrics
|
2023-12-12 10:28:35 -05:00
|
|
|
attributes := append(labeler.Get(), semconvutil.HTTPServerRequestMetrics(h.server, r)...)
|
2023-10-13 15:38:21 -04:00
|
|
|
if rww.statusCode > 0 {
|
|
|
|
attributes = append(attributes, semconv.HTTPStatusCode(rww.statusCode))
|
|
|
|
}
|
2024-11-01 09:10:09 -04:00
|
|
|
o := metric.WithAttributeSet(attribute.NewSet(attributes...))
|
|
|
|
addOpts := []metric.AddOption{o} // Allocate vararg slice once.
|
|
|
|
h.requestBytesCounter.Add(ctx, bw.read.Load(), addOpts...)
|
|
|
|
h.responseBytesCounter.Add(ctx, rww.written, addOpts...)
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
// Use floating point division here for higher precision (instead of Millisecond method).
|
|
|
|
elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond)
|
|
|
|
|
2024-11-01 09:10:09 -04:00
|
|
|
h.serverLatencyMeasure.Record(ctx, elapsedTime, o)
|
2023-09-07 19:55:58 -04:00
|
|
|
}
|
|
|
|
|
2023-12-12 10:28:35 -05:00
|
|
|
// WithRouteTag annotates spans and metrics with the provided route name
|
|
|
|
// with HTTP route attribute.
|
2023-09-07 19:55:58 -04:00
|
|
|
func WithRouteTag(route string, h http.Handler) http.Handler {
|
2024-11-01 09:10:09 -04:00
|
|
|
attr := semconv.NewHTTPServer().Route(route)
|
2023-09-07 19:55:58 -04:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
span := trace.SpanFromContext(r.Context())
|
2023-12-12 10:28:35 -05:00
|
|
|
span.SetAttributes(attr)
|
|
|
|
|
|
|
|
labeler, _ := LabelerFromContext(r.Context())
|
|
|
|
labeler.Add(attr)
|
|
|
|
|
2023-09-07 19:55:58 -04:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|