2022-10-11 15:37:34 +03:00
|
|
|
package redisotel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2023-01-23 09:48:54 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
2022-10-11 15:37:34 +03:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InstrumentMetrics starts reporting OpenTelemetry Metrics.
|
|
|
|
//
|
|
|
|
// Based on https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/database-metrics.md
|
|
|
|
func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error {
|
|
|
|
baseOpts := make([]baseOption, len(opts))
|
|
|
|
for i, opt := range opts {
|
|
|
|
baseOpts[i] = opt
|
|
|
|
}
|
|
|
|
conf := newConfig(baseOpts...)
|
|
|
|
|
|
|
|
if conf.meter == nil {
|
|
|
|
conf.meter = conf.mp.Meter(
|
|
|
|
instrumName,
|
|
|
|
metric.WithInstrumentationVersion("semver:"+redis.Version()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rdb := rdb.(type) {
|
|
|
|
case *redis.Client:
|
|
|
|
if conf.poolName == "" {
|
|
|
|
opt := rdb.Options()
|
|
|
|
conf.poolName = opt.Addr
|
|
|
|
}
|
|
|
|
conf.attrs = append(conf.attrs, attribute.String("pool.name", conf.poolName))
|
|
|
|
|
|
|
|
if err := reportPoolStats(rdb, conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := addMetricsHook(rdb, conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case *redis.ClusterClient:
|
|
|
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
|
|
|
if conf.poolName == "" {
|
|
|
|
opt := rdb.Options()
|
|
|
|
conf.poolName = opt.Addr
|
|
|
|
}
|
|
|
|
conf.attrs = append(conf.attrs, attribute.String("pool.name", conf.poolName))
|
|
|
|
|
|
|
|
if err := reportPoolStats(rdb, conf); err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
if err := addMetricsHook(rdb, conf); err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
case *redis.Ring:
|
|
|
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
2022-11-18 16:32:17 +03:00
|
|
|
if conf.poolName == "" {
|
|
|
|
opt := rdb.Options()
|
|
|
|
conf.poolName = opt.Addr
|
|
|
|
}
|
|
|
|
conf.attrs = append(conf.attrs, attribute.String("pool.name", conf.poolName))
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
if err := reportPoolStats(rdb, conf); err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
if err := addMetricsHook(rdb, conf); err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("redisotel: %T not supported", rdb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reportPoolStats(rdb *redis.Client, conf *config) error {
|
|
|
|
labels := conf.attrs
|
|
|
|
idleAttrs := append(labels, attribute.String("state", "idle"))
|
|
|
|
usedAttrs := append(labels, attribute.String("state", "used"))
|
|
|
|
|
2023-02-01 11:04:33 +03:00
|
|
|
idleMax, err := conf.meter.Int64ObservableUpDownCounter(
|
2022-11-17 17:24:16 +03:00
|
|
|
"db.client.connections.idle.max",
|
2023-05-24 12:07:43 +03:00
|
|
|
metric.WithDescription("The maximum number of idle open connections allowed"),
|
2022-11-17 17:24:16 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:04:33 +03:00
|
|
|
idleMin, err := conf.meter.Int64ObservableUpDownCounter(
|
2022-11-17 17:24:16 +03:00
|
|
|
"db.client.connections.idle.min",
|
2023-05-24 12:07:43 +03:00
|
|
|
metric.WithDescription("The minimum number of idle open connections allowed"),
|
2022-11-17 17:24:16 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:04:33 +03:00
|
|
|
connsMax, err := conf.meter.Int64ObservableUpDownCounter(
|
2022-11-17 17:24:16 +03:00
|
|
|
"db.client.connections.max",
|
2023-05-24 12:07:43 +03:00
|
|
|
metric.WithDescription("The maximum number of open connections allowed"),
|
2022-11-17 17:24:16 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:04:33 +03:00
|
|
|
usage, err := conf.meter.Int64ObservableUpDownCounter(
|
2022-10-11 15:37:34 +03:00
|
|
|
"db.client.connections.usage",
|
2023-05-24 12:07:43 +03:00
|
|
|
metric.WithDescription("The number of connections that are currently in state described by the state attribute"),
|
2022-10-11 15:37:34 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:04:33 +03:00
|
|
|
timeouts, err := conf.meter.Int64ObservableUpDownCounter(
|
2022-10-11 15:37:34 +03:00
|
|
|
"db.client.connections.timeouts",
|
2023-05-24 12:07:43 +03:00
|
|
|
metric.WithDescription("The number of connection timeouts that have occurred trying to obtain a connection from the pool"),
|
2022-10-11 15:37:34 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:24:16 +03:00
|
|
|
redisConf := rdb.Options()
|
2023-02-01 11:04:33 +03:00
|
|
|
_, err = conf.meter.RegisterCallback(
|
|
|
|
func(ctx context.Context, o metric.Observer) error {
|
2022-10-11 15:37:34 +03:00
|
|
|
stats := rdb.PoolStats()
|
|
|
|
|
2023-05-01 11:33:30 +03:00
|
|
|
o.ObserveInt64(idleMax, int64(redisConf.MaxIdleConns), metric.WithAttributes(labels...))
|
|
|
|
o.ObserveInt64(idleMin, int64(redisConf.MinIdleConns), metric.WithAttributes(labels...))
|
|
|
|
o.ObserveInt64(connsMax, int64(redisConf.PoolSize), metric.WithAttributes(labels...))
|
2022-11-17 17:24:16 +03:00
|
|
|
|
2023-05-01 11:33:30 +03:00
|
|
|
o.ObserveInt64(usage, int64(stats.IdleConns), metric.WithAttributes(idleAttrs...))
|
|
|
|
o.ObserveInt64(usage, int64(stats.TotalConns-stats.IdleConns), metric.WithAttributes(usedAttrs...))
|
2022-10-11 15:37:34 +03:00
|
|
|
|
2023-05-01 11:33:30 +03:00
|
|
|
o.ObserveInt64(timeouts, int64(stats.Timeouts), metric.WithAttributes(labels...))
|
2023-02-01 11:04:33 +03:00
|
|
|
return nil
|
2022-10-11 15:37:34 +03:00
|
|
|
},
|
2023-02-01 11:04:33 +03:00
|
|
|
idleMax,
|
|
|
|
idleMin,
|
|
|
|
connsMax,
|
|
|
|
usage,
|
|
|
|
timeouts,
|
2022-10-11 15:37:34 +03:00
|
|
|
)
|
2023-02-01 11:04:33 +03:00
|
|
|
|
|
|
|
return err
|
2022-10-11 15:37:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func addMetricsHook(rdb *redis.Client, conf *config) error {
|
2023-02-01 11:04:33 +03:00
|
|
|
createTime, err := conf.meter.Float64Histogram(
|
2022-10-11 15:37:34 +03:00
|
|
|
"db.client.connections.create_time",
|
2023-05-24 12:07:43 +03:00
|
|
|
metric.WithDescription("The time it took to create a new connection."),
|
|
|
|
metric.WithUnit("ms"),
|
2022-10-11 15:37:34 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:04:33 +03:00
|
|
|
useTime, err := conf.meter.Float64Histogram(
|
2022-10-11 15:37:34 +03:00
|
|
|
"db.client.connections.use_time",
|
2023-05-24 12:07:43 +03:00
|
|
|
metric.WithDescription("The time between borrowing a connection and returning it to the pool."),
|
|
|
|
metric.WithUnit("ms"),
|
2022-10-11 15:37:34 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rdb.AddHook(&metricsHook{
|
|
|
|
createTime: createTime,
|
|
|
|
useTime: useTime,
|
2022-11-18 16:32:17 +03:00
|
|
|
attrs: conf.attrs,
|
2022-10-11 15:37:34 +03:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type metricsHook struct {
|
2023-05-24 12:07:43 +03:00
|
|
|
createTime metric.Float64Histogram
|
|
|
|
useTime metric.Float64Histogram
|
2022-11-18 16:32:17 +03:00
|
|
|
attrs []attribute.KeyValue
|
2022-10-11 15:37:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ redis.Hook = (*metricsHook)(nil)
|
|
|
|
|
|
|
|
func (mh *metricsHook) DialHook(hook redis.DialHook) redis.DialHook {
|
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conn, err := hook(ctx, network, addr)
|
|
|
|
|
2022-12-23 15:40:07 +03:00
|
|
|
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+1)
|
|
|
|
attrs = append(attrs, mh.attrs...)
|
|
|
|
attrs = append(attrs, statusAttr(err))
|
|
|
|
|
2023-05-01 11:33:30 +03:00
|
|
|
mh.createTime.Record(ctx, milliseconds(time.Since(start)), metric.WithAttributes(attrs...))
|
2022-10-11 15:37:34 +03:00
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
|
|
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
err := hook(ctx, cmd)
|
|
|
|
|
2022-11-18 16:32:17 +03:00
|
|
|
dur := time.Since(start)
|
|
|
|
|
|
|
|
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
|
|
|
|
attrs = append(attrs, mh.attrs...)
|
|
|
|
attrs = append(attrs, attribute.String("type", "command"))
|
|
|
|
attrs = append(attrs, statusAttr(err))
|
|
|
|
|
2023-05-01 11:33:30 +03:00
|
|
|
mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributes(attrs...))
|
2022-10-11 15:37:34 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *metricsHook) ProcessPipelineHook(
|
|
|
|
hook redis.ProcessPipelineHook,
|
|
|
|
) redis.ProcessPipelineHook {
|
|
|
|
return func(ctx context.Context, cmds []redis.Cmder) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
err := hook(ctx, cmds)
|
|
|
|
|
2022-11-18 16:32:17 +03:00
|
|
|
dur := time.Since(start)
|
|
|
|
|
|
|
|
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
|
|
|
|
attrs = append(attrs, mh.attrs...)
|
|
|
|
attrs = append(attrs, attribute.String("type", "pipeline"))
|
|
|
|
attrs = append(attrs, statusAttr(err))
|
|
|
|
|
2023-05-01 11:33:30 +03:00
|
|
|
mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributes(attrs...))
|
2022-10-11 15:37:34 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 16:32:17 +03:00
|
|
|
func milliseconds(d time.Duration) float64 {
|
|
|
|
return float64(d) / float64(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
func statusAttr(err error) attribute.KeyValue {
|
|
|
|
if err != nil {
|
|
|
|
return attribute.String("status", "error")
|
|
|
|
}
|
|
|
|
return attribute.String("status", "ok")
|
|
|
|
}
|