Skip to content

Commit cf9b683

Browse files
committed
remove redundent code
1 parent 28292b2 commit cf9b683

File tree

3 files changed

+105
-236
lines changed

3 files changed

+105
-236
lines changed

extra/redisotel-native/config.go

Lines changed: 41 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,20 @@ import (
44
"go.opentelemetry.io/otel/metric"
55
)
66

7-
// MetricGroup represents a group of related metrics
87
type MetricGroup string
98

109
const (
11-
// MetricGroupCommand includes command-level metrics
12-
MetricGroupCommand MetricGroup = "command"
13-
// MetricGroupConnectionBasic includes basic connection metrics
14-
MetricGroupConnectionBasic MetricGroup = "connection-basic"
15-
// MetricGroupResiliency includes resiliency metrics (errors, retries, etc.)
16-
MetricGroupResiliency MetricGroup = "resiliency"
17-
// MetricGroupConnectionAdvanced includes advanced connection metrics
10+
MetricGroupCommand MetricGroup = "command"
11+
MetricGroupConnectionBasic MetricGroup = "connection-basic"
12+
MetricGroupResiliency MetricGroup = "resiliency"
1813
MetricGroupConnectionAdvanced MetricGroup = "connection-advanced"
19-
// MetricGroupStream includes stream-specific metrics
20-
MetricGroupStream MetricGroup = "stream"
14+
MetricGroupStream MetricGroup = "stream"
2115
)
2216

23-
// HistogramAggregation represents the histogram aggregation mode
2417
type HistogramAggregation string
2518

2619
const (
27-
// HistogramAggregationExplicitBucket uses explicit bucket boundaries
28-
HistogramAggregationExplicitBucket HistogramAggregation = "explicit_bucket_histogram"
29-
// HistogramAggregationBase2Exponential uses base-2 exponential buckets
20+
HistogramAggregationExplicitBucket HistogramAggregation = "explicit_bucket_histogram"
3021
HistogramAggregationBase2Exponential HistogramAggregation = "base2_exponential_bucket_histogram"
3122
)
3223

@@ -58,15 +49,13 @@ type config struct {
5849
bucketsConnectionUseTime []float64
5950
}
6051

