1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

feat: upgrade OpenTelemetry, use the new metrics API. (#2410)

This commit is contained in:
Ngọc Long
2023-02-01 15:04:33 +07:00
committed by GitHub
parent 154ca02e2f
commit e29e42cde2
5 changed files with 71 additions and 78 deletions

View File

@ -11,7 +11,6 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
)
// InstrumentMetrics starts reporting OpenTelemetry Metrics.
@ -88,7 +87,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
idleAttrs := append(labels, attribute.String("state", "idle"))
usedAttrs := append(labels, attribute.String("state", "used"))
idleMax, err := conf.meter.AsyncInt64().UpDownCounter(
idleMax, err := conf.meter.Int64ObservableUpDownCounter(
"db.client.connections.idle.max",
instrument.WithDescription("The maximum number of idle open connections allowed"),
)
@ -96,7 +95,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
return err
}
idleMin, err := conf.meter.AsyncInt64().UpDownCounter(
idleMin, err := conf.meter.Int64ObservableUpDownCounter(
"db.client.connections.idle.min",
instrument.WithDescription("The minimum number of idle open connections allowed"),
)
@ -104,7 +103,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
return err
}
connsMax, err := conf.meter.AsyncInt64().UpDownCounter(
connsMax, err := conf.meter.Int64ObservableUpDownCounter(
"db.client.connections.max",
instrument.WithDescription("The maximum number of open connections allowed"),
)
@ -112,7 +111,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
return err
}
usage, err := conf.meter.AsyncInt64().UpDownCounter(
usage, err := conf.meter.Int64ObservableUpDownCounter(
"db.client.connections.usage",
instrument.WithDescription("The number of connections that are currently in state described by the state attribute"),
)
@ -120,7 +119,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
return err
}
timeouts, err := conf.meter.AsyncInt64().UpDownCounter(
timeouts, err := conf.meter.Int64ObservableUpDownCounter(
"db.client.connections.timeouts",
instrument.WithDescription("The number of connection timeouts that have occurred trying to obtain a connection from the pool"),
)
@ -129,31 +128,32 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
}
redisConf := rdb.Options()
return conf.meter.RegisterCallback(
[]instrument.Asynchronous{
idleMax,
idleMin,
connsMax,
usage,
timeouts,
},
func(ctx context.Context) {
_, err = conf.meter.RegisterCallback(
func(ctx context.Context, o metric.Observer) error {
stats := rdb.PoolStats()
idleMax.Observe(ctx, int64(redisConf.MaxIdleConns), labels...)
idleMin.Observe(ctx, int64(redisConf.MinIdleConns), labels...)
connsMax.Observe(ctx, int64(redisConf.PoolSize), labels...)
o.ObserveInt64(idleMax, int64(redisConf.MaxIdleConns), labels...)
o.ObserveInt64(idleMin, int64(redisConf.MinIdleConns), labels...)
o.ObserveInt64(connsMax, int64(redisConf.PoolSize), labels...)
usage.Observe(ctx, int64(stats.IdleConns), idleAttrs...)
usage.Observe(ctx, int64(stats.TotalConns-stats.IdleConns), usedAttrs...)
o.ObserveInt64(usage, int64(stats.IdleConns), idleAttrs...)
o.ObserveInt64(usage, int64(stats.TotalConns-stats.IdleConns), usedAttrs...)
timeouts.Observe(ctx, int64(stats.Timeouts), labels...)
o.ObserveInt64(timeouts, int64(stats.Timeouts), labels...)
return nil
},
idleMax,
idleMin,
connsMax,
usage,
timeouts,
)
return err
}
func addMetricsHook(rdb *redis.Client, conf *config) error {
createTime, err := conf.meter.SyncFloat64().Histogram(
createTime, err := conf.meter.Float64Histogram(
"db.client.connections.create_time",
instrument.WithDescription("The time it took to create a new connection."),
instrument.WithUnit("ms"),
@ -162,7 +162,7 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
return err
}
useTime, err := conf.meter.SyncFloat64().Histogram(
useTime, err := conf.meter.Float64Histogram(
"db.client.connections.use_time",
instrument.WithDescription("The time between borrowing a connection and returning it to the pool."),
instrument.WithUnit("ms"),
@ -180,8 +180,8 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
}
type metricsHook struct {
createTime syncfloat64.Histogram
useTime syncfloat64.Histogram
createTime instrument.Float64Histogram
useTime instrument.Float64Histogram
attrs []attribute.KeyValue
}