| package redis |
|
|
| import ( |
| "crypto/tls" |
| "fmt" |
|
|
| "github.com/redis/go-redis/extra/redisotel/v9" |
| "github.com/redis/go-redis/v9" |
| "go.opentelemetry.io/otel/metric" |
| "go.opentelemetry.io/otel/trace" |
| ) |
|
|
| type Option func(*Options) |
|
|
| |
| func WithTracingProvider(p trace.TracerProvider) Option { |
| return func(o *Options) { |
| if p != nil { |
| o.TracingProvider = p |
| } |
| } |
| } |
|
|
| |
| func WithMeterProvider(p metric.MeterProvider) Option { |
| return func(o *Options) { |
| if p != nil { |
| o.MeterProvider = p |
| } |
| } |
| } |
|
|
| |
| type Options struct { |
| Config |
|
|
| TracingProvider trace.TracerProvider |
| MeterProvider metric.MeterProvider |
| } |
|
|
| |
| func NewClient(o Options, opts ...Option) (*redis.Client, error) { |
| |
| for _, opt := range opts { |
| opt(&o) |
| } |
|
|
| |
| var tlsConfig *tls.Config |
| if o.TLS.Enabled { |
| tlsConfig = &tls.Config{ |
| InsecureSkipVerify: o.TLS.InsecureSkipVerify, |
| MinVersion: tls.VersionTLS13, |
| } |
| } |
|
|
| |
| var client *redis.Client |
| if o.Sentinel.Enabled { |
| client = redis.NewFailoverClient(&redis.FailoverOptions{ |
| MasterName: o.Sentinel.MasterName, |
| SentinelAddrs: []string{o.Address}, |
| DB: o.Database, |
| Username: o.Username, |
| Password: o.Password, |
| TLSConfig: tlsConfig, |
| }) |
| } else { |
| client = redis.NewClient(&redis.Options{ |
| Addr: o.Address, |
| DB: o.Database, |
| Username: o.Username, |
| Password: o.Password, |
| TLSConfig: tlsConfig, |
| }) |
| } |
|
|
| |
| var tracingOpts []redisotel.TracingOption |
| if o.TracingProvider != nil { |
| tracingOpts = append(tracingOpts, redisotel.WithTracerProvider(o.TracingProvider)) |
| } |
| if err := redisotel.InstrumentTracing(client, tracingOpts...); err != nil { |
| return nil, fmt.Errorf("failed to instrument redis client with tracing provider: %w", err) |
| } |
|
|
| |
| var metricsOpts []redisotel.MetricsOption |
| if o.MeterProvider != nil { |
| metricsOpts = append(metricsOpts, redisotel.WithMeterProvider(o.MeterProvider)) |
| } |
| if err := redisotel.InstrumentMetrics(client, metricsOpts...); err != nil { |
| return nil, fmt.Errorf("failed to instrument redis client with meter provider: %w", err) |
| } |
|
|
| return client, nil |
| } |
|
|