Documentation ¶
Overview ¶
Package redis provides tracing for the go-redis Redis client (https://github.com/go-redis/redis)
Example ¶
To start tracing Redis commands, use the NewTracedClient function to create a traced Redis clienty, passing in a service name of choice.
package main import ( "context" "fmt" redistrace "github.com/DataDog/dd-trace-go/contrib/go-redis/redis" "github.com/DataDog/dd-trace-go/tracer" "github.com/DataDog/dd-trace-go/tracer/contrib/gin-gonic/gintrace" "github.com/gin-gonic/gin" "github.com/go-redis/redis" ) func main() { opts := &redis.Options{ Addr: "127.0.0.1:6379", Password: "", // no password set DB: 0, // use default db } c := redistrace.NewTracedClient(opts, tracer.DefaultTracer, "my-redis-backend") // Emit spans per command by using your Redis connection as usual c.Set("test_key", "test_value", 0) // Use a context to pass information down the call chain root := tracer.NewRootSpan("parent.request", "web", "/home") ctx := root.Context(context.Background()) // When set with a context, the traced client will emit a span inheriting from 'parent.request' c.SetContext(ctx) c.Set("food", "cheese", 0) root.Finish() // Contexts can be easily passed between Datadog integrations r := gin.Default() r.Use(gintrace.Middleware("web-admin")) client := redistrace.NewTracedClient(opts, tracer.DefaultTracer, "redis-img-backend") r.GET("/user/settings/:id", func(ctx *gin.Context) { // create a span that is a child of your http request client.SetContext(ctx) client.Get(fmt.Sprintf("cached_user_details_%s", ctx.Param("id"))) }) }
Output:
Example (Pipeline) ¶
You can also trace Redis Pipelines
package main import ( redistrace "github.com/DataDog/dd-trace-go/contrib/go-redis/redis" "github.com/DataDog/dd-trace-go/tracer" "github.com/go-redis/redis" "time" ) func main() { opts := &redis.Options{ Addr: "127.0.0.1:6379", Password: "", // no password set DB: 0, // use default db } c := redistrace.NewTracedClient(opts, tracer.DefaultTracer, "my-redis-backend") // pipe is a TracedPipeliner pipe := c.Pipeline() pipe.Incr("pipeline_counter") pipe.Expire("pipeline_counter", time.Hour) pipe.Exec() }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type TracedClient ¶
TracedClient is used to trace requests to a redis server.
func NewTracedClient ¶
NewTracedClient takes a Client returned by redis.NewClient and configures it to emit spans under the given service name
Example ¶
package main import ( "context" redistrace "github.com/DataDog/dd-trace-go/contrib/go-redis/redis" "github.com/DataDog/dd-trace-go/tracer" "github.com/go-redis/redis" ) func main() { opts := &redis.Options{ Addr: "127.0.0.1:6379", Password: "", // no password set DB: 0, // use default db } c := redistrace.NewTracedClient(opts, tracer.DefaultTracer, "my-redis-backend") // Emit spans per command by using your Redis connection as usual c.Set("test_key", "test_value", 0) // Use a context to pass information down the call chain root := tracer.NewRootSpan("parent.request", "web", "/home") ctx := root.Context(context.Background()) // When set with a context, the traced client will emit a span inheriting from 'parent.request' c.SetContext(ctx) c.Set("food", "cheese", 0) root.Finish() }
Output:
func (*TracedClient) Pipeline ¶
func (c *TracedClient) Pipeline() *TracedPipeliner
Pipeline creates a TracedPipeline from a TracedClient
func (*TracedClient) SetContext ¶
func (c *TracedClient) SetContext(ctx context.Context)
SetContext sets a context on a TracedClient. Use it to ensure that emitted spans have the correct parent
type TracedPipeliner ¶
TracedPipeline is used to trace pipelines executed on a redis server.
func (*TracedPipeliner) Exec ¶
func (c *TracedPipeliner) Exec() ([]redis.Cmder, error)
Exec calls Pipeline.Exec() ensuring that the resulting Redis calls are traced
func (*TracedPipeliner) ExecWithContext ¶
ExecWithContext calls Pipeline.Exec(). It ensures that the resulting Redis calls are traced, and that emitted spans are children of the given Context