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

@ -17,7 +17,6 @@ cd example/otel
**Step 2**. Start the services using Docker: **Step 2**. Start the services using Docker:
```shell ```shell
docker-compose pull
docker-compose up -d docker-compose up -d
``` ```
@ -27,16 +26,10 @@ docker-compose up -d
docker-compose logs uptrace 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 ```shell
UPTRACE_DSN=http://project2_secret_token@localhost:14317/2 go run client.go 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
trace: http://localhost:14318/traces/ee029d8782242c8ed38b16d961093b35 trace: http://localhost:14318/traces/ee029d8782242c8ed38b16d961093b35
``` ```

View File

@ -22,7 +22,7 @@ func main() {
uptrace.ConfigureOpentelemetry( uptrace.ConfigureOpentelemetry(
// copy your project DSN here or use UPTRACE_DSN env var // 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.WithServiceName("myservice"),
uptrace.WithServiceVersion("v1.0.0"), uptrace.WithServiceVersion("v1.0.0"),

View File

@ -18,7 +18,7 @@ services:
- '9000:9000' - '9000:9000'
uptrace: uptrace:
image: 'uptrace/uptrace:1.2.0' image: 'uptrace/uptrace:1.2.2'
#image: 'uptrace/uptrace-dev:latest' #image: 'uptrace/uptrace-dev:latest'
restart: on-failure restart: on-failure
volumes: volumes:
@ -74,8 +74,5 @@ services:
volumes: volumes:
uptrace_data: uptrace_data:
driver: local
ch_data: ch_data:
driver: local
alertmanager_data: alertmanager_data:
driver: local

View File

@ -5,14 +5,15 @@ import (
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global" "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" "go.opentelemetry.io/otel/trace"
) )
type config struct { type config struct {
// Common options. // Common options.
attrs []attribute.KeyValue dbSystem string
attrs []attribute.KeyValue
// Tracing options. // Tracing options.
@ -51,19 +52,29 @@ func (fn option) metrics() {}
func newConfig(opts ...baseOption) *config { func newConfig(opts ...baseOption) *config {
conf := &config{ conf := &config{
tp: otel.GetTracerProvider(), dbSystem: "redis",
mp: global.MeterProvider(), attrs: []attribute.KeyValue{},
attrs: []attribute.KeyValue{
semconv.DBSystemRedis, tp: otel.GetTracerProvider(),
}, mp: global.MeterProvider(),
dbStmtEnabled: true, dbStmtEnabled: true,
} }
for _, opt := range opts { for _, opt := range opts {
opt.apply(conf) opt.apply(conf)
} }
conf.attrs = append(conf.attrs, semconv.DBSystemKey.String(conf.dbSystem))
return conf 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. // WithAttributes specifies additional attributes to be added to the span.
func WithAttributes(attrs ...attribute.KeyValue) Option { func WithAttributes(attrs ...attribute.KeyValue) Option {
return option(func(conf *config) { 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")) idleAttrs := append(labels, attribute.String("state", "idle"))
usedAttrs := append(labels, attribute.String("state", "used")) 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( usage, err := conf.meter.AsyncInt64().UpDownCounter(
"db.client.connections.usage", "db.client.connections.usage",
instrument.WithDescription("The number of connections that are currently in state described by the state attribute"), 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 return err
} }
redisConf := rdb.Options()
return conf.meter.RegisterCallback( return conf.meter.RegisterCallback(
[]instrument.Asynchronous{ []instrument.Asynchronous{
idleMax,
idleMin,
connsMax,
usage, usage,
timeouts, timeouts,
}, },
func(ctx context.Context) { func(ctx context.Context) {
stats := rdb.PoolStats() 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.IdleConns), idleAttrs...)
usage.Observe(ctx, int64(stats.TotalConns-stats.IdleConns), usedAttrs...) usage.Observe(ctx, int64(stats.TotalConns-stats.IdleConns), usedAttrs...)