73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
// Package redisadapter provides the Redis client helpers used by Notification
|
|
// Service runtime wiring.
|
|
package redisadapter
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"galaxy/notification/internal/config"
|
|
"galaxy/notification/internal/telemetry"
|
|
|
|
"github.com/redis/go-redis/extra/redisotel/v9"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// NewClient constructs one Redis client from cfg.
|
|
func NewClient(cfg config.RedisConfig) *redis.Client {
|
|
return redis.NewClient(&redis.Options{
|
|
Addr: cfg.Addr,
|
|
Username: cfg.Username,
|
|
Password: cfg.Password,
|
|
DB: cfg.DB,
|
|
TLSConfig: cfg.TLSConfig(),
|
|
DialTimeout: cfg.OperationTimeout,
|
|
ReadTimeout: cfg.OperationTimeout,
|
|
WriteTimeout: cfg.OperationTimeout,
|
|
})
|
|
}
|
|
|
|
// InstrumentClient attaches Redis tracing and metrics exporters to client when
|
|
// telemetryRuntime is available.
|
|
func InstrumentClient(client *redis.Client, telemetryRuntime *telemetry.Runtime) error {
|
|
if client == nil {
|
|
return fmt.Errorf("instrument redis client: nil client")
|
|
}
|
|
if telemetryRuntime == nil {
|
|
return nil
|
|
}
|
|
|
|
if err := redisotel.InstrumentTracing(
|
|
client,
|
|
redisotel.WithTracerProvider(telemetryRuntime.TracerProvider()),
|
|
redisotel.WithDBStatement(false),
|
|
); err != nil {
|
|
return fmt.Errorf("instrument redis client tracing: %w", err)
|
|
}
|
|
if err := redisotel.InstrumentMetrics(
|
|
client,
|
|
redisotel.WithMeterProvider(telemetryRuntime.MeterProvider()),
|
|
); err != nil {
|
|
return fmt.Errorf("instrument redis client metrics: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Ping performs the startup Redis connectivity check bounded by
|
|
// cfg.OperationTimeout.
|
|
func Ping(ctx context.Context, cfg config.RedisConfig, client *redis.Client) error {
|
|
if client == nil {
|
|
return fmt.Errorf("ping redis: nil client")
|
|
}
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, cfg.OperationTimeout)
|
|
defer cancel()
|
|
|
|
if err := client.Ping(pingCtx).Err(); err != nil {
|
|
return fmt.Errorf("ping redis: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|