redis

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2018 License: BSD-3-Clause Imports: 7 Imported by: 0

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

func String

func String(cmds []redis.Cmder) string

String returns a string representation of a slice of redis Commands, separated by newlines

Types

type TracedClient

type TracedClient struct {
	*redis.Client
	// contains filtered or unexported fields
}

TracedClient is used to trace requests to a redis server.

func NewTracedClient

func NewTracedClient(opt *redis.Options, t *tracer.Tracer, service string) *TracedClient

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

type TracedPipeliner struct {
	redis.Pipeliner
	// contains filtered or unexported fields
}

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

func (c *TracedPipeliner) ExecWithContext(ctx context.Context) ([]redis.Cmder, error)

ExecWithContext calls Pipeline.Exec(). It ensures that the resulting Redis calls are traced, and that emitted spans are children of the given Context

Jump to

Keyboard shortcuts

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