-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathreports.go
More file actions
114 lines (94 loc) · 3.12 KB
/
reports.go
File metadata and controls
114 lines (94 loc) · 3.12 KB
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
package metrics
import (
"context"
"strconv"
"time"
"golang.org/x/exp/constraints"
)
func CustomReport(reportFn func(s Statter, tagSpec []string), tags ...Tags) {
clientMux.RLock()
defer clientMux.RUnlock()
if client == nil {
return
}
reportFn(client, JoinTags(tags...))
}
func SlowSubscriberEventsDropped(amount int, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Count("pubsub.slow_subscriber.events_dropped", int64(amount), tagSpec, 1)
})
}
func SpotTradesBatchSubmitted(size int, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Count("events.spot_trades_batch.size", int64(size), tagSpec, 1)
})
}
func DerivativeTradesBatchSubmitted(size int, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Count("events.derivative_trades_batch.size", int64(size), tagSpec, 1)
})
}
func IndexPriceUpdatesBatchSubmitted(size int, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Count("events.set_price_batch.size", int64(size), tagSpec, 1)
})
}
func Counter[T constraints.Integer](metric string, value T, tags ...interface{}) {
CustomReport(func(s Statter, tagSpec []string) {
s.Count(metric, int64(value), tagSpec, 1)
}, Combine(tags...))
}
func CounterPositive[T constraints.Integer](metric string, value T, tags ...interface{}) {
if value > 0 {
Counter(metric, value, Combine(tags...))
}
}
func Incr(metric string, tags ...interface{}) {
Counter(metric, 1, Combine(tags...))
}
func Timer(metric string, value time.Duration, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Timing(metric, value, tagSpec, 1)
}, tags...)
}
// Histogram records a value in milliseconds.
func Histogram(metric string, value time.Duration, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Histogram(metric, value.Seconds()*1000, tagSpec, 1)
}, tags...)
}
// Timing supports both Tags or pairs of key-value arguments.
func Timing(metric string, initialTags ...interface{}) func(deferredTags ...interface{}) {
start := time.Now()
it := Combine(initialTags...)
return func(deferredTags ...interface{}) {
dt := Combine(deferredTags...)
Timer(metric, time.Since(start), MergeTags(it, dt))
}
}
// TimingWithErr supports both Tags or pairs of key-value arguments.
func TimingWithErr(metric string, initialTags ...interface{}) func(err *error, deferredTags ...interface{}) {
stop := Timing(metric, initialTags...)
return func(err *error, deferredTags ...interface{}) {
dt := append(deferredTags, "error", BoolTag(err != nil && *err != nil))
stop(dt...)
}
}
// TimingCtxWithErr supports both Tags or pairs of key-value arguments.
func TimingCtxWithErr(_ context.Context, metric string, initialTags ...interface{}) func(err *error, deferredTags ...interface{}) {
return TimingWithErr(metric, initialTags...)
}
func Gauge(metric string, value float64, tags ...interface{}) {
CustomReport(func(s Statter, tagSpec []string) {
s.Gauge(metric, value, tagSpec, 1)
}, Combine(tags...))
}
func BoolTag(b bool) string {
if b {
return "true"
}
return "false"
}
func IntTag[T constraints.Integer](i T) string {
return strconv.Itoa(int(i))
}