redisbp

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2021 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package redisbp provides Baseplate integrations for go-redis.

See https://pkg.go.dev/github.com/go-redis/redis/v7 for documentation for go-redis

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func OptionsMust added in v0.2.0

func OptionsMust(options *redis.Options, err error) *redis.Options

OptionsMust can be combine with ClientOptions.Options() to either return the *redis.Options object or panic if an error was returned. This allows you to just pass this into redis.NewClient.

Ex:

var opts redisbp.ClientOptions
client := redis.NewClient(redisbp.OptionsMust(opts.Options()))

Types

type ClientConfig added in v0.2.0

type ClientConfig struct {
	// URL is passed to redis.ParseURL to initialize the client options.  This is
	// a required field.
	//
	// https://pkg.go.dev/github.com/go-redis/redis/v7?tab=doc#ParseURL
	URL string `yaml:"url"`

	Pool     PoolOptions    `yaml:"pool"`
	Retries  RetryOptions   `yaml:"retries"`
	Timeouts TimeoutOptions `yaml:"timeouts"`
}

ClientConfig can be used to configure a redis-go "Client". See the docs for redis.Options in redis-go for details on what each value means and what its defaults are: https://pkg.go.dev/github.com/go-redis/redis/v7?tab=doc#Options

Can be deserialized from YAML.

Examples:

Minimal YAML:

redis:
 url: redis://localhost:6379

Full YAML:

redis:
 url: redis://localhost:6379
 pool:
  size: 10
  minIdleConnections: 5
  maxConnectionAge: 1m
  timeout: 10s
 retries:
  max: 2
  minBackoff: 1ms
  maxBackoff: 10ms
 timeouts:
  dial: 1s
  read: 100ms
  write: 200ms
Example

This example shows how you can embed a redis config in a struct and parse that with `baseplate.New`.

package main

import (
	"context"

	"github.com/go-redis/redis/v7"
	"github.com/reddit/baseplate.go"
	"github.com/reddit/baseplate.go/redisbp"
)

type config struct {
	Redis redisbp.ClientConfig `yaml:"redis"`
}

// This example shows how you can embed a redis config in a struct and parse
// that with `baseplate.New`.
func main() {
	var cfg config
	ctx, bp, err := baseplate.New(context.Background(), "example.yaml", &cfg)
	if err != nil {
		panic(err)
	}
	defer bp.Close()

	factory := redisbp.NewMonitoredClientFactory(
		"redis",
		redis.NewClient(redisbp.OptionsMust(cfg.Redis.Options())),
	)
	client := factory.BuildClient(ctx)
	client.Ping()
}

func (ClientConfig) Options added in v0.2.0

func (cfg ClientConfig) Options() (*redis.Options, error)

Options returns a redis.Options populated using the values from cfg.

type ClusterConfig added in v0.2.0

type ClusterConfig struct {
	// Addrs is the seed list of cluster nodes in the format "host:port". This is
	// a required field.
	//
	// Maps to Addrs on redis.ClusterClient.
	Addrs []string `yaml:"addrs"`

	Pool     PoolOptions    `yaml:"pool"`
	Retries  RetryOptions   `yaml:"retries"`
	Timeouts TimeoutOptions `yaml:"timeouts"`
}

ClusterConfig can be used to configure a redis-go "ClusterClient". See the docs for redis.ClusterOptions in redis-go for details on what each value means and what its defaults are: https://pkg.go.dev/github.com/go-redis/redis/v7?tab=doc#ClusterOptions

Can be deserialized from YAML.

Examples:

Minimal YAML:

redis:
 addrs:
  - localhost:6379
  - localhost:6380

Full YAML:

redis:
 addrs:
  - localhost:6379
  - localhost:6380
 pool:
  size: 10
  minIdleConnections: 5
  maxConnectionAge: 1m
  timeout: 10s
 retries:
  max: 2
  minBackoff: 1ms
  maxBackoff: 10ms
 timeouts:
  dial: 1s
  read: 100ms
  write: 200ms

func (ClusterConfig) Options added in v0.2.0

func (cfg ClusterConfig) Options() *redis.ClusterOptions

Options returns a redis.ClusterOptions populated using the values from cfg.

type MonitoredCmdable

type MonitoredCmdable interface {
	// Close should generally not be called directly on a MonitoredCmdable, since
	// they are meant to be shared and long lived.  It will be called by
	// MonitoredCmdableFactory.Close which should be called when a server is shut
	// down.
	io.Closer
	redis.Cmdable

	// PoolStats returns the stats of the underlying connection pool.
	PoolStats() *redis.PoolStats

	// AddHook adds a hook onto the object.
	//
	// Note most redis.Cmdable objects already implement this but it is not a
	// part of the redis.Cmdable interface.
	AddHook(hook redis.Hook)

	// WithMonitoredContext returns a clone of the MonitoredCmdable with its
	// context set to the provided one.
	//
	// This is similar to the WithContext method that many redis.Cmdable objects
	// implement, but this returns a MonitoredCmdable rather than a pointer to
	// the exact type.  Also note that WithContext is not a part of the
	// redis.Cmdable interface.
	WithMonitoredContext(ctx context.Context) MonitoredCmdable
}

