|
| 1 | +package redisotel |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/redis/go-redis/v9" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "go.opentelemetry.io/otel" |
| 12 | + "go.opentelemetry.io/otel/attribute" |
| 13 | + "go.opentelemetry.io/otel/metric" |
| 14 | + "go.opentelemetry.io/otel/sdk/instrumentation" |
| 15 | + sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 16 | + "go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 17 | + "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 18 | + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" |
| 19 | +) |
| 20 | + |
| 21 | +var instrumentationScope = instrumentation.Scope{ |
| 22 | + Name: instrumName, |
| 23 | + Version: "semver:" + redis.Version(), |
| 24 | +} |
| 25 | + |
| 26 | +func setupMetrics(conf *config) (*sdkmetric.ManualReader, *redis.Client) { |
| 27 | + reader := sdkmetric.NewManualReader() |
| 28 | + mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 29 | + otel.SetMeterProvider(mp) |
| 30 | + |
| 31 | + rdb := redis.NewClient(&redis.Options{ |
| 32 | + Addr: ":6379", |
| 33 | + }) |
| 34 | + if conf.meter == nil { |
| 35 | + conf.meter = conf.mp.Meter( |
| 36 | + instrumName, |
| 37 | + metric.WithInstrumentationVersion("semver:"+redis.Version()), |
| 38 | + ) |
| 39 | + } |
| 40 | + addMetricsHook(rdb, conf) |
| 41 | + return reader, rdb |
| 42 | +} |
| 43 | + |
| 44 | +func TestMetrics(t *testing.T) { |
| 45 | + reader, rdb := setupMetrics(newConfig()) |
| 46 | + rdb.Get(context.Background(), "key") |
| 47 | + |
| 48 | + want := metricdata.ScopeMetrics{ |
| 49 | + Scope: instrumentationScope, |
| 50 | + Metrics: []metricdata.Metrics{ |
| 51 | + { |
| 52 | + Name: "db.client.connections.create_time", |
| 53 | + Description: "The time it took to create a new connection.", |
| 54 | + Unit: "ms", |
| 55 | + Data: metricdata.Histogram[float64]{ |
| 56 | + Temporality: metricdata.CumulativeTemporality, |
| 57 | + DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 58 | + { |
| 59 | + Attributes: attribute.NewSet( |
| 60 | + semconv.DBSystemRedis, |
| 61 | + attribute.String("status", "error"), |
| 62 | + ), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + Name: "db.client.connections.use_time", |
| 69 | + Description: "The time between borrowing a connection and returning it to the pool.", |
| 70 | + Unit: "ms", |
| 71 | + Data: metricdata.Histogram[float64]{ |
| 72 | + Temporality: metricdata.CumulativeTemporality, |
| 73 | + DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 74 | + { |
| 75 | + Attributes: attribute.NewSet( |
| 76 | + semconv.DBSystemRedis, |
| 77 | + attribute.String("type", "command"), |
| 78 | + attribute.String("status", "error"), |
| 79 | + ), |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + rm := metricdata.ResourceMetrics{} |
| 87 | + err := reader.Collect(context.Background(), &rm) |
| 88 | + assert.NoError(t, err) |
| 89 | + require.Len(t, rm.ScopeMetrics, 1) |
| 90 | + metricdatatest.AssertEqual(t, want, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()) |
| 91 | +} |
| 92 | + |
| 93 | +func TestCustomAttributes(t *testing.T) { |
| 94 | + customAttrFn := func(ctx context.Context) []attribute.KeyValue { |
| 95 | + return []attribute.KeyValue{ |
| 96 | + attribute.String("custom", "value"), |
| 97 | + } |
| 98 | + } |
| 99 | + config := newConfig(WithAttributesFunc(customAttrFn)) |
| 100 | + reader, rdb := setupMetrics(config) |
| 101 | + |
| 102 | + rdb.Get(context.Background(), "key") |
| 103 | + |
| 104 | + want := metricdata.ScopeMetrics{ |
| 105 | + Scope: instrumentationScope, |
| 106 | + Metrics: []metricdata.Metrics{ |
| 107 | + { |
| 108 | + Name: "db.client.connections.create_time", |
| 109 | + Description: "The time it took to create a new connection.", |
| 110 | + Unit: "ms", |
| 111 | + Data: metricdata.Histogram[float64]{ |
| 112 | + Temporality: metricdata.CumulativeTemporality, |
| 113 | + DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 114 | + { |
| 115 | + Attributes: attribute.NewSet( |
| 116 | + semconv.DBSystemRedis, |
| 117 | + attribute.String("status", "error"), |
| 118 | + ), |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + { |
| 124 | + Name: "db.client.connections.use_time", |
| 125 | + Description: "The time between borrowing a connection and returning it to the pool.", |
| 126 | + Unit: "ms", |
| 127 | + Data: metricdata.Histogram[float64]{ |
| 128 | + Temporality: metricdata.CumulativeTemporality, |
| 129 | + DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 130 | + { |
| 131 | + Attributes: attribute.NewSet( |
| 132 | + semconv.DBSystemRedis, |
| 133 | + attribute.String("type", "command"), |
| 134 | + attribute.String("status", "error"), |
| 135 | + attribute.String("custom", "value"), |
| 136 | + ), |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + rm := metricdata.ResourceMetrics{} |
| 144 | + err := reader.Collect(context.Background(), &rm) |
| 145 | + assert.NoError(t, err) |
| 146 | + require.Len(t, rm.ScopeMetrics, 1) |
| 147 | + metricdatatest.AssertEqual(t, want, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()) |
| 148 | +} |
0 commit comments