-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
134 lines (103 loc) · 2.58 KB
/
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
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
package spandecorator
import (
"bytes"
"fmt"
"io"
"net/http"
"github.com/SKF/go-rest-utility/problems"
"github.com/SKF/go-utility/v2/useridcontext"
"github.com/SKF/go-enlight-middleware/spandecorator/internal"
middleware "github.com/SKF/go-enlight-middleware"
)
// Limit tag value to 5000 characters (limit in datadog)
// https://docs.datadoghq.com/tracing/troubleshooting/#data-volume-guidelines
const maxTagValueSize int = 5000
type Middleware struct {
Tracer middleware.Tracer
withBody bool
}
func New(opts ...Option) *Middleware {
mw := &Middleware{Tracer: middleware.DefaultTracer}
for _, opt := range opts {
opt(mw)
}
return mw
}
func (m *Middleware) Middleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
span := m.Tracer.SpanFromContext(ctx)
for k, v := range extractAttributes(r) {
span.AddStringAttribute(k, v)
}
if m.withBody && !emptyBody(r) {
partialBody, err := extractPartialBody(r, maxTagValueSize)
if err != nil {
problems.WriteResponse(ctx, err, w, r)
return
}
span.AddStringAttribute("http.request.body", string(partialBody))
}
next.ServeHTTP(w, r)
})
}
}
func extractAttributes(r *http.Request) map[string]string {
attributes := map[string]string{}
for key, values := range r.Header {
if shouldIgnore(key) {
continue
}
switch len(values) {
case 0:
attributes[fmt.Sprintf("header.%s", key)] = ""
case 1:
attributes[fmt.Sprintf("header.%s", key)] = values[0]
default:
for i := range values {
attributes[fmt.Sprintf("header.%s.%d", key, i)] = values[i]
}
}
}
userID, ok := useridcontext.FromContext(r.Context())
if ok {
attributes[internal.UserIDKey] = userID
}
return attributes
}
func shouldIgnore(key string) bool {
if key == "Authorization" {
return true
}
if key == "X-Forwarded-For" {
return true
}
return false
}
func extractBody(r *http.Request) ([]byte, error) {
buf := new(bytes.Buffer)
tee := io.TeeReader(r.Body, buf)
b, err := io.ReadAll(tee)
if err != nil {
return nil, fmt.Errorf("unable to read body from request: %w", err)
}
if err = r.Body.Close(); err != nil {
return nil, err
}
r.Body = io.NopCloser(buf)
return b, nil
}
func extractPartialBody(r *http.Request, limit int) ([]byte, error) {
b, err := extractBody(r)
if err != nil {
return nil, err
}
if len(b) > limit {
return b[:limit], nil
}
return b, nil
}
func emptyBody(r *http.Request) bool {
return r.Body == nil || r.Body == http.NoBody
}