/
config.go
186 lines (151 loc) · 4.03 KB
/
config.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
package http
import (
"net/http"
"regexp"
"strings"
"github.com/tel-io/tel/v2"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
var (
reID = regexp.MustCompile(`^\d+$`)
reResource = regexp.MustCompile(`^[a-zA-Z0-9\-]+\.\w{2,4}$`) // .css, .js, .png, .jpeg, etc.
reUUID = regexp.MustCompile(`^[a-f\d]{4}(?:[a-f\d]{4}-){4}[a-f\d]{12}$`)
decreasePathCardinality = func(path string) string {
var b strings.Builder
path = strings.TrimLeft(path, "/")
pathParts := strings.Split(path, "/")
for _, part := range pathParts {
b.WriteString("/")
p := part
if reID.MatchString(part) {
p = ":id:"
} else if reResource.MatchString(part) {
p = ":resource:"
} else if reUUID.MatchString(part) {
p = ":uuid:"
}
b.WriteString(p)
}
return b.String()
}
DefaultSpanNameFormatter = func(_ string, r *http.Request) string {
var b strings.Builder
b.WriteString(r.Method)
b.WriteString(":")
b.WriteString(decreasePathCardinality(r.URL.Path))
return b.String()
}
DefaultFilter = func(r *http.Request) bool {
if k, ok := r.Header["Upgrade"]; ok {
for _, v := range k {
if v == "websocket" {
return false
}
}
}
return !(r.Method == http.MethodGet && strings.HasPrefix(r.URL.RequestURI(), "/health"))
}
)
type PathExtractor func(r *http.Request) string
type config struct {
log *tel.Telemetry
operation string
otelOpts []otelhttp.Option
pathExtractor PathExtractor
filters []otelhttp.Filter
readRequest bool
readHeader bool
writeResponse bool
dumpPayloadOnError bool
}
// Option interface used for setting optional config properties.
type Option interface {
apply(*config)
}
type optionFunc func(*config)
func (o optionFunc) apply(c *config) {
o(c)
}
// newConfig creates a new config struct and applies opts to it.
func newConfig(opts ...Option) *config {
l := tel.Global()
c := &config{
log: &l,
operation: "HTTP",
otelOpts: []otelhttp.Option{
otelhttp.WithSpanNameFormatter(DefaultSpanNameFormatter),
otelhttp.WithFilter(DefaultFilter),
},
pathExtractor: DefaultURI,
filters: []otelhttp.Filter{DefaultFilter},
dumpPayloadOnError: true,
}
for _, opt := range opts {
opt.apply(c)
}
return c
}
// WithTel also add options to pass own metric and trace provider
func WithTel(t *tel.Telemetry) Option {
return optionFunc(func(c *config) {
c.log = t
c.otelOpts = append(c.otelOpts,
otelhttp.WithMeterProvider(t.MetricProvider()),
otelhttp.WithTracerProvider(t.TracerProvider()),
)
})
}
func WithOperation(name string) Option {
return optionFunc(func(c *config) {
c.operation = name
})
}
func WithOtelOpts(opts ...otelhttp.Option) Option {
return optionFunc(func(c *config) {
c.otelOpts = append(c.otelOpts, opts...)
})
}
func WithPathExtractor(in PathExtractor) Option {
return optionFunc(func(c *config) {
c.pathExtractor = in
})
}
// WithFilter append filter to default
func WithFilter(f ...otelhttp.Filter) Option {
return optionFunc(func(c *config) {
c.filters = append(c.filters, f...)
for _, filter := range f {
c.otelOpts = append(c.otelOpts, otelhttp.WithFilter(filter))
}
})
}
// WithDumpRequest dump request as plain text to log and trace
// i guess we can go further and perform option with encoding requests
func WithDumpRequest(enable bool) Option {
return optionFunc(func(c *config) {
c.readRequest = enable
})
}
// WithHeaders explicitly set possibility to write http headers
func WithHeaders(enable bool) Option {
return optionFunc(func(c *config) {
c.readHeader = enable
})
}
// WithDumpResponse dump response as plain text to log and trace
func WithDumpResponse(enable bool) Option {
return optionFunc(func(c *config) {
c.writeResponse = enable
})
}
func DefaultURI(r *http.Request) string {
return r.URL.RequestURI()
}
// WithDumpPayloadOnError write dump request and response on faults
//
// Default: true
func WithDumpPayloadOnError(enable bool) Option {
return optionFunc(func(c *config) {
c.dumpPayloadOnError = enable
})
}