Skip to content

Commit

Permalink
Support valkey-go tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
keisku committed Jan 15, 2025
1 parent 471d723 commit 00e39a8
Show file tree
Hide file tree
Showing 10 changed files with 834 additions and 5 deletions.
40 changes: 40 additions & 0 deletions contrib/valkey-go/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

package valkey_test

import (
"context"
"log/slog"

"github.com/valkey-io/valkey-go"
valkeytrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/valkey-go"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

// To start tracing Valkey, simply create a new client using the library and continue
// using as you normally would.
func Example() {
vk, err := valkeytrace.NewClient(valkey.ClientOption{
InitAddress: []string{"localhost:6379"},
})
if err != nil {
slog.Error(err.Error())
return
}

span, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request",
tracer.SpanType(ext.SpanTypeValkey),
tracer.ServiceName("web"),
tracer.ResourceName("/home"),
)

if err := vk.Do(ctx, vk.B().Set().Key("key").Value("value").Build()).Error(); err != nil {
slog.ErrorContext(ctx, "set a value", slog.Any("error", err))
}

span.Finish()
}
83 changes: 83 additions & 0 deletions contrib/valkey-go/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

// Package redis provides tracing functions for tracing the go-redis/redis package (https://github.com/go-redis/redis).
// This package supports versions up to go-redis 6.15.
package valkey

import (
"math"
"os"

"gopkg.in/DataDog/dd-trace-go.v1/internal"
"gopkg.in/DataDog/dd-trace-go.v1/internal/namingschema"
)

const defaultServiceName = "valkey.client"

type clientConfig struct {
serviceName string
spanName string
analyticsRate float64
skipRaw bool
}

// ClientOption represents an option that can be used to create or wrap a client.
type ClientOption func(*clientConfig)

func defaults(cfg *clientConfig) {
cfg.serviceName = namingschema.ServiceNameOverrideV0(defaultServiceName, defaultServiceName)
cfg.spanName = namingschema.OpName(namingschema.ValkeyOutbound)
if internal.BoolEnv("DD_TRACE_VALKEY_ANALYTICS_ENABLED", false) {
cfg.analyticsRate = 1.0
} else {
cfg.analyticsRate = math.NaN()
}
if v := os.Getenv("DD_TRACE_VALKEY_SERVICE_NAME"); v == "" {
cfg.serviceName = defaultServiceName
} else {
cfg.serviceName = v
}
cfg.skipRaw = internal.BoolEnv("DD_TRACE_VALKEY_SKIP_RAW_COMMAND", false)
}

// WithSkipRawCommand reports whether to skip setting the raw command value
// on instrumenation spans. This may be useful if the Datadog Agent is not
// set up to obfuscate this value and it could contain sensitive information.
func WithSkipRawCommand(skip bool) ClientOption {
return func(cfg *clientConfig) {
cfg.skipRaw = skip
}
}

// WithServiceName sets the given service name for the client.
func WithServiceName(name string) ClientOption {
return func(cfg *clientConfig) {
cfg.serviceName = name
}
}

// WithAnalytics enables Trace Analytics for all started spans.
func WithAnalytics(on bool) ClientOption {
return func(cfg *clientConfig) {
if on {
cfg.analyticsRate = 1.0
} else {
cfg.analyticsRate = math.NaN()
}
}
}

// WithAnalyticsRate sets the sampling rate for Trace Analytics events
// correlated to started spans.
func WithAnalyticsRate(rate float64) ClientOption {
return func(cfg *clientConfig) {
if rate >= 0.0 && rate <= 1.0 {
cfg.analyticsRate = rate
} else {
cfg.analyticsRate = math.NaN()
}
}
}
Loading

0 comments on commit 00e39a8

Please sign in to comment.