45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package drivers
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"github.com/redis/go-redis/extra/redisotel/v9"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/spf13/viper"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
|
|
)
|
|
|
|
func Redis(dsn RedisDSN) (redis.UniversalClient, error) {
|
|
serviceName := viper.GetString("cache.service_name")
|
|
|
|
// Connect to Redis
|
|
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
|
|
Addrs: dsn.Hosts,
|
|
MasterName: dsn.Master,
|
|
Username: dsn.Username,
|
|
Password: dsn.Password,
|
|
DB: dsn.DB,
|
|
})
|
|
|
|
attrs := []attribute.KeyValue{
|
|
semconv.DBSystemRedis,
|
|
attribute.String("db.instance", serviceName),
|
|
}
|
|
|
|
if err := redisotel.InstrumentMetrics(rdb, redisotel.WithAttributes(attrs...)); err != nil {
|
|
slog.Error("[Redis] Error starting otel metrics plugin!", "name", serviceName, "err", err)
|
|
}
|
|
|
|
if err := redisotel.InstrumentTracing(rdb, redisotel.WithAttributes(attrs...)); err != nil {
|
|
slog.Error("[Redis] Error starting otel tracing plugin!", "name", serviceName, "err", err)
|
|
}
|
|
|
|
// Ping redis
|
|
ctx := context.Background()
|
|
_, err := rdb.Ping(ctx).Result()
|
|
|
|
return rdb, err
|
|
}
|