mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +03:00
feat: upgrade OpenTelemetry, use the new metrics API. (#2410)
This commit is contained in:
@ -9,8 +9,8 @@ replace github.com/redis/go-redis/extra/rediscmd/v9 => ../rediscmd
|
||||
require (
|
||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.1
|
||||
github.com/redis/go-redis/v9 v9.0.1
|
||||
go.opentelemetry.io/otel v1.11.2
|
||||
go.opentelemetry.io/otel/metric v0.34.0
|
||||
go.opentelemetry.io/otel/sdk v1.9.0
|
||||
go.opentelemetry.io/otel/trace v1.11.2
|
||||
go.opentelemetry.io/otel v1.12.0
|
||||
go.opentelemetry.io/otel/metric v0.35.0
|
||||
go.opentelemetry.io/otel/sdk v1.12.0
|
||||
go.opentelemetry.io/otel/trace v1.12.0
|
||||
)
|
||||
|
@ -14,7 +14,6 @@ github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
|
||||
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@ -26,18 +25,16 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo=
|
||||
go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0=
|
||||
go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI=
|
||||
go.opentelemetry.io/otel/metric v0.34.0 h1:MCPoQxcg/26EuuJwpYN1mZTeCYAUGx8ABxfW07YkjP8=
|
||||
go.opentelemetry.io/otel/metric v0.34.0/go.mod h1:ZFuI4yQGNCupurTXCwkeD/zHBt+C2bR7bw5JqUm/AP8=
|
||||
go.opentelemetry.io/otel/sdk v1.9.0 h1:LNXp1vrr83fNXTHgU8eO89mhzxb/bbWAsHG6fNf3qWo=
|
||||
go.opentelemetry.io/otel/sdk v1.9.0/go.mod h1:AEZc8nt5bd2F7BC24J5R0mrjYnpEgYHyTcM/vrSple4=
|
||||
go.opentelemetry.io/otel/trace v1.9.0/go.mod h1:2737Q0MuG8q1uILYm2YYVkAyLtOofiTNGg6VODnOiPo=
|
||||
go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0=
|
||||
go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA=
|
||||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 h1:iGu644GcxtEcrInvDsQRCwJjtCIOlT2V7IRt6ah2Whw=
|
||||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
go.opentelemetry.io/otel v1.12.0 h1:IgfC7kqQrRccIKuB7Cl+SRUmsKbEwSGPr0Eu+/ht1SQ=
|
||||
go.opentelemetry.io/otel v1.12.0/go.mod h1:geaoz0L0r1BEOR81k7/n9W4TCXYCJ7bPO7K374jQHG0=
|
||||
go.opentelemetry.io/otel/metric v0.35.0 h1:aPT5jk/w7F9zW51L7WgRqNKDElBdyRLGuBtI5MX34e8=
|
||||
go.opentelemetry.io/otel/metric v0.35.0/go.mod h1:qAcbhaTRFU6uG8QM7dDo7XvFsWcugziq/5YI065TokQ=
|
||||
go.opentelemetry.io/otel/sdk v1.12.0 h1:8npliVYV7qc0t1FKdpU08eMnOjgPFMnriPhn0HH4q3o=
|
||||
go.opentelemetry.io/otel/sdk v1.12.0/go.mod h1:WYcvtgquYvgODEvxOry5owO2y9MyciW7JqMz6cpXShE=
|
||||
go.opentelemetry.io/otel/trace v1.12.0 h1:p28in++7Kd0r2d8gSt931O57fdjUyWxkVbESuILAeUc=
|
||||
go.opentelemetry.io/otel/trace v1.12.0/go.mod h1:pHlgBynn6s25qJ2szD+Bv+iwKJttjHSI3lUAyf0GNuQ=
|
||||
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
|
||||
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user