Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom attributes function #3198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading