redigo

package
v1.42.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package redigo provides functions to trace the garyburd/redigo package (https://github.com/garyburd/redigo).

Example

To start tracing Redis commands, use the TracedDial function to create a connection, passing in a service name of choice.

package main

import (
	"context"
	"log"

	redigotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/garyburd/redigo"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
	c, err := redigotrace.Dial("tcp", "127.0.0.1:6379")
	if err != nil {
		log.Fatal(err)
	}

	// Emit spans per command by using your Redis connection as usual
	c.Do("SET", "vehicle", "truck")

	// Use a context to pass information down the call chain
	root, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request",
		tracer.ServiceName("web"),
		tracer.ResourceName("/home"),
	)

	// When passed a context as the final argument, c.Do will emit a span inheriting from 'parent.request'
	c.Do("SET", "food", "cheese", ctx)
	root.Finish()
}
Output:

Example (DialURL)

Alternatively, provide a redis URL to the TracedDialURL function

package main

import (
	"log"

	redigotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/garyburd/redigo"
)

func main() {
	c, err := redigotrace.DialURL("redis://127.0.0.1:6379")
	if err != nil {
		log.Fatal(err)
	}
	c.Do("SET", "vehicle", "truck")
}
Output:

Example (Pool)

When using a redigo Pool, set your Dial function to return a traced connection

package main

import (
	redigotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/garyburd/redigo"

	"github.com/garyburd/redigo/redis"
)

func main() {
	pool := &redis.Pool{
		Dial: func() (redis.Conn, error) {
			return redigotrace.Dial("tcp", "127.0.0.1:6379",
				redigotrace.WithServiceName("my-redis-backend"),
			)
		},
	}

	c := pool.Get()
	c.Do("SET", " whiskey", " glass")
}
Output:

Example (TracedConn)
package main

import (
	"context"
	"log"
	"time"

	redigotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/garyburd/redigo"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

	"github.com/garyburd/redigo/redis"
)

func main() {
	c, err := redigotrace.Dial("tcp", "127.0.0.1:6379",
		redigotrace.WithServiceName("my-redis-backend"),
		redis.DialKeepAlive(time.Minute),
	)
	if err != nil {
		log.Fatal(err)
	}

	// Emit spans per command by using your Redis connection as usual
	c.Do("SET", "vehicle", "truck")

	// Use a context to pass information down the call chain
	root, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request",
		tracer.ServiceName("web"),
		tracer.ResourceName("/home"),
	)

	// When passed a context as the final argument, c.Do will emit a span inheriting from 'parent.request'
	c.Do("SET", "food", "cheese", ctx)
	root.Finish()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(network, address string, options ...interface{}) (redis.Conn, error)

Dial dials into the network address and returns a traced redis.Conn. The set of supported options must be either of type redis.DialOption or this package's DialOption.

func DialURL

func DialURL(rawurl string, options ...interface{}) (redis.Conn, error)

DialURL connects to a Redis server at the given URL using the Redis URI scheme. URLs should follow the draft IANA specification for the scheme (https://www.iana.org/assignments/uri-schemes/prov/redis). The returned redis.Conn is traced.

Types

type Conn

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

Conn is an implementation of the redis.Conn interface that supports tracing

func (Conn) Do

func (tc Conn) Do(commandName string, args ...interface{}) (reply interface{}, err error)

Do wraps redis.Conn.Do. It sends a command to the Redis server and returns the received reply. In the process it emits a span containing key information about the command sent. When passed a context.Context as the final argument, Do will ensure that any span created inherits from this context. The rest of the arguments are passed through to the Redis server unchanged.

type DialOption

type DialOption func(*dialConfig)

DialOption represents an option that can be passed to Dial.

func WithAnalytics added in v1.11.0

func WithAnalytics(on bool) DialOption

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate added in v1.11.0

func WithAnalyticsRate(rate float64) DialOption

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

func WithServiceName

func WithServiceName(name string) DialOption

WithServiceName sets the given service name for the dialled connection.

Jump to

Keyboard shortcuts

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