forked from valkey-io/valkey-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
195 lines (167 loc) · 4.96 KB
/
metrics.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
package valkeyotel
import (
"context"
"crypto/tls"
"net"
"sync/atomic"
"time"
"github.com/valkey-io/valkey-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
var (
defaultHistogramBuckets = []float64{
.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10,
}
)
// MetricAttrs set additional attributes to append to each metric.
func MetricAttrs(attrs ...attribute.KeyValue) Option {
return func(o *otelclient) {
mAttrs := metric.WithAttributeSet(attribute.NewSet(attrs...))
// Allocate slices once and use many times
o.addOpts = []metric.AddOption{mAttrs}
o.recordOpts = []metric.RecordOption{mAttrs}
}
}
// WithMeterProvider sets the MeterProvider for the otelclient.
func WithMeterProvider(provider metric.MeterProvider) Option {
return func(o *otelclient) {
o.meterProvider = provider
}
}
type HistogramOption struct {
Buckets []float64
}
type dialMetrics struct {
addOpts []metric.AddOption
recordOpts []metric.RecordOption
attempt metric.Int64Counter
success metric.Int64Counter
counts metric.Int64UpDownCounter
latency metric.Float64Histogram
}
// WithHistogramOption sets the HistogramOption.
// If not set, DefaultHistogramBuckets will be used.
func WithHistogramOption(histogramOption HistogramOption) Option {
return func(cli *otelclient) {
cli.histogramOption = histogramOption
}
}
// NewClient creates a new Client.
// The following metrics are recorded:
// - valkey_dial_attempt: number of dial attempts
// - valkey_dial_success: number of successful dials
// - valkey_dial_conns: number of active connections
// - valkey_dial_latency: dial latency in seconds
func NewClient(clientOption valkey.ClientOption, opts ...Option) (valkey.Client, error) {
oclient, err := newClient(opts...)
if err != nil {
return nil, err
}
if clientOption.DialFn == nil {
clientOption.DialFn = defaultDialFn
}
metrics := dialMetrics{
addOpts: oclient.addOpts,
recordOpts: oclient.recordOpts,
}
metrics.attempt, err = oclient.meter.Int64Counter("valkey_dial_attempt")
if err != nil {
return nil, err
}
metrics.success, err = oclient.meter.Int64Counter("valkey_dial_success")
if err != nil {
return nil, err
}
metrics.counts, err = oclient.meter.Int64UpDownCounter("valkey_dial_conns")
if err != nil {
return nil, err
}
metrics.latency, err = oclient.meter.Float64Histogram(
"valkey_dial_latency",
metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(oclient.histogramOption.Buckets...),
)
if err != nil {
return nil, err
}
clientOption.DialFn = trackDialing(metrics, clientOption.DialFn)
cli, err := valkey.NewClient(clientOption)
if err != nil {
return nil, err
}
oclient.client = cli
return oclient, nil
}
func newClient(opts ...Option) (*otelclient, error) {
cli := &otelclient{
tAttrs: trace.WithAttributes(),
}
for _, opt := range opts {
opt(cli)
}
if cli.histogramOption.Buckets == nil {
cli.histogramOption.Buckets = defaultHistogramBuckets
}
if cli.meterProvider == nil {
cli.meterProvider = otel.GetMeterProvider() // Default to global MeterProvider
}
if cli.tracerProvider == nil {
cli.tracerProvider = otel.GetTracerProvider() // Default to global TracerProvider
}
// Now that we have the meterProvider and tracerProvider, get the Meter and Tracer
cli.meter = cli.meterProvider.Meter(name)
cli.tracer = cli.tracerProvider.Tracer(name)
// Now create the counters using the meter
var err error
cli.cscMiss, err = cli.meter.Int64Counter("valkey_do_cache_miss")
if err != nil {
return nil, err
}
cli.cscHits, err = cli.meter.Int64Counter("valkey_do_cache_hits")
if err != nil {
return nil, err
}
return cli, nil
}
func trackDialing(m dialMetrics, dialFn func(string, *net.Dialer, *tls.Config) (conn net.Conn, err error)) func(string, *net.Dialer, *tls.Config) (conn net.Conn, err error) {
return func(network string, dialer *net.Dialer, tlsConfig *tls.Config) (conn net.Conn, err error) {
ctx := context.Background()
m.attempt.Add(ctx, 1, m.addOpts...)
start := time.Now()
conn, err = dialFn(network, dialer, tlsConfig)
if err != nil {
return nil, err
}
// Use floating point division for higher precision (instead of Seconds method).
m.latency.Record(ctx, float64(time.Since(start))/float64(time.Second), m.recordOpts...)
m.success.Add(ctx, 1, m.addOpts...)
m.counts.Add(ctx, 1, m.addOpts...)
return &connTracker{
Conn: conn,
counts: m.counts,
addOpts: m.addOpts,
once: 0,
}, nil
}
}
type connTracker struct {
net.Conn
counts metric.Int64UpDownCounter
addOpts []metric.AddOption
once int32
}
func (t *connTracker) Close() error {
if atomic.CompareAndSwapInt32(&t.once, 0, 1) {
t.counts.Add(context.Background(), -1, t.addOpts...)
}
return t.Conn.Close()
}
func defaultDialFn(dst string, dialer *net.Dialer, cfg *tls.Config) (conn net.Conn, err error) {
if cfg != nil {
return tls.DialWithDialer(dialer, "tcp", dst, cfg)
}
return dialer.Dial("tcp", dst)
}