forked from riandyrn/otelchi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
223 lines (193 loc) · 6.33 KB
/
middleware.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package otelchi
import (
"context"
"net/http"
"sync"
"github.com/felixge/httpsnoop"
"github.com/go-chi/chi/v5"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/semconv/v1.20.0/httpconv"
oteltrace "go.opentelemetry.io/otel/trace"
)
const (
tracerName = "github.com/riandyrn/otelchi"
)
// Middleware sets up a handler to start tracing the incoming
// requests. The serverName parameter should describe the name of the
// (virtual) server handling the request.
func Middleware(serverName string, opts ...Option) func(next http.Handler) http.Handler {
cfg := config{}
for _, opt := range opts {
opt.apply(&cfg)
}
if cfg.TracerProvider == nil {
cfg.TracerProvider = otel.GetTracerProvider()
}
tracer := cfg.TracerProvider.Tracer(
tracerName,
oteltrace.WithInstrumentationVersion(Version()),
)
if cfg.Propagators == nil {
cfg.Propagators = otel.GetTextMapPropagator()
}
return func(handler http.Handler) http.Handler {
return traceware{
serverName: serverName,
tracer: tracer,
propagators: cfg.Propagators,
handler: handler,
chiRoutes: cfg.ChiRoutes,
reqMethodInSpanName: cfg.RequestMethodInSpanName,
filters: cfg.Filters,
traceResponseHeaderKey: cfg.TraceResponseHeaderKey,
publicEndpointFn: cfg.PublicEndpointFn,
}
}
}
type traceware struct {
serverName string
tracer oteltrace.Tracer
propagators propagation.TextMapPropagator
handler http.Handler
chiRoutes chi.Routes
reqMethodInSpanName bool
filters []Filter
traceResponseHeaderKey string
publicEndpointFn func(r *http.Request) bool
}
type recordingResponseWriter struct {
writer http.ResponseWriter
written bool
status int
}
var rrwPool = &sync.Pool{
New: func() interface{} {
return &recordingResponseWriter{}
},
}
func getRRW(writer http.ResponseWriter) *recordingResponseWriter {
rrw := rrwPool.Get().(*recordingResponseWriter)
rrw.written = false
rrw.status = http.StatusOK
rrw.writer = httpsnoop.Wrap(writer, httpsnoop.Hooks{
Write: func(next httpsnoop.WriteFunc) httpsnoop.WriteFunc {
return func(b []byte) (int, error) {
if !rrw.written {
rrw.written = true
}
return next(b)
}
},
WriteHeader: func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
return func(statusCode int) {
if !rrw.written {
rrw.written = true
rrw.status = statusCode
}
next(statusCode)
}
},
})
return rrw
}
func putRRW(rrw *recordingResponseWriter) {
rrw.writer = nil
rrwPool.Put(rrw)
}
// ServeHTTP implements the http.Handler interface. It does the actual
// tracing of the request.
func (tw traceware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// go through all filters if any
for _, filter := range tw.filters {
// if there is a filter that returns false, we skip tracing
// and execute next handler
if !filter(r) {
tw.handler.ServeHTTP(w, r)
return
}
}
// extract tracing header using propagator
ctx := tw.propagators.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
// create span, based on specification, we need to set already known attributes
// when creating the span, the only thing missing here is HTTP route pattern since
// in go-chi/chi route pattern could only be extracted once the request is executed
// check here for details:
//
// https://github.com/go-chi/chi/issues/150#issuecomment-278850733
//
// if we have access to chi routes, we could extract the route pattern beforehand.
spanName := ""
routePattern := ""
spanAttributes := httpconv.ServerRequest(tw.serverName, r)
if tw.chiRoutes != nil {
rctx := chi.NewRouteContext()
if tw.chiRoutes.Match(rctx, r.Method, r.URL.Path) {
routePattern = rctx.RoutePattern()
spanName = addPrefixToSpanName(tw.reqMethodInSpanName, r.Method, routePattern)
spanAttributes = append(spanAttributes, semconv.HTTPRoute(routePattern))
}
}
// define span start options
spanOpts := []oteltrace.SpanStartOption{
oteltrace.WithAttributes(spanAttributes...),
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}
if tw.publicEndpointFn != nil && tw.publicEndpointFn(r) {
// mark span as the root span
spanOpts = append(spanOpts, oteltrace.WithNewRoot())
// linking incoming span context to the root span, we need to
// ensure if the incoming span context is valid (because it is
// possible for us to receive invalid span context due to various
// reason such as bug or context propagation error) and it is
// coming from another service (remote) before linking it to the
// root span
spanCtx := oteltrace.SpanContextFromContext(ctx)
if spanCtx.IsValid() && spanCtx.IsRemote() {
spanOpts = append(
spanOpts,
oteltrace.WithLinks(oteltrace.Link{
SpanContext: spanCtx,
}),
)
}
}
// start span
ctx, span := tw.tracer.Start(ctx, spanName, spanOpts...)
defer span.End()
// put trace_id to response header only when WithTraceResponseHeaderKey is used
if len(tw.traceResponseHeaderKey) > 0 && span.SpanContext().HasTraceID() {
w.Header().Add(tw.traceResponseHeaderKey, span.SpanContext().TraceID().String())
}
// get recording response writer
rrw := getRRW(w)
defer putRRW(rrw)
ctx = context.WithValue(ctx, "trace-id", span.SpanContext().TraceID().String())
// execute next http handler
r = r.WithContext(ctx)
tw.handler.ServeHTTP(rrw.writer, r)
// set span name & http route attribute if route pattern cannot be determined
// during span creation
if len(routePattern) == 0 {
routePattern = chi.RouteContext(r.Context()).RoutePattern()
span.SetAttributes(semconv.HTTPRoute(routePattern))
spanName = addPrefixToSpanName(tw.reqMethodInSpanName, r.Method, routePattern)
span.SetName(spanName)
}
// set status code attribute
span.SetAttributes(semconv.HTTPStatusCode(rrw.status))
// set span status
span.SetStatus(httpconv.ServerStatus(rrw.status))
}
func addPrefixToSpanName(shouldAdd bool, prefix, spanName string) string {
// in chi v5.0.8, the root route will be returned has an empty string
// (see https://github.com/go-chi/chi/blob/v5.0.8/context.go#L126)
if spanName == "" {
spanName = "/"
}
if shouldAdd && len(spanName) > 0 {
spanName = prefix + " " + spanName
}
return spanName
}