MonitoredCmdable wraps the redis.Cmdable interface and adds additional methods to support integrating with Baseplate.go SpanHooks.

type MonitoredCmdableFactory

type MonitoredCmdableFactory struct {
	// contains filtered or unexported fields
}

MonitoredCmdableFactory is used to create Redis clients that are monitored by a SpanHook.

This is intended for use by a top-level Service interface where you use it on each new request to build a monitored client to inject into the actual request handler.

A MonitoredCmdableFactory should be created using one of the New methods provided in this package.

Example

This example demonstrates how to use a MonitoredCmdableFactory.

package main

import (
	"context"

	"github.com/go-redis/redis/v7"
	opentracing "github.com/opentracing/opentracing-go"
	"github.com/reddit/baseplate.go/metricsbp"
	"github.com/reddit/baseplate.go/redisbp"
	"github.com/reddit/baseplate.go/tracing"
)

// Service is an example go-kit service to help demonstrate how to use
// redisbp.type MonitoredCmdableFactory in a service.
type Service struct {
	RedisFactory redisbp.MonitoredCmdableFactory
}

// Endpoint is an example endpoint that will use redis.
func (s Service) Endpoint(ctx context.Context) error {
	// Use the factory to create a new, monitored redis client that can be
	// injected into your endpoint handler
	redis := s.RedisFactory.BuildClient(ctx)
	return EndpointHandler(ctx, redis)
}

// EndpointHandler is the actual handler function for
// Service.Endpoint.
func EndpointHandler(_ context.Context, client redisbp.MonitoredCmdable) error {
	// Any calls using the injected Redis client will be monitored using Spans.
	client.Ping()
	return nil
}

// This example demonstrates how to use a MonitoredCmdableFactory.
func main() {
	// Create a factory for basic, monitored redis.Client objects.
	clientFactory := redisbp.NewMonitoredClientFactory(
		"redis",
		redis.NewClient(&redis.Options{Addr: ":6379"}),
	)
	// Create a factory for monitored redis.Client objects that implements
	// failover with Redis Sentinel.
	failoverFactory := redisbp.NewMonitoredClientFactory(
		"redis",
		redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    "master",
			SentinelAddrs: []string{":6379"},
		}),
	)
	// Create a factory for monitored redis.ClusterClient objects.
	clusterFactory := redisbp.NewMonitoredClusterFactory(
		"redis",
		redis.NewClusterClient(&redis.ClusterOptions{
			Addrs: []string{":7000", ":7001", ":7002"},
		}),
	)
	defer func() {
		clientFactory.Close()
		failoverFactory.Close()
		clusterFactory.Close()
	}()

	go clientFactory.MonitorPoolStats(metricsbp.M.Ctx(), metricsbp.Tags{"env": "test"})

	// Create a "service" with a monitored client factory.
	svc := Service{RedisFactory: clientFactory}
	// Create a server span and attach it to a context object.
	//
	// In production, this will be handled by service middleware rather than
	// being called manually.
	_, ctx := opentracing.StartSpanFromContext(
		context.Background(),
		"test",
		tracing.SpanTypeOption{Type: tracing.SpanTypeServer},
	)
	// Calls to this endpoint will use the factory to create a new client and
	// inject it into the actual implementation
	//
	// In production, the service framework will call these endpoints in
	// response to requests from clients rather than you calling it manually.
	svc.Endpoint(ctx)
}

func NewMonitoredClientFactory

func NewMonitoredClientFactory(name string, client *redis.Client) MonitoredCmdableFactory

NewMonitoredClientFactory creates a MonitoredCmdableFactory for a redis.Client object.

This may connect to a single redis instance, or be a failover client using Redis Sentinel.

func NewMonitoredClusterFactory

func NewMonitoredClusterFactory(name string, client *redis.ClusterClient) MonitoredCmdableFactory

NewMonitoredClusterFactory creates a MonitoredCmdableFactory for a redis.ClusterClient object.

func (MonitoredCmdableFactory) BuildClient

BuildClient returns a new MonitoredCmdable with its context set to the provided one.

func (MonitoredCmdableFactory) Close added in v0.2.0

func (f MonitoredCmdableFactory) Close() error

Close closes the underlying MonitoredCmdable, which will close the underlying connection pool, closing out any clients created with the factory.

func (MonitoredCmdableFactory) MonitorPoolStats added in v0.5.1

func (f MonitoredCmdableFactory) MonitorPoolStats(ctx context.Context, tags metricsbp.Tags)

MonitorPoolStats publishes stats for the underlying Redis client pool at the rate defined by metricsbp.SysStatsTickerInterval using metricsbp.M.

It is recommended that you call this in a separate goroutine as it will run until it is stopped. It will stop when the given context is Done()

Ex:

go factory.MonitorPoolStats(metricsbp.M.Ctx(), tags)

type PoolOptions added in v0.2.0

