-
Notifications
You must be signed in to change notification settings - Fork 47
/
listeners.go
61 lines (51 loc) · 1.91 KB
/
listeners.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
/*
Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package r2stats
import (
"context"
"strconv"
"github.com/blend/go-sdk/logger"
"github.com/blend/go-sdk/r2"
"github.com/blend/go-sdk/sanitize"
"github.com/blend/go-sdk/stats"
"github.com/blend/go-sdk/timeutil"
)
// AddListeners adds web listeners.
func AddListeners(log logger.FilterListenable, collector stats.Collector, opts ...stats.AddListenerOption) {
if log == nil || collector == nil {
return
}
options := stats.NewAddListenerOptions(opts...)
requestSanitizer := sanitize.NewRequestSanitizer(options.RequestSanitizeDefaults...)
log.Filter(r2.Flag,
stats.FilterNameSanitization,
r2.NewEventFilter(func(_ context.Context, r2e r2.Event) (r2.Event, bool) {
r2e.Request = requestSanitizer.Sanitize(r2e.Request)
return r2e, false
}),
)
log.Filter(r2.FlagResponse,
stats.FilterNameSanitization,
r2.NewEventFilter(func(_ context.Context, r2e r2.Event) (r2.Event, bool) {
r2e.Request = requestSanitizer.Sanitize(r2e.Request)
return r2e, false
}),
)
log.Listen(r2.FlagResponse, stats.ListenerNameStats,
r2.NewEventListener(func(ctx context.Context, r2e r2.Event) {
hostname := stats.Tag(TagHostname, r2e.Request.URL.Hostname())
target := stats.Tag(TagTarget, r2e.Request.URL.Hostname())
method := stats.Tag(TagMethod, r2e.Request.Method)
status := stats.Tag(TagStatus, strconv.Itoa(r2e.Response.StatusCode))
tags := []string{
hostname, target, method, status,
}
tags = append(tags, options.GetLoggerLabelsAsTags(ctx)...)
_ = collector.Increment(MetricNameHTTPClientRequest, tags...)
_ = collector.Gauge(MetricNameHTTPClientRequestElapsedLast, timeutil.Milliseconds(r2e.Elapsed), tags...)
_ = collector.Histogram(MetricNameHTTPClientRequestElapsed, timeutil.Milliseconds(r2e.Elapsed), tags...)
}),
)
}