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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add max cmd bytes for otel hook #2929

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions extra/redisotel/config.go
Expand Up @@ -19,7 +19,8 @@ type config struct {
tp trace.TracerProvider
tracer trace.Tracer

dbStmtEnabled bool
dbStmtEnabled bool
maxCommandBytes int

// Metrics options.

Expand Down Expand Up @@ -54,9 +55,10 @@ func newConfig(opts ...baseOption) *config {
dbSystem: "redis",
attrs: []attribute.KeyValue{},

tp: otel.GetTracerProvider(),
mp: otel.GetMeterProvider(),
dbStmtEnabled: true,
tp: otel.GetTracerProvider(),
mp: otel.GetMeterProvider(),
dbStmtEnabled: true,
maxCommandBytes: 0,
}

for _, opt := range opts {
Expand Down Expand Up @@ -113,6 +115,14 @@ func WithDBStatement(on bool) TracingOption {
})
}

// WithMaxCommandBytes limit the bytes of raw redis commands.
// Default is 0, don't limit.
func WithMaxCommandBytes(size int) TracingOption {
return tracingOption(func(conf *config) {
conf.maxCommandBytes = size
})
}

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

type MetricsOption interface {
Expand Down
19 changes: 19 additions & 0 deletions extra/redisotel/redisotel_test.go
Expand Up @@ -2,6 +2,7 @@ package redisotel

import (
"context"
"strings"
"testing"

semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
Expand Down Expand Up @@ -59,3 +60,21 @@ func TestWithDBStatement(t *testing.T) {
t.Fatal(err)
}
}

func TestMaxString(t *testing.T) {
src := strings.Repeat("a", 100)
dst := maxString(src, 50)
if dst != src[:50] {
t.Fatal("failed to maxstring")
}

dst = maxString(src, 200)
if dst != src {
t.Fatal("failed to maxstring")
}

dst = maxString(src, 0)
if dst != src {
t.Fatal("failed to maxstring")
}
}
18 changes: 18 additions & 0 deletions extra/redisotel/tracing.go
Expand Up @@ -27,6 +27,7 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
connString := formatDBConnString(opt.Network, opt.Addr)
rdb.AddHook(newTracingHook(connString, opts...))
return nil

case *redis.ClusterClient:
rdb.AddHook(newTracingHook("", opts...))

Expand Down Expand Up @@ -120,6 +121,9 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {

if th.conf.dbStmtEnabled {
cmdString := rediscmd.CmdString(cmd)
if th.conf.maxCommandBytes > 0 {
cmdString = maxString(cmdString, th.conf.maxCommandBytes)
}
attrs = append(attrs, semconv.DBStatementKey.String(cmdString))
}

Expand Down Expand Up @@ -157,6 +161,9 @@ func (th *tracingHook) ProcessPipelineHook(

summary, cmdsString := rediscmd.CmdsString(cmds)
if th.conf.dbStmtEnabled {
if th.conf.maxCommandBytes > 0 {
cmdsString = maxString(cmdsString, th.conf.maxCommandBytes)
}
attrs = append(attrs, semconv.DBStatementKey.String(cmdsString))
}

Expand Down Expand Up @@ -213,3 +220,14 @@ func funcFileLine(pkg string) (string, string, int) {

return fn, file, line
}

func maxString(s string, length int) string {
if length <= 0 { // no define or no limit
return s
}

if len(s) > length {
return s[:length]
}
return s
}