type PoolOptions struct {
	// Maps to PoolSize on the redis-go options.
	Size int `yaml:"size"`

	// Maps to MinIdleConnections on the redis-go options.
	MinIdleConnections int `yaml:"minIdleConnections"`

	// Maps to MaxConnAge on the redis-go options.
	MaxConnectionAge time.Duration `yaml:"maxConnectionAge"`

	// Maps to PoolTimeout on the redis-go options.
	Timeout time.Duration `yaml:"timeout"`
}

PoolOptions is used to configure the pool attributes of a redis-go Client or ClusterClient. If any value is not set, it will use whatever default is defined by redis-go.

See https://pkg.go.dev/github.com/go-redis/redis/v7?tab=doc#Options for details on the specific fields.

Can be deserialized from YAML.

func (PoolOptions) ApplyClusterOptions added in v0.2.0

func (opts PoolOptions) ApplyClusterOptions(options *redis.ClusterOptions)

ApplyClusterOptions applies the PoolOptions to the redis.ClusterOptions.

func (PoolOptions) ApplyOptions added in v0.2.0

func (opts PoolOptions) ApplyOptions(options *redis.Options)

ApplyOptions applies the PoolOptions to the redis.Options.

type RetryOptions added in v0.2.0

type RetryOptions struct {
	// Maps to MaxRetries on the redis-go options.
	Max int `yaml:"max"`

	// Maps to MinRetryBackoff on the redis-go options.
	MinBackoff time.Duration `yaml:"minBackoff"`

	// Maps to MaxRetryBackoff on the redis-go options.
	MaxBackoff time.Duration `yaml:"maxBackoff"`
}

RetryOptions is used to configure the retry behavior of a redis-go Client or ClusterClient.

See https://pkg.go.dev/github.com/go-redis/redis/v7?tab=doc#Options for details on the specific fields.

Can be deserialized from YAML.

func (RetryOptions) ApplyClusterOptions added in v0.2.0

func (opts RetryOptions) ApplyClusterOptions(options *redis.ClusterOptions)

ApplyClusterOptions applies the RetryOptions to the redis.ClusterOptions.

func (RetryOptions) ApplyOptions added in v0.2.0

func (opts RetryOptions) ApplyOptions(options *redis.Options)

ApplyOptions applies the RetryOptions to the redis.Options.

type SpanHook

type SpanHook struct {
	ClientName string
}

SpanHook is a redis.Hook for wrapping Redis commands and pipelines in Client Spans and metrics.

Example

This example demonstrates how to use SpanHook to automatically add Spans around Redis commands using go-redis

Baseplate.go also provides the MonitoredCmdableFactory object that you can use to create Redis clients with a SpanHook already attached.

// variables should be properly initialized in production code
var (
	// baseClient is not actually used to run commands, we register the Hook
	// to it and use it to create clients for each Server Span.
	baseClient redis.Client
)
// Add the Hook onto baseClient
baseClient.AddHook(redisbp.SpanHook{ClientName: "redis"})
// Create a server span and attach it into a context object
_, ctx := opentracing.StartSpanFromContext(
	context.Background(),
	"test",
	tracing.SpanTypeOption{Type: tracing.SpanTypeServer},
)
// Create a new client using the context for the server span
client := baseClient.WithContext(ctx)
// Commands should now be wrapped using Client Spans
client.Ping()

func (SpanHook) AfterProcess

func (h SpanHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error

AfterProcess ends the client Span started by BeforeProcess, publishes the time the Redis command took to complete, and a metric indicating whether the command was a "success" or "fail"

func (SpanHook) AfterProcessPipeline

func (h SpanHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error

AfterProcessPipeline ends the client span started by BeforeProcessPipeline, publishes the time the Redis pipeline took to complete, and a metric indicating whether the pipeline was a "success" or "fail"

func (SpanHook) BeforeProcess

func (h SpanHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error)

BeforeProcess starts a client Span before processing a Redis command and starts a timer to record how long the command took.

func (SpanHook) BeforeProcessPipeline

func (h SpanHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error)

BeforeProcessPipeline starts a client span before processing a Redis pipeline and starts a timer to record how long the pipeline took.

type TimeoutOptions added in v0.2.0

type TimeoutOptions struct {
	Dial  time.Duration `yaml:"dial"`
	Read  time.Duration `yaml:"read"`
	Write time.Duration `yaml:"write"`
}

TimeoutOptions is used to configure the timeout behavior of a redis-go Client or ClusterClient.

See https://pkg.go.dev/github.com/go-redis/redis/v7?tab=doc#Options for details on the specific fields.

Can be deserialized from YAML.

func (TimeoutOptions) ApplyClusterOptions added in v0.2.0

func (opts TimeoutOptions) ApplyClusterOptions(options *redis.ClusterOptions)

ApplyClusterOptions applies the TimeoutOptions to the redis.ClusterOptions.

func (TimeoutOptions) ApplyOptions added in v0.2.0

func (opts TimeoutOptions) ApplyOptions(options *redis.Options)

ApplyOptions applies the TimeoutOptions to the redis.Options.

Jump to

Keyboard shortcuts

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