github.com/MetalBlockchain/metalgo@v1.11.9/api/traced_handler.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package api 5 6 import ( 7 "net/http" 8 9 "go.opentelemetry.io/otel/attribute" 10 11 "github.com/MetalBlockchain/metalgo/trace" 12 13 oteltrace "go.opentelemetry.io/otel/trace" 14 ) 15 16 var _ http.Handler = (*tracedHandler)(nil) 17 18 type tracedHandler struct { 19 h http.Handler 20 serveHTTPTag string 21 tracer trace.Tracer 22 } 23 24 func TraceHandler(h http.Handler, name string, tracer trace.Tracer) http.Handler { 25 return &tracedHandler{ 26 h: h, 27 serveHTTPTag: name + ".ServeHTTP", 28 tracer: tracer, 29 } 30 } 31 32 func (h *tracedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 33 ctx := r.Context() 34 ctx, span := h.tracer.Start(ctx, h.serveHTTPTag, oteltrace.WithAttributes( 35 attribute.String("method", r.Method), 36 attribute.String("url", r.URL.Redacted()), 37 attribute.String("proto", r.Proto), 38 attribute.String("host", r.Host), 39 attribute.String("remoteAddr", r.RemoteAddr), 40 attribute.String("requestURI", r.RequestURI), 41 )) 42 defer span.End() 43 44 r = r.WithContext(ctx) 45 h.h.ServeHTTP(w, r) 46 }