Skip to content

Commit

Permalink
Add support for custom attributes function
Browse files Browse the repository at this point in the history
  • Loading branch information
urdarinda committed Nov 25, 2024
1 parent e63669e commit 64bfb34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 16 additions & 4 deletions extra/redisotel/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package redisotel

import (
"context"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
Expand All @@ -11,8 +13,9 @@ import (
type config struct {
// Common options.

dbSystem string
attrs []attribute.KeyValue
dbSystem string
attrs []attribute.KeyValue
attrsFunc func(context.Context) []attribute.KeyValue

// Tracing options.

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

func newConfig(opts ...baseOption) *config {
conf := &config{
dbSystem: "redis",
attrs: []attribute.KeyValue{},
dbSystem: "redis",
attrs: []attribute.KeyValue{},
attrsFunc: func(ctx context.Context) []attribute.KeyValue { return []attribute.KeyValue{} },

tp: otel.GetTracerProvider(),
mp: otel.GetMeterProvider(),
Expand Down Expand Up @@ -81,6 +85,14 @@ func WithAttributes(attrs ...attribute.KeyValue) Option {
})
}

// WithAttributesFunc takes a function that returns additional attributes to be added using the context.
// This is executed only in ProcessPipelineHook and ProcessHook
func WithAttributesFunc(f func(context.Context) []attribute.KeyValue) Option {
return option(func(conf *config) {
conf.attrsFunc = f
})
}

//------------------------------------------------------------------------------

type TracingOption interface {
Expand Down
4 changes: 4 additions & 0 deletions extra/redisotel/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
createTime: createTime,
useTime: useTime,
attrs: conf.attrs,
attrsFunc: conf.attrsFunc,
})
return nil
}
Expand All @@ -183,6 +184,7 @@ type metricsHook struct {
createTime metric.Float64Histogram
useTime metric.Float64Histogram
attrs []attribute.KeyValue
attrsFunc func(context.Context) []attribute.KeyValue
}

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

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

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

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

Expand Down

0 comments on commit 64bfb34

Please sign in to comment.