Skip to content

Commit 120cdd0

Browse files
committed
Add support for custom attributes function
1 parent 731258f commit 120cdd0

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
@@ -180,6 +180,7 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
180180
createTime: createTime,
181181
useTime: useTime,
182182
attrs: conf.attrs,
183+
attrsFunc: conf.attrsFunc,
183184
})
184185
return nil
185186
}
@@ -188,6 +189,7 @@ type metricsHook struct {
188189
createTime metric.Float64Histogram
189190
useTime metric.Float64Histogram
190191
attrs []attribute.KeyValue
192+
attrsFunc func(context.Context) []attribute.KeyValue
191193
}
192194

193195
var _ redis.Hook = (*metricsHook)(nil)
@@ -219,6 +221,7 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
219221

220222
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
221223
attrs = append(attrs, mh.attrs...)
224+
attrs = append(attrs, mh.attrsFunc(ctx)...)
222225
attrs = append(attrs, attribute.String("type", "command"))
223226
attrs = append(attrs, statusAttr(err))
224227
attrs = append(attrs, semconv.DBOperationName(cmd.FullName()))
@@ -240,6 +243,7 @@ func (mh *metricsHook) ProcessPipelineHook(
240243

241244
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
242245
attrs = append(attrs, mh.attrs...)
246+
attrs = append(attrs, mh.attrsFunc(ctx)...)
243247
attrs = append(attrs, attribute.String("type", "pipeline"))
244248
attrs = append(attrs, statusAttr(err))
245249
if len(cmds) > 0 {

0 commit comments

Comments
 (0)