-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracer.go
101 lines (78 loc) · 2.7 KB
/
tracer.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package tracer
import (
"fmt"
"net/http"
"strings"
"github.com/alexfalkowski/go-service/meta"
sh "github.com/alexfalkowski/go-service/net/http"
"github.com/alexfalkowski/go-service/telemetry/tracer"
tm "github.com/alexfalkowski/go-service/transport/meta"
ts "github.com/alexfalkowski/go-service/transport/strings"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
"go.opentelemetry.io/otel/trace"
)
// Handler for tracer.
type Handler struct {
tracer trace.Tracer
}
// NewHandler for tracer.
func NewHandler(tracer trace.Tracer) *Handler {
return &Handler{tracer: tracer}
}
// ServeHTTP for tracer.
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
path, method := req.URL.Path, strings.ToLower(req.Method)
if ts.IsHealth(path) {
next(resp, req)
return
}
ctx := extract(req.Context(), req)
attrs := []attribute.KeyValue{
semconv.HTTPRoute(path),
semconv.HTTPRequestMethodKey.String(method),
}
ctx, span := h.tracer.Start(trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)), operationName(fmt.Sprintf("%s %s", method, path)),
trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(attrs...))
defer span.End()
ctx = tm.WithTraceID(ctx, meta.ToString(span.SpanContext().TraceID()))
res := &sh.ResponseWriter{ResponseWriter: resp, StatusCode: http.StatusOK}
next(res, req.WithContext(ctx))
tracer.Meta(ctx, span)
span.SetAttributes(semconv.HTTPResponseStatusCode(res.StatusCode))
}
// NewRoundTripper for tracer.
func NewRoundTripper(tracer trace.Tracer, hrt http.RoundTripper) *RoundTripper {
return &RoundTripper{tracer: tracer, RoundTripper: hrt}
}
// RoundTripper for tracer.
type RoundTripper struct {
tracer trace.Tracer
http.RoundTripper
}
// RoundTrip for tracer.
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if ts.IsHealth(req.URL.String()) {
return r.RoundTripper.RoundTrip(req)
}
method := strings.ToLower(req.Method)
ctx := req.Context()
attrs := []attribute.KeyValue{
semconv.HTTPRoute(req.URL.Path),
semconv.HTTPRequestMethodKey.String(method),
}
ctx, span := r.tracer.Start(ctx, operationName(fmt.Sprintf("%s %s", method, req.URL.Redacted())), trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attrs...))
defer span.End()
ctx = tm.WithTraceID(ctx, meta.ToString(span.SpanContext().TraceID()))
inject(ctx, req)
resp, err := r.RoundTripper.RoundTrip(req.WithContext(ctx))
tracer.Meta(ctx, span)
tracer.Error(err, span)
if resp != nil {
span.SetAttributes(semconv.HTTPResponseStatusCode(resp.StatusCode))
}
return resp, err
}
func operationName(name string) string {
return tracer.OperationName("http", name)
}