generated from keboola/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attrs.go
266 lines (240 loc) · 7.92 KB
/
attrs.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package otel
import (
"net/http"
"net/url"
"reflect"
"sort"
"strings"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/semconv/v1.18.0/httpconv"
"github.com/keboola/go-client/pkg/request"
)
const (
maskedAttrValue = "****"
maskedURLPart = "...."
attrDefHeader = "http.header."
attrDefQueryParam = "http.query."
attrDefPathParam = "http.url.path_params."
attrQueryParam = "http.query."
attrRequestHeader = "http.header."
attrResponseHeader = "http.response.header."
)
type attributes struct {
config config
definitionURL *url.URL
httpURL *url.URL
// definition attributes for span and metrics
definition []attribute.KeyValue
// definitionExtra attributes for span only
definitionExtra []attribute.KeyValue
// httpRequest attributes for span and metrics
httpRequest []attribute.KeyValue
// httpRequestExtra attributes for span only
httpRequestExtra []attribute.KeyValue
// httpResponse attributes for span and metrics
httpResponse []attribute.KeyValue
// httpResponseExtra attributes for span only
httpResponseExtra []attribute.KeyValue
redactedPathValues []string
}
func newAttributes(cfg config, reqDef request.HTTPRequest) *attributes {
out := &attributes{config: cfg}
reqURL := reqDef.URL()
// Result type
var resultType string
if v := reflect.TypeOf(reqDef.ResultDef()); v != nil {
resultType = v.String()
}
// Headers
var headerAttrs []attribute.KeyValue
{
for k, v := range reqDef.RequestHeader() {
value := strings.Join(v, ";")
if _, found := cfg.redactedHeaders[strings.ToLower(k)]; found {
value = maskedAttrValue
}
headerAttrs = append(headerAttrs, attribute.String(attrDefHeader+k, value))
}
sort.SliceStable(headerAttrs, func(i, j int) bool {
return headerAttrs[i].Key < headerAttrs[j].Key
})
out.definitionExtra = append(out.definitionExtra, headerAttrs...)
}
// Query params
var queryAttrs []attribute.KeyValue
{
for key, values := range reqDef.QueryParams() {
value := strings.Join(values, ";")
if _, found := cfg.redactedQueryParams[strings.ToLower(key)]; found {
value = maskedAttrValue
}
queryAttrs = append(queryAttrs, attribute.String(attrDefQueryParam+key, value))
}
sort.SliceStable(queryAttrs, func(i, j int) bool {
return queryAttrs[i].Key < queryAttrs[j].Key
})
}
// Path params
var definitionPathAttrs []attribute.KeyValue
{
for key, value := range reqDef.PathParams() {
out.redactedPathValues = append(out.redactedPathValues, value)
if _, found := cfg.redactedPathParams[strings.ToLower(key)]; found {
value = maskedAttrValue
reqURL.Path = strings.ReplaceAll(reqURL.Path, value, maskedURLPart)
}
definitionPathAttrs = append(definitionPathAttrs, attribute.String(attrDefPathParam+key, value))
}
sort.SliceStable(definitionPathAttrs, func(i, j int) bool {
return definitionPathAttrs[i].Key < definitionPathAttrs[j].Key
})
}
// Base
out.definitionURL = reqURL
out.definition = []attribute.KeyValue{
attribute.String("http.result_type", resultType),
attribute.String("http.method", reqDef.Method()),
attribute.String("http.url", mustURLPathUnescape(reqURL.String())),
attribute.String("http.url_details.scheme", reqURL.Scheme),
attribute.String("http.url_details.path", mustURLPathUnescape(reqURL.Path)),
attribute.String("http.url_details.host", reqURL.Host),
}
if dotPos := strings.IndexByte(reqURL.Host, '.'); dotPos > 0 {
// Host parts: to trace service name (host prefix) and stack (host suffix).
out.definition = append(out.definition,
// Host prefix, e.g. "connection", "encryption", "scheduler" ...
attribute.String("http.url_details.host_prefix", reqURL.Host[:dotPos]),
// Host suffix, e.g. "keboola.com"
attribute.String("http.url_details.host_suffix", strings.TrimLeft(reqURL.Host[dotPos:], ".")),
)
}
// Extra
out.definitionExtra = append(out.definitionExtra, headerAttrs...)
out.definitionExtra = append(out.definitionExtra, definitionPathAttrs...)
out.definitionExtra = append(out.definitionExtra, queryAttrs...)
return out
}
func (v *attributes) SetFromRequest(reqOriginal *http.Request) {
if reqOriginal == nil {
v.httpURL = nil
v.httpRequest = nil
v.httpRequestExtra = nil
return
}
// Clone URL to use redacted query params in the URL attribute generated by the httpconv.ClientRequest call.
reqVal := *reqOriginal
req := &reqVal
urlVal := *reqOriginal.URL
reqVal.URL = &urlVal
// Replace redacted values in the URL path
for _, value := range v.redactedPathValues {
req.URL.Path = strings.ReplaceAll(req.URL.Path, value, maskedURLPart)
}
// Query params
var queryAttrs []attribute.KeyValue
{
query := req.URL.Query()
for key, values := range query {
// Mask redacted params
value := strings.Join(values, ";")
if _, found := v.config.redactedQueryParams[strings.ToLower(key)]; found {
value = maskedAttrValue
}
queryAttrs = append(queryAttrs, attribute.String(attrQueryParam+key, value))
// Remove query values for URL attributes
for i := range values {
values[i] = maskedURLPart
}
query[key] = values
}
sort.SliceStable(queryAttrs, func(i, j int) bool {
return queryAttrs[i].Key < queryAttrs[j].Key
})
// Modify query value for the httpconv.ClientRequest call,
// so URL attribute will be redacted too.
req.URL.RawQuery = query.Encode()
}
// Headers
var headerAttrs []attribute.KeyValue
{
for key, values := range req.Header {
key = strings.ToLower(key)
value := strings.Join(values, ";")
if key == "user-agent" {
// Skip, it is already present from httpconv
continue
}
if _, found := v.config.redactedHeaders[key]; found {
value = maskedAttrValue
}
headerAttrs = append(headerAttrs, attribute.String(attrRequestHeader+key, value))
}
sort.SliceStable(headerAttrs, func(i, j int) bool {
return headerAttrs[i].Key < headerAttrs[j].Key
})
}
// Base
v.httpURL = req.URL
v.httpRequest = httpconv.ClientRequest(req)
v.httpRequest = append(
v.httpRequest,
attribute.String("http.url_details.scheme", req.URL.Scheme),
attribute.String("http.url_details.path", mustURLPathUnescape(req.URL.Path)),
attribute.String("http.url_details.host", req.URL.Host),
)
if dotPos := strings.IndexByte(req.URL.Host, '.'); dotPos > 0 {
// Host parts: to trace service name (host prefix) and stack (host suffix).
v.httpRequest = append(v.httpRequest,
// Host prefix, e.g. "connection", "encryption", "scheduler" ...
attribute.String("http.url_details.host_prefix", req.URL.Host[:dotPos]),
// Host suffix, e.g. "keboola.com"
attribute.String("http.url_details.host_suffix", strings.TrimLeft(req.URL.Host[dotPos:], ".")),
)
}
// Extra
v.httpRequestExtra = nil
v.httpRequestExtra = append(v.httpRequestExtra, headerAttrs...)
v.httpRequestExtra = append(v.httpRequestExtra, queryAttrs...)
}
func (v *attributes) SetFromResponse(res *http.Response, err error) {
// Success
if res == nil {
v.httpResponse = nil
v.httpResponseExtra = nil
} else {
// Headers
var headerAttrs []attribute.KeyValue
{
for key, values := range res.Header {
key = strings.ToLower(key)
value := strings.Join(values, ";")
if _, found := v.config.redactedHeaders[key]; found {
value = maskedAttrValue
}
headerAttrs = append(headerAttrs, attribute.String(attrResponseHeader+key, value))
}
sort.SliceStable(headerAttrs, func(i, j int) bool {
return headerAttrs[i].Key < headerAttrs[j].Key
})
}
// Base
v.httpResponse = httpconv.ClientResponse(res)
if isRedirection(res) {
v.httpResponse = append(v.httpResponse, attribute.Bool("http.is_redirection", true))
}
// Extra
v.httpResponseExtra = nil
v.httpResponseExtra = append(v.httpResponseExtra, headerAttrs...)
}
// Error
if errType := errorType(res, err); errType != "" {
v.httpResponse = append(v.httpResponse, attribute.String("http.error_type", errType))
}
}
func mustURLPathUnescape(in string) string {
out, err := url.PathUnescape(in)
if err != nil {
return in
}
return out
}