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 ¶
- type MonitoredCmdable
- type MonitoredCmdableFactory
- type SpanHook
- func (h SpanHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error
- func (h SpanHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error
- func (h SpanHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error)
- func (h SpanHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MonitoredCmdable ¶
type MonitoredCmdable interface { redis.Cmdable // 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/fizx/baseplate.go/redisbp" "github.com/fizx/baseplate.go/tracing" "github.com/go-redis/redis/v7" opentracing "github.com/opentracing/opentracing-go" ) // 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 service with a factory using a basic redis.Client svc := Service{ RedisFactory: redisbp.NewMonitoredClientFactory( "redis", redis.NewClient(&redis.Options{Addr: ":6379"}), ), } // 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) // Create service with a factory using a redis.Client that implements failover // with Redis Sentinel. svc = Service{ RedisFactory: redisbp.NewMonitoredClientFactory( "redis", redis.NewFailoverClient(&redis.FailoverOptions{ MasterName: "master", SentinelAddrs: []string{":6379"}, }), ), } _, ctx = opentracing.StartSpanFromContext( context.Background(), "test", tracing.SpanTypeOption{Type: tracing.SpanTypeServer}, ) svc.Endpoint(ctx) // Create service with a factory using a redis.ClusterClient to connect to // Redis Cluster. svc = Service{ RedisFactory: redisbp.NewMonitoredClusterFactory( "redis", redis.NewClusterClient(&redis.ClusterOptions{ Addrs: []string{":7000", ":7001", ":7002"}, }), ), } _, ctx = opentracing.StartSpanFromContext( context.Background(), "test", tracing.SpanTypeOption{Type: tracing.SpanTypeServer}, ) svc.Endpoint(ctx) // Create service with a factory using a redis.Ring client. svc = Service{ RedisFactory: redisbp.NewMonitoredRingFactory( "redis", redis.NewRing(&redis.RingOptions{ Addrs: map[string]string{ "shard0": ":7000", "shard1": ":7001", "shard2": ":7002", }, }), ), } _, ctx = opentracing.StartSpanFromContext( context.Background(), "test", tracing.SpanTypeOption{Type: tracing.SpanTypeServer}, ) svc.Endpoint(ctx) }
Output:
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 NewMonitoredRingFactory ¶
func NewMonitoredRingFactory(name string, client *redis.Ring) MonitoredCmdableFactory
NewMonitoredRingFactory creates a MonitoredCmdableFactory for a redis.Ring object.
func (MonitoredCmdableFactory) BuildClient ¶
func (f MonitoredCmdableFactory) BuildClient(ctx context.Context) MonitoredCmdable
BuildClient returns a new MonitoredCmdable with its context set to the provided one.
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()
Output:
func (SpanHook) AfterProcess ¶
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 ¶
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 ¶
BeforeProcess starts a client Span before processing a Redis command and starts a timer to record how long the command took.