redis

package module
v2.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: Apache-2.0, BSD-3-Clause Imports: 11 Imported by: 1

Documentation

Overview

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.

Example

To start tracing Redis, simply create a new client using the library and continue using as you normally would.

package main

import (
	"context"

	redistrace "github.com/DataDog/dd-trace-go/contrib/go-redis/redis.v8/v2"

	"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/go-redis/redis/v8"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	ctx := context.Background()
	// create a new Client
	opts := &redis.Options{Addr: "127.0.0.1", Password: "", DB: 0}
	c := redistrace.NewClient(opts)

	// any action emits a span
	c.Set(ctx, "test_key", "test_value", 0)

	// optionally, create a new root span
	root, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request",
		tracer.SpanType(ext.SpanTypeRedis),
		tracer.ServiceName("web"),
		tracer.ResourceName("/home"),
	)

	// commit further commands, which will inherit from the parent in the context.
	c.Set(ctx, "food", "cheese", 0)
	root.Finish()
}
Example (Pipeliner)

You can also trace Redis Pipelines. Simply use as usual and the traces will be automatically picked up by the underlying implementation.

package main

import (
	"context"
	"time"

	redistrace "github.com/DataDog/dd-trace-go/contrib/go-redis/redis.v8/v2"

	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/go-redis/redis/v8"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	ctx := context.Background()
	// create a client
	opts := &redis.Options{Addr: "127.0.0.1", Password: "", DB: 0}
	c := redistrace.NewClient(opts, redistrace.WithService("my-redis-service"))

	// open the pipeline
	pipe := c.Pipeline()

	// submit some commands
	pipe.Incr(ctx, "pipeline_counter")
	pipe.Expire(ctx, "pipeline_counter", time.Hour)

	// execute with trace
	pipe.Exec(ctx)
}
Example (WrapClient)

You can create a traced ClusterClient using WrapClient

package main

import (
	"context"

	redistrace "github.com/DataDog/dd-trace-go/contrib/go-redis/redis.v8/v2"

	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/go-redis/redis/v8"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	c := redis.NewClusterClient(&redis.ClusterOptions{})
	redistrace.WrapClient(c)

	//Do something, passing in any relevant context
	c.Incr(context.TODO(), "my_counter")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(opt *redis.Options, opts ...ClientOption) redis.UniversalClient

NewClient returns a new Client that is traced with the default tracer under the service name "redis".

func WrapClient

func WrapClient(client redis.UniversalClient, opts ...ClientOption)

WrapClient adds a hook to the given client that traces with the default tracer under the service name "redis".

Types

type ClientOption

type ClientOption interface {
	// contains filtered or unexported methods
}

ClientOption describes options for the Redis integration.

type ClientOptionFn

type ClientOptionFn func(*clientConfig)

ClientOptionFn represents options applicable to NewClient and WrapClient.

func WithAnalytics

func WithAnalytics(on bool) ClientOptionFn

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) ClientOptionFn

WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.

func WithErrorCheck

func WithErrorCheck(fn func(err error) bool) ClientOptionFn

WithErrorCheck specifies a function fn which determines whether the passed error should be marked as an error.

func WithService

func WithService(name string) ClientOptionFn

WithService sets the given service name for the client.

func WithSkipRawCommand

func WithSkipRawCommand(skip bool) ClientOptionFn

WithSkipRawCommand reports whether to skip setting the "redis.raw_command" tag 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL