-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathconfig.go
More file actions
267 lines (226 loc) · 8.06 KB
/
config.go
File metadata and controls
267 lines (226 loc) · 8.06 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
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
267
package redisotel
import (
"strings"
"go.opentelemetry.io/otel/metric"
)
type MetricGroup string
const (
MetricGroupCommand MetricGroup = "command"
MetricGroupConnectionBasic MetricGroup = "connection-basic"
MetricGroupResiliency MetricGroup = "resiliency"
MetricGroupConnectionAdvanced MetricGroup = "connection-advanced"
MetricGroupPubSub MetricGroup = "pubsub"
MetricGroupStream MetricGroup = "stream"
)
type HistogramAggregation string
const (
HistogramAggregationExplicitBucket HistogramAggregation = "explicit_bucket_histogram"
HistogramAggregationBase2Exponential HistogramAggregation = "base2_exponential_bucket_histogram"
)
type config struct {
// Core settings
meterProvider metric.MeterProvider
enabled bool
// Metric group settings
enabledMetricGroups map[MetricGroup]bool
// Command filtering
includeCommands map[string]bool // nil means include all
excludeCommands map[string]bool // nil means exclude none
// Cardinality reduction
hidePubSubChannelNames bool
hideStreamNames bool
// Histogram settings
histAggregation HistogramAggregation
// Bucket configurations for different histogram metrics
bucketsOperationDuration []float64
bucketsStreamProcessingDuration []float64
bucketsConnectionCreateTime []float64
bucketsConnectionWaitTime []float64
}
func (c *config) isMetricGroupEnabled(group MetricGroup) bool {
return c.enabledMetricGroups[group]
}
func (c *config) isCommandIncluded(command string) bool {
command = strings.ToLower(command)
if c.excludeCommands != nil && c.excludeCommands[command] {
return false
}
if c.includeCommands != nil {
return c.includeCommands[command]
}
return true
}
// defaultHistogramBuckets returns the default histogram buckets for all duration metrics.
// These buckets are designed to capture typical Redis operation and connection latencies:
// - Sub-millisecond: 0.0001s (0.1ms), 0.0005s (0.5ms)
// - Milliseconds: 0.001s (1ms), 0.005s (5ms), 0.01s (10ms), 0.05s (50ms), 0.1s (100ms)
// - Sub-second: 0.5s (500ms)
// - Seconds: 1s, 5s, 10s
//
// This covers the range from 0.1ms to 10s, which is suitable for:
// - db.client.operation.duration (command execution time)
// - db.client.connection.create_time (connection establishment)
// - db.client.connection.wait_time (waiting for connection from pool)
// - redis.client.stream.processing_duration (stream message processing)
func defaultHistogramBuckets() []float64 {
return []float64{
0.0001, // 0.1ms
0.0005, // 0.5ms
0.001, // 1ms
0.005, // 5ms
0.01, // 10ms
0.05, // 50ms
0.1, // 100ms
0.5, // 500ms
1.0, // 1s
5.0, // 5s
10.0, // 10s
}
}
// MetricGroupFlags represents metric groups as bitwise flags
type MetricGroupFlags uint32
const (
MetricGroupFlagCommand MetricGroupFlags = 1 << 0
MetricGroupFlagConnectionBasic MetricGroupFlags = 1 << 1
MetricGroupFlagResiliency MetricGroupFlags = 1 << 2
MetricGroupFlagConnectionAdvanced MetricGroupFlags = 1 << 3
MetricGroupFlagPubSub MetricGroupFlags = 1 << 4
MetricGroupFlagStream MetricGroupFlags = 1 << 5
// MetricGroupAll enables all metric groups
MetricGroupAll MetricGroupFlags = MetricGroupFlagCommand |
MetricGroupFlagConnectionBasic |
MetricGroupFlagResiliency |
MetricGroupFlagConnectionAdvanced |
MetricGroupFlagPubSub |
MetricGroupFlagStream
)
// Use NewConfig() to create a new instance with defaults, then chain
// builder methods to customize.
//
// Example:
//
// config := redisotel.NewConfig().
// WithEnabled(true).
// WithMetricGroups(redisotel.MetricGroupAll).
// WithMeterProvider(myProvider)
//
// otel := redisotel.GetObservabilityInstance()
// otel.Init(config)
type Config struct {
// Core settings
Enabled bool
MeterProvider metric.MeterProvider
// Metric groups (bitwise flags)
MetricGroups MetricGroupFlags
// Command filtering
IncludeCommands map[string]bool // nil means include all
ExcludeCommands map[string]bool // nil means exclude none
// Cardinality reduction
HidePubSubChannelNames bool
HideStreamNames bool
// Histogram settings
HistogramAggregation HistogramAggregation
// Bucket configurations for different histogram metrics
BucketsOperationDuration []float64
BucketsStreamLag []float64
BucketsConnectionCreateTime []float64
BucketsConnectionWaitTime []float64
}
// NewConfig creates a new Config with default values.
// Default configuration:
// - Enabled: false (must explicitly enable)
// - MetricGroups: all metric groups (command, connection, resiliency, pubsub, stream)
// - HistogramAggregation: explicit bucket
// - Buckets: 0.1ms to 10s (suitable for Redis operations)
//
// Example:
//
// config := redisotel.NewConfig().
// WithEnabled(true)
//
// To disable specific metric groups, use WithMetricGroups:
//
// config := redisotel.NewConfig().
// WithEnabled(true).
// WithMetricGroups(redisotel.MetricGroupFlagConnectionBasic | redisotel.MetricGroupFlagResiliency)
func NewConfig() *Config {
return &Config{
Enabled: false,
MeterProvider: nil, // Will use global otel.GetMeterProvider() if nil
// Default metric groups: all groups enabled for comprehensive observability
MetricGroups: MetricGroupAll,
// No command filtering by default
IncludeCommands: nil,
ExcludeCommands: nil,
// Don't hide labels by default
HidePubSubChannelNames: false,
HideStreamNames: false,
// Use explicit bucket histogram by default
HistogramAggregation: HistogramAggregationExplicitBucket,
// Default buckets for all duration metrics
BucketsOperationDuration: defaultHistogramBuckets(),
BucketsStreamLag: defaultHistogramBuckets(),
BucketsConnectionCreateTime: defaultHistogramBuckets(),
BucketsConnectionWaitTime: defaultHistogramBuckets(),
}
}
// WithEnabled enables or disables metrics emission.
// Default: false (must explicitly enable)
func (c *Config) WithEnabled(enabled bool) *Config {
c.Enabled = enabled
return c
}
// WithMeterProvider sets the meter provider to use for creating metrics.
// If not provided, the global meter provider from otel.GetMeterProvider() will be used.
func (c *Config) WithMeterProvider(provider metric.MeterProvider) *Config {
c.MeterProvider = provider
return c
}
// WithMetricGroups sets which metric groups to register using bitwise flags.
// You can combine multiple groups with the | operator.
func (c *Config) WithMetricGroups(groups MetricGroupFlags) *Config {
c.MetricGroups = groups
return c
}
// WithIncludeCommands sets a command allow-list for metrics.
func (c *Config) WithIncludeCommands(commands []string) *Config {
c.IncludeCommands = make(map[string]bool)
for _, cmd := range commands {
c.IncludeCommands[strings.ToLower(cmd)] = true
}
return c
}
// WithExcludeCommands sets a command deny-list for metrics.
// Commands in this list will not have metrics recorded.
func (c *Config) WithExcludeCommands(commands []string) *Config {
c.ExcludeCommands = make(map[string]bool)
for _, cmd := range commands {
c.ExcludeCommands[strings.ToLower(cmd)] = true
}
return c
}
// WithHidePubSubChannelNames omits channel label from Pub/Sub metrics to reduce cardinality.
func (c *Config) WithHidePubSubChannelNames(hide bool) *Config {
c.HidePubSubChannelNames = hide
return c
}
// WithHideStreamNames omits stream label from stream metrics to reduce cardinality.
func (c *Config) WithHideStreamNames(hide bool) *Config {
c.HideStreamNames = hide
return c
}
// WithHistogramAggregation sets the histogram aggregation mode.
func (c *Config) WithHistogramAggregation(agg HistogramAggregation) *Config {
c.HistogramAggregation = agg
return c
}
// WithHistogramBuckets sets custom histogram buckets for ALL duration metrics.
// If not set, uses defaultHistogramBuckets() which covers 0.1ms to 10s.
// Buckets should be in seconds (e.g., 0.001 = 1ms, 0.1 = 100ms, 1.0 = 1s).
func (c *Config) WithHistogramBuckets(buckets []float64) *Config {
c.BucketsOperationDuration = buckets
c.BucketsStreamLag = buckets
c.BucketsConnectionCreateTime = buckets
c.BucketsConnectionWaitTime = buckets
return c
}