-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotel_mux.go
104 lines (81 loc) · 3.12 KB
/
otel_mux.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
package main
import (
"fmt"
"net/http"
chi "github.com/go-chi/chi/v5"
)
var _ chi.Router = &OtelMux{}
// Wrapper around base Mux that adds otel metric reporting for APIs
type OtelMux struct {
chi.Router
otelClient Client
}
func NewOtelMux(otelClient Client) *OtelMux {
mx := chi.NewMux()
return &OtelMux{Router: mx, otelClient: otelClient}
}
func (mx *OtelMux) withRouter(router chi.Router) *OtelMux {
return &OtelMux{Router: router, otelClient: mx.otelClient}
}
func (mx *OtelMux) Handle(pattern string, handler http.Handler) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Handle(pattern, handler)
}
func (mx *OtelMux) HandleFunc(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).HandleFunc(pattern, handlerFn)
}
func (mx *OtelMux) Method(method, pattern string, handler http.Handler) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Method(method, pattern, handler)
}
func (mx *OtelMux) Connect(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Connect(pattern, handlerFn)
}
func (mx *OtelMux) Delete(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Delete(pattern, handlerFn)
}
func (mx *OtelMux) Get(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Get(pattern, handlerFn)
}
func (mx *OtelMux) Head(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Head(pattern, handlerFn)
}
func (mx *OtelMux) Options(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Options(pattern, handlerFn)
}
func (mx *OtelMux) Patch(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Patch(pattern, handlerFn)
}
func (mx *OtelMux) Post(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Post(pattern, handlerFn)
}
func (mx *OtelMux) Put(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Put(pattern, handlerFn)
}
func (mx *OtelMux) Trace(pattern string, handlerFn http.HandlerFunc) {
mx.With(otelMiddleware(pattern, mx.otelClient)).Trace(pattern, handlerFn)
}
func (mx *OtelMux) Group(fn func(r chi.Router)) chi.Router {
r := mx.Router.Group(fn)
return mx.withRouter(r)
}
func (mx *OtelMux) Route(pattern string, fn func(r chi.Router)) chi.Router {
// override entire Mux.Route method because there is no way to inject a
// difference kind of Router
if fn == nil {
panic(fmt.Sprintf("chi: attempting to Route() a nil subrouter on '%s'", pattern))
}
subRouter := NewOtelMux(mx.otelClient)
fn(subRouter)
mx.Mount(pattern, subRouter)
return subRouter
}
// TODO: fix this
func (mx *OtelMux) With(middlewares ...func(http.Handler) http.Handler) chi.Router {
r := mx.Router.With(middlewares...)
return mx.withRouter(r)
}
// Note: this will result in a stack overflow error
// func (mx *OtelMux) With(middlewares ...func(http.Handler) http.Handler) chi.Router {
// mx = NewOtelMux(mx.otelClient)
// mx.Router = mx.Router.With(middlewares...)
// return mx
// }