Skip to content

Commit 44af3c0

Browse files
committed
chore: add more metrics to redisotel
1 parent f9e60f2 commit 44af3c0

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed

example/otel/README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ cd example/otel
1717
**Step 2**. Start the services using Docker:
1818

1919
```shell
20-
docker-compose pull
2120
docker-compose up -d
2221
```
2322

@@ -27,16 +26,10 @@ docker-compose up -d
2726
docker-compose logs uptrace
2827
```
2928

30-
**Step 4**. Run the Redis client example:
29+
**Step 4**. Run the Redis client example and Follow the link to view the trace:
3130

3231
```shell
33-
UPTRACE_DSN=http://project2_secret_token@localhost:14317/2 go run client.go
34-
```
35-
36-
**Step 5**. Follow the link from the CLI to view the trace:
37-
38-
```shell
39-
UPTRACE_DSN=http://project2_secret_token@localhost:14317/2 go run client.go
32+
go run client.go
4033
trace: http://localhost:14318/traces/ee029d8782242c8ed38b16d961093b35
4134
```
4235

example/otel/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func main() {
2222

2323
uptrace.ConfigureOpentelemetry(
2424
// copy your project DSN here or use UPTRACE_DSN env var
25-
// uptrace.WithDSN("http://project2_secret_token@localhost:14317/2"),
25+
uptrace.WithDSN("http://project2_secret_token@localhost:14317/2"),
2626

2727
uptrace.WithServiceName("myservice"),
2828
uptrace.WithServiceVersion("v1.0.0"),

example/otel/docker-compose.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
- '9000:9000'
1919

2020
uptrace:
21-
image: 'uptrace/uptrace:1.2.0'
21+
image: 'uptrace/uptrace:1.2.2'
2222
#image: 'uptrace/uptrace-dev:latest'
2323
restart: on-failure
2424
volumes:
@@ -74,8 +74,5 @@ services:
7474

7575
volumes:
7676
uptrace_data:
77-
driver: local
7877
ch_data:
79-
driver: local
8078
alertmanager_data:
81-
driver: local

extra/redisotel/config.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"go.opentelemetry.io/otel/attribute"
66
"go.opentelemetry.io/otel/metric"
77
"go.opentelemetry.io/otel/metric/global"
8-
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
8+
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
99
"go.opentelemetry.io/otel/trace"
1010
)
1111

1212
type config struct {
1313
// Common options.
1414

15-
attrs []attribute.KeyValue
15+
dbSystem string
16+
attrs []attribute.KeyValue
1617

1718
// Tracing options.
1819

@@ -51,19 +52,29 @@ func (fn option) metrics() {}
5152

5253
func newConfig(opts ...baseOption) *config {
5354
conf := &config{
54-
tp: otel.GetTracerProvider(),
55-
mp: global.MeterProvider(),
56-
attrs: []attribute.KeyValue{
57-
semconv.DBSystemRedis,
58-
},
55+
dbSystem: "redis",
56+
attrs: []attribute.KeyValue{},
57+
58+
tp: otel.GetTracerProvider(),
59+
mp: global.MeterProvider(),
5960
dbStmtEnabled: true,
6061
}
62+
6163
for _, opt := range opts {
6264
opt.apply(conf)
6365
}
66+
67+
conf.attrs = append(conf.attrs, semconv.DBSystemKey.String(conf.dbSystem))
68+
6469
return conf
6570
}
6671

72+
func WithDBSystem(dbSystem string) Option {
73+
return option(func(conf *config) {
74+
conf.dbSystem = dbSystem
75+
})
76+
}
77+
6778
// WithAttributes specifies additional attributes to be added to the span.
6879
func WithAttributes(attrs ...attribute.KeyValue) Option {
6980
return option(func(conf *config) {

extra/redisotel/metrics.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
8282
idleAttrs := append(labels, attribute.String("state", "idle"))
8383
usedAttrs := append(labels, attribute.String("state", "used"))
8484

85+
idleMax, err := conf.meter.AsyncInt64().UpDownCounter(
86+
"db.client.connections.idle.max",
87+
instrument.WithDescription("The maximum number of idle open connections allowed"),
88+
)
89+
if err != nil {
90+
return err
91+
}
92+
93+
idleMin, err := conf.meter.AsyncInt64().UpDownCounter(
94+
"db.client.connections.idle.min",
95+
instrument.WithDescription("The minimum number of idle open connections allowed"),
96+
)
97+
if err != nil {
98+
return err
99+
}
100+
101+
connsMax, err := conf.meter.AsyncInt64().UpDownCounter(
102+
"db.client.connections.max",
103+
instrument.WithDescription("The maximum number of open connections allowed"),
104+
)
105+
if err != nil {
106+
return err
107+
}
108+
85109
usage, err := conf.meter.AsyncInt64().UpDownCounter(
86110
"db.client.connections.usage",
87111
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 {
98122
return err
99123
}
100124

125+
redisConf := rdb.Options()
101126
return conf.meter.RegisterCallback(
102127
[]instrument.Asynchronous{
128+
idleMax,
129+
idleMin,
130+
connsMax,
103131
usage,
104132
timeouts,
105133
},
106134
func(ctx context.Context) {
107135
stats := rdb.PoolStats()
108136

137+
idleMax.Observe(ctx, int64(redisConf.MinIdleConns))
138+
idleMin.Observe(ctx, int64(redisConf.MaxIdleConns))
139+
connsMax.Observe(ctx, int64(redisConf.PoolSize))
140+
109141
usage.Observe(ctx, int64(stats.IdleConns), idleAttrs...)
110142
usage.Observe(ctx, int64(stats.TotalConns-stats.IdleConns), usedAttrs...)
111143

0 commit comments

Comments
 (0)