61-
// defaultConfig returns the default configuration
6252
func defaultConfig() config {
6353
return config{
6454
meterProvider: nil, // Will use global otel.GetMeterProvider() if nil
6555
enabled: false,
6656

67-
// Default metric groups: command, connection-basic, resiliency
57+
// Default metric groups: connection-basic, resiliency
6858
enabledMetricGroups: map[MetricGroup]bool{
69-
MetricGroupCommand: true,
7059
MetricGroupConnectionBasic: true,
7160
MetricGroupResiliency: true,
7261
},
@@ -82,12 +71,12 @@ func defaultConfig() config {
8271
// Use explicit bucket histogram by default
8372
histAggregation: HistogramAggregationExplicitBucket,
8473

85-
// Default buckets for different metrics
86-
bucketsOperationDuration: defaultOperationDurationBuckets(),
87-
bucketsStreamProcessingDuration: defaultStreamProcessingDurationBuckets(),
88-
bucketsConnectionCreateTime: defaultConnectionCreateTimeBuckets(),
89-
bucketsConnectionWaitTime: defaultConnectionWaitTimeBuckets(),
90-
bucketsConnectionUseTime: defaultConnectionUseTimeBuckets(),
74+
// Default buckets for all duration metrics
75+
bucketsOperationDuration: defaultHistogramBuckets(),
76+
bucketsStreamProcessingDuration: defaultHistogramBuckets(),
77+
bucketsConnectionCreateTime: defaultHistogramBuckets(),
78+
bucketsConnectionWaitTime: defaultHistogramBuckets(),
79+
bucketsConnectionUseTime: defaultHistogramBuckets(),
9180
}
9281
}
9382

@@ -98,98 +87,31 @@ func (c *config) isMetricGroupEnabled(group MetricGroup) bool {
9887

9988
// isCommandIncluded checks if a command should be included in metrics
10089
func (c *config) isCommandIncluded(command string) bool {
101-
// If there's an exclude list and command is in it, exclude
10290
if c.excludeCommands != nil && c.excludeCommands[command] {
10391
return false
10492
}
10593

106-
// If there's an include list, only include if command is in it
10794
if c.includeCommands != nil {
10895
return c.includeCommands[command]
10996
}
11097

111-
// No filtering, include all
11298
return true
11399
}
114100

115-
// defaultOperationDurationBuckets returns the default histogram buckets for db.client.operation.duration
116-
// These buckets are designed to capture typical Redis operation latencies:
101+
// defaultHistogramBuckets returns the default histogram buckets for all duration metrics.
102+
// These buckets are designed to capture typical Redis operation and connection latencies:
117103
// - Sub-millisecond: 0.0001s (0.1ms), 0.0005s (0.5ms)
118104
// - Milliseconds: 0.001s (1ms), 0.005s (5ms), 0.01s (10ms), 0.05s (50ms), 0.1s (100ms)
119105
// - Sub-second: 0.5s (500ms)
120-
// - Seconds: 1s, 2.5s
121-
func defaultOperationDurationBuckets() []float64 {
122-
return []float64{
123-
0.0001, // 0.1ms
124-
0.0005, // 0.5ms
125-
0.001, // 1ms
126-
0.005, // 5ms
127-
0.01, // 10ms
128-
0.05, // 50ms
129-
0.1, // 100ms
130-
0.5, // 500ms
131-
1.0, // 1s
132-
2.5, // 2.5s
133-
}
134-
}
135-
136-
// defaultStreamProcessingDurationBuckets returns the default histogram buckets for redis.client.stream.processing_duration
137-
// Stream processing can take longer than regular operations
138-
func defaultStreamProcessingDurationBuckets() []float64 {
139-
return []float64{
140-
0.0001, // 0.1ms
141-
0.0005, // 0.5ms
142-
0.001, // 1ms
143-
0.005, // 5ms
144-
0.01, // 10ms
145-
0.05, // 50ms
146-
0.1, // 100ms
147-
0.5, // 500ms
148-
1.0, // 1s
149-
5.0, // 5s
150-
10.0, // 10s
151-
}
152-
}
153-
154-
// defaultConnectionCreateTimeBuckets returns the default histogram buckets for db.client.connection.create_time
155-
// Connection creation can take longer than regular operations
156-
func defaultConnectionCreateTimeBuckets() []float64 {
157-
return []float64{
158-
0.0001, // 0.1ms
159-
0.0005, // 0.5ms
160-
0.001, // 1ms
161-
0.005, // 5ms
162-
0.01, // 10ms
163-
0.05, // 50ms
164-
0.1, // 100ms
165-
0.5, // 500ms
166-
1.0, // 1s
167-
5.0, // 5s
168-
10.0, // 10s
169-
}
170-
}
171-
172-
// defaultConnectionWaitTimeBuckets returns the default histogram buckets for db.client.connection.wait_time
173-
// Time waiting for a connection from the pool
174-
func defaultConnectionWaitTimeBuckets() []float64 {
175-
return []float64{
176-
0.0001, // 0.1ms
177-
0.0005, // 0.5ms
178-
0.001, // 1ms
179-
0.005, // 5ms
180-
0.01, // 10ms
181-
0.05, // 50ms
182-
0.1, // 100ms
183-
0.5, // 500ms
184-
1.0, // 1s
185-
5.0, // 5s
186-
10.0, // 10s
187-
}
188-
}
189-
190-
// defaultConnectionUseTimeBuckets returns the default histogram buckets for db.client.connection.use_time
191-
// Time a connection is in use (checked out from pool)
192-
func defaultConnectionUseTimeBuckets() []float64 {
106+
// - Seconds: 1s, 5s, 10s
107+
//
108+
// This covers the range from 0.1ms to 10s, which is suitable for:
109+
// - db.client.operation.duration (command execution time)
110+
// - db.client.connection.create_time (connection establishment)
111+
// - db.client.connection.wait_time (waiting for connection from pool)
112+
// - db.client.connection.use_time (time connection is checked out)
113+
// - redis.client.stream.processing_duration (stream message processing)
114+
func defaultHistogramBuckets() []float64 {
193115
return []float64{
194116
0.0001, // 0.1ms
195117
0.0005, // 0.5ms
@@ -233,7 +155,7 @@ func WithEnabled(enabled bool) Option {
233155
}
234156

235157
// WithEnabledMetricGroups sets which metric groups to register
236-
// Default: ["command", "connection-basic", "resiliency"]
158+
// Default: ["connection-basic", "resiliency"]
237159
func WithEnabledMetricGroups(groups []MetricGroup) Option {
238160
return optionFunc(func(c *config) {
239161
c.enabledMetricGroups = make(map[MetricGroup]bool)
@@ -288,37 +210,28 @@ func WithHistogramAggregation(agg HistogramAggregation) Option {
288210
})
289211
}
290212

291-
// WithBucketsOperationDuration sets explicit buckets (seconds) for db.client.operation.duration
292-
func WithBucketsOperationDuration(buckets []float64) Option {
213+
// WithHistogramBuckets sets custom histogram buckets for ALL duration metrics.
214+
// If not set, uses defaultHistogramBuckets() which covers 0.1ms to 10s.
215+
// Buckets should be in seconds (e.g., 0.001 = 1ms, 0.1 = 100ms, 1.0 = 1s).
216+
//
217+
// This applies to all duration histograms:
218+
// - db.client.operation.duration
219+
// - db.client.connection.create_time
220+
// - db.client.connection.wait_time
221+
// - db.client.connection.use_time
222+
// - redis.client.stream.processing_duration
223+
//
224+
// Example:
225+
//
226+
// redisotel.Init(rdb,
227+
// redisotel.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 1.0}),
228+
// )
229+
func WithHistogramBuckets(buckets []float64) Option {
293230
return optionFunc(func(c *config) {
294231
c.bucketsOperationDuration = buckets
295-
})
296-
}
297-
298-
// WithBucketsStreamProcessingDuration sets explicit buckets (seconds) for redis.client.stream.processing_duration
299-
func WithBucketsStreamProcessingDuration(buckets []float64) Option {
300-
return optionFunc(func(c *config) {
301232
c.bucketsStreamProcessingDuration = buckets
302-
})
303-
}
304-
305-
// WithBucketsConnectionCreateTime sets buckets for db.client.connection.create_time
306-
func WithBucketsConnectionCreateTime(buckets []float64) Option {
307-
return optionFunc(func(c *config) {
308233
c.bucketsConnectionCreateTime = buckets
309-
})
310-
}
311-
312-
// WithBucketsConnectionWaitTime sets buckets for db.client.connection.wait_time
313-
func WithBucketsConnectionWaitTime(buckets []float64) Option {
314-
return optionFunc(func(c *config) {
315234
c.bucketsConnectionWaitTime = buckets
316-
})
317-
}
318-
319-
// WithBucketsConnectionUseTime sets buckets for db.client.connection.use_time
320-
func WithBucketsConnectionUseTime(buckets []float64) Option {
321-
return optionFunc(func(c *config) {
322235
c.bucketsConnectionUseTime = buckets
323236
})
324237
}

0 commit comments

Comments
 (0)