Skip to content

Commit dffbe61

Browse files
committed
Add tests
1 parent bfba654 commit dffbe61

File tree

3 files changed

+175
-16
lines changed

3 files changed

+175
-16
lines changed

extra/redisotel/go.mod

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ replace github.com/redis/go-redis/extra/rediscmd/v9 => ../rediscmd
99
require (
1010
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2
1111
github.com/redis/go-redis/v9 v9.6.2
12-
go.opentelemetry.io/otel v1.22.0
13-
go.opentelemetry.io/otel/metric v1.22.0
14-
go.opentelemetry.io/otel/sdk v1.22.0
15-
go.opentelemetry.io/otel/trace v1.22.0
12+
github.com/stretchr/testify v1.10.0
13+
go.opentelemetry.io/otel v1.23.0
14+
go.opentelemetry.io/otel/metric v1.23.0
15+
go.opentelemetry.io/otel/sdk v1.23.0
16+
go.opentelemetry.io/otel/sdk/metric v1.23.0
17+
go.opentelemetry.io/otel/trace v1.23.0
1618
)
1719

1820
require (
1921
github.com/cespare/xxhash/v2 v2.2.0 // indirect
22+
github.com/davecgh/go-spew v1.1.1 // indirect
2023
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2124
github.com/go-logr/logr v1.4.1 // indirect
2225
github.com/go-logr/stdr v1.2.2 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
2327
golang.org/x/sys v0.16.0 // indirect
28+
gopkg.in/yaml.v3 v3.0.1 // indirect
2429
)
2530

26-
retract (
27-
v9.5.3 // This version was accidentally released.
28-
)
31+
retract v9.5.3 // This version was accidentally released.

extra/redisotel/go.sum

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
33
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
44
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
67
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
78
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
89
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@@ -12,15 +13,22 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
1213
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
1314
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1415
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
16-
go.opentelemetry.io/otel v1.22.0 h1:xS7Ku+7yTFvDfDraDIJVpw7XPyuHlB9MCiqqX5mcJ6Y=
17-
go.opentelemetry.io/otel v1.22.0/go.mod h1:eoV4iAi3Ea8LkAEI9+GFT44O6T/D0GWAVFyZVCC6pMI=
18-
go.opentelemetry.io/otel/metric v1.22.0 h1:lypMQnGyJYeuYPhOM/bgjbFM6WE44W1/T45er4d8Hhg=
19-
go.opentelemetry.io/otel/metric v1.22.0/go.mod h1:evJGjVpZv0mQ5QBRJoBF64yMuOf4xCWdXjK8pzFvliY=
20-
go.opentelemetry.io/otel/sdk v1.22.0 h1:6coWHw9xw7EfClIC/+O31R8IY3/+EiRFHevmHafB2Gw=
21-
go.opentelemetry.io/otel/sdk v1.22.0/go.mod h1:iu7luyVGYovrRpe2fmj3CVKouQNdTOkxtLzPvPz1DOc=
22-
go.opentelemetry.io/otel/trace v1.22.0 h1:Hg6pPujv0XG9QaVbGOBVHunyuLcCC3jN7WEhPx83XD0=
23-
go.opentelemetry.io/otel/trace v1.22.0/go.mod h1:RbbHXVqKES9QhzZq/fE5UnOSILqRt40a21sPw2He1xo=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
18+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
19+
go.opentelemetry.io/otel v1.23.0 h1:Df0pqjqExIywbMCMTxkAwzjLZtRf+bBKLbUcpxO2C9E=
20+
go.opentelemetry.io/otel v1.23.0/go.mod h1:YCycw9ZeKhcJFrb34iVSkyT0iczq/zYDtZYFufObyB0=
21+
go.opentelemetry.io/otel/metric v1.23.0 h1:pazkx7ss4LFVVYSxYew7L5I6qvLXHA0Ap2pwV+9Cnpo=
22+
go.opentelemetry.io/otel/metric v1.23.0/go.mod h1:MqUW2X2a6Q8RN96E2/nqNoT+z9BSms20Jb7Bbp+HiTo=
23+
go.opentelemetry.io/otel/sdk v1.23.0 h1:0KM9Zl2esnl+WSukEmlaAEjVY5HDZANOHferLq36BPc=
24+
go.opentelemetry.io/otel/sdk v1.23.0/go.mod h1:wUscup7byToqyKJSilEtMf34FgdCAsFpFOjXnAwFfO0=
25+
go.opentelemetry.io/otel/sdk/metric v1.23.0 h1:u81lMvmK6GMgN4Fty7K7S6cSKOZhMKJMK2TB+KaTs0I=
26+
go.opentelemetry.io/otel/sdk/metric v1.23.0/go.mod h1:2LUOToN/FdX6wtfpHybOnCZjoZ6ViYajJYMiJ1LKDtQ=
27+
go.opentelemetry.io/otel/trace v1.23.0 h1:37Ik5Ib7xfYVb4V1UtnT97T1jI+AoIYkJyPkuL4iJgI=
28+
go.opentelemetry.io/otel/trace v1.23.0/go.mod h1:GSGTbIClEsuZrGIzoEHqsVfxgn5UkggkflQwDScNUsk=
2429
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
2530
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
31+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
32+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2633
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

extra/redisotel/metrics_test.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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

Comments
 (0)