72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"galaxy/lobby/internal/config"
|
|
"galaxy/lobby/internal/telemetry"
|
|
|
|
"github.com/redis/go-redis/extra/redisotel/v9"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// newRedisClient builds a Redis client wired with the configured timeouts
|
|
// and TLS settings taken from cfg.
|
|
func newRedisClient(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,
|
|
})
|
|
}
|
|
|
|
// instrumentRedisClient attaches the OpenTelemetry tracing and metrics
|
|
// instrumentation to client when telemetryRuntime is available.
|
|
func instrumentRedisClient(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
|
|
}
|
|
|
|
// pingRedis performs a single Redis PING bounded by cfg.OperationTimeout to
|
|
// confirm that the configured Redis endpoint is reachable at startup.
|
|
func pingRedis(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
|
|
}
|