Skip to content

Commit 64bfb34

Browse files
committed
Add support for custom attributes function
1 parent e63669e commit 64bfb34

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

extra/redisotel/config.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package redisotel
22

33
import (
4+
"context"
5+
46
"go.opentelemetry.io/otel"
57
"go.opentelemetry.io/otel/attribute"
68
"go.opentelemetry.io/otel/metric"
@@ -11,8 +13,9 @@ import (
1113
type config struct {
1214
// Common options.
1315

14-
dbSystem string
15-
attrs []attribute.KeyValue
16+
dbSystem string
17+
attrs []attribute.KeyValue
18+
attrsFunc func(context.Context) []attribute.KeyValue
1619

1720
// Tracing options.
1821

@@ -51,8 +54,9 @@ func (fn option) metrics() {}
5154

5255
func newConfig(opts ...baseOption) *config {
5356
conf := &config{
54-
dbSystem: "redis",
55-
attrs: []attribute.KeyValue{},
57+
dbSystem: "redis",
58+
attrs: []attribute.KeyValue{},
59+
attrsFunc: func(ctx context.Context) []attribute.KeyValue { return []attribute.KeyValue{} },
5660

5761
tp: otel.GetTracerProvider(),
5862
mp: otel.GetMeterProvider(),
@@ -81,6 +85,14 @@ func WithAttributes(attrs ...attribute.KeyValue) Option {
8185
})
8286
}
8387

88+
// WithAttributesFunc takes a function that returns additional attributes to be added using the context.
89+
// This is executed only in ProcessPipelineHook and ProcessHook
90+
func WithAttributesFunc(f func(context.Context) []attribute.KeyValue) Option {
91+
return option(func(conf *config) {
92+
conf.attrsFunc = f
93+
})
94+
}
95+
8496
//------------------------------------------------------------------------------
8597

8698
type TracingOption interface {

extra/redisotel/metrics.go

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
175175
createTime: createTime,
176176
useTime: useTime,
177177
attrs: conf.attrs,
178+
attrsFunc: conf.attrsFunc,
178179
})
179180
return nil
180181
}
@@ -183,6 +184,7 @@ type metricsHook struct {
183184
createTime metric.Float64Histogram
184185
useTime metric.Float64Histogram
185186
attrs []attribute.KeyValue
187+
attrsFunc func(context.Context) []attribute.KeyValue
186188
}
187189

188190
var _ redis.Hook = (*metricsHook)(nil)
@@ -214,6 +216,7 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
214216

215217
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
216218
attrs = append(attrs, mh.attrs...)
219+
attrs = append(attrs, mh.attrsFunc(ctx)...)
217220
attrs = append(attrs, attribute.String("type", "command"))
218221
attrs = append(attrs, statusAttr(err))
219222

@@ -235,6 +238,7 @@ func (mh *metricsHook) ProcessPipelineHook(
235238

236239
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
237240
attrs = append(attrs, mh.attrs...)
241+
attrs = append(attrs, mh.attrsFunc(ctx)...)
238242
attrs = append(attrs, attribute.String("type", "pipeline"))
239243
attrs = append(attrs, statusAttr(err))
240244

0 commit comments

Comments
 (0)