From 44af3c09a71c968cf2f576553f85f9170e78173a Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 17 Nov 2022 16:24:16 +0200 Subject: [PATCH] chore: add more metrics to redisotel --- example/otel/README.md | 11 ++--------- example/otel/client.go | 2 +- example/otel/docker-compose.yml | 5 +---- extra/redisotel/config.go | 25 ++++++++++++++++++------- extra/redisotel/metrics.go | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/example/otel/README.md b/example/otel/README.md index 5d1431e0..5af11bcf 100644 --- a/example/otel/README.md +++ b/example/otel/README.md @@ -17,7 +17,6 @@ cd example/otel **Step 2**. Start the services using Docker: ```shell -docker-compose pull docker-compose up -d ``` @@ -27,16 +26,10 @@ docker-compose up -d docker-compose logs uptrace ``` -**Step 4**. Run the Redis client example: +**Step 4**. Run the Redis client example and Follow the link to view the trace: ```shell -UPTRACE_DSN=http://project2_secret_token@localhost:14317/2 go run client.go -``` - -**Step 5**. Follow the link from the CLI to view the trace: - -```shell -UPTRACE_DSN=http://project2_secret_token@localhost:14317/2 go run client.go +go run client.go trace: http://localhost:14318/traces/ee029d8782242c8ed38b16d961093b35 ``` diff --git a/example/otel/client.go b/example/otel/client.go index 97cec2cb..9e087a9e 100644 --- a/example/otel/client.go +++ b/example/otel/client.go @@ -22,7 +22,7 @@ func main() { uptrace.ConfigureOpentelemetry( // copy your project DSN here or use UPTRACE_DSN env var - // uptrace.WithDSN("http://project2_secret_token@localhost:14317/2"), + uptrace.WithDSN("http://project2_secret_token@localhost:14317/2"), uptrace.WithServiceName("myservice"), uptrace.WithServiceVersion("v1.0.0"), diff --git a/example/otel/docker-compose.yml b/example/otel/docker-compose.yml index 786f6e1a..e607dd01 100644 --- a/example/otel/docker-compose.yml +++ b/example/otel/docker-compose.yml @@ -18,7 +18,7 @@ services: - '9000:9000' uptrace: - image: 'uptrace/uptrace:1.2.0' + image: 'uptrace/uptrace:1.2.2' #image: 'uptrace/uptrace-dev:latest' restart: on-failure volumes: @@ -74,8 +74,5 @@ services: volumes: uptrace_data: - driver: local ch_data: - driver: local alertmanager_data: - driver: local diff --git a/extra/redisotel/config.go b/extra/redisotel/config.go index 7c29e0cc..5b558aa2 100644 --- a/extra/redisotel/config.go +++ b/extra/redisotel/config.go @@ -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) { diff --git a/extra/redisotel/metrics.go b/extra/redisotel/metrics.go index 6d41d327..49128536 100644 --- a/extra/redisotel/metrics.go +++ b/extra/redisotel/metrics.go @@ -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...)