-
Notifications
You must be signed in to change notification settings - Fork 672
/
traced_handler.go
47 lines (37 loc) · 1.11 KB
/
traced_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package api
import (
"fmt"
"net/http"
"go.opentelemetry.io/otel/attribute"
oteltrace "go.opentelemetry.io/otel/trace"
"github.com/ava-labs/avalanchego/trace"
)
var _ http.Handler = (*tracedHandler)(nil)
type tracedHandler struct {
h http.Handler
serveHTTPTag string
tracer trace.Tracer
}
func TraceHandler(h http.Handler, name string, tracer trace.Tracer) http.Handler {
return &tracedHandler{
h: h,
serveHTTPTag: fmt.Sprintf("%s.ServeHTTP", name),
tracer: tracer,
}
}
func (h *tracedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx, span := h.tracer.Start(ctx, h.serveHTTPTag, oteltrace.WithAttributes(
attribute.String("method", r.Method),
attribute.String("url", r.URL.Redacted()),
attribute.String("proto", r.Proto),
attribute.String("host", r.Host),
attribute.String("remoteAddr", r.RemoteAddr),
attribute.String("requestURI", r.RequestURI),
))
defer span.End()
r = r.WithContext(ctx)
h.h.ServeHTTP(w, r)
}