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

chore: add more metrics to redisotel

This commit is contained in:
Vladimir Mihailenco
2022-11-17 16:24:16 +02:00
parent f9e60f214b
commit 44af3c09a7
5 changed files with 54 additions and 21 deletions

View File

@ -5,14 +5,15 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
)
type config struct {
// Common options.
attrs []attribute.KeyValue
dbSystem string
attrs []attribute.KeyValue
// Tracing options.
@ -51,19 +52,29 @@ func (fn option) metrics() {}
func newConfig(opts ...baseOption) *config {
conf := &config{
tp: otel.GetTracerProvider(),
mp: global.MeterProvider(),
attrs: []attribute.KeyValue{
semconv.DBSystemRedis,
},
dbSystem: "redis",
attrs: []attribute.KeyValue{},
tp: otel.GetTracerProvider(),
mp: global.MeterProvider(),
dbStmtEnabled: true,
}
for _, opt := range opts {
opt.apply(conf)
}
conf.attrs = append(conf.attrs, semconv.DBSystemKey.String(conf.dbSystem))
return conf
}
func WithDBSystem(dbSystem string) Option {
return option(func(conf *config) {
conf.dbSystem = dbSystem
})
}
// WithAttributes specifies additional attributes to be added to the span.
func WithAttributes(attrs ...attribute.KeyValue) Option {
return option(func(conf *config) {

View File

@ -82,6 +82,30 @@ 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(
"db.client.connections.idle.max",
instrument.WithDescription("The maximum number of idle open connections allowed"),
)
if err != nil {
return err
}
idleMin, err := conf.meter.AsyncInt64().UpDownCounter(
"db.client.connections.idle.min",
instrument.WithDescription("The minimum number of idle open connections allowed"),
)
if err != nil {
return err
}
connsMax, err := conf.meter.AsyncInt64().UpDownCounter(
"db.client.connections.max",
instrument.WithDescription("The maximum number of open connections allowed"),
)
if err != nil {
return err
}
usage, err := conf.meter.AsyncInt64().UpDownCounter(
"db.client.connections.usage",
instrument.WithDescription("The number of connections that are currently in state described by the state attribute"),
@ -98,14 +122,22 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
return err
}
redisConf := rdb.Options()
return conf.meter.RegisterCallback(
[]instrument.Asynchronous{
idleMax,
idleMin,
connsMax,
usage,
timeouts,
},
func(ctx context.Context) {
stats := rdb.PoolStats()
idleMax.Observe(ctx, int64(redisConf.MinIdleConns))
idleMin.Observe(ctx, int64(redisConf.MaxIdleConns))
connsMax.Observe(ctx, int64(redisConf.PoolSize))
usage.Observe(ctx, int64(stats.IdleConns), idleAttrs...)
usage.Observe(ctx, int64(stats.TotalConns-stats.IdleConns), usedAttrs...)