@@ -14,8 +14,7 @@ version of the plugin. If you want to add tests for redis, you should do the fol
14
14
### 1. Add lowest-version dependency for your rule
15
15
16
16
For example, if you add a rule that support redis from ` v9.0.5 ` to the latest version, you should verify the lowest
17
- redis
18
- version, which is ` v9.0.5 ` first. You may create a subdirectory named ` v9.0.5 ` and add the following ` go.mod ` :
17
+ redis version, which is ` v9.0.5 ` first. You may create a subdirectory named ` v9.0.5 ` and add the following ` go.mod ` :
19
18
20
19
```
21
20
module redis/v9.0.5
@@ -24,9 +23,11 @@ go 1.22
24
23
25
24
replace github.com/alibaba/opentelemetry-go-auto-instrumentation => ../../../../opentelemetry-go-auto-instrumentation
26
25
26
+ replace github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier => ../../../../opentelemetry-go-auto-instrumentation/test/verifier
27
+
27
28
require (
28
29
// import this dependency to use verifier
29
- github.com/alibaba/opentelemetry-go-auto-instrumentation v0.0.0-00010101000000-000000000000
30
+ github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier v0.0.0-00010101000000-000000000000
30
31
github.com/redis/go-redis/v9 v9.0.5
31
32
go.opentelemetry.io/otel v1.30.0
32
33
go.opentelemetry.io/otel/sdk v1.30.0
@@ -42,7 +43,7 @@ possible.
42
43
### 3. Write verification code
43
44
44
45
Some telemetry data(like span) will be produced if the business code you've written is matched to the rule. For example,
45
- there should be two spans in ` test_executing_commands.go ` , one represents for the ` set ` redis operation and the other
46
+ there should be two spans produced by ` test_executing_commands.go ` , one represents for the ` set ` redis operation and the other
46
47
represents for the ` get ` redis operation. You should use the ` verifier ` to verify the correctness:
47
48
48
49
``` go
@@ -57,9 +58,37 @@ verifier.WaitAndAssertTraces(func (stubs []tracetest.SpanStubs) {
57
58
The ` WaitAndAssertTraces ` of the verifier accept a callback function, which provides all the traces that are produced.
58
59
You should verify the attribute, the parent context and all other key information of every span in all the traces.
59
60
60
- ### 4. Register your tests
61
+ If you want to verify the metrics data, you can also use the ` verifier ` like the following code:
62
+ ``` go
63
+ verifier.WaitAndAssertMetrics (map [string ]func (metricdata.ResourceMetrics ) {
64
+ " http.server.request.duration" : func (mrs metricdata.ResourceMetrics ) {
65
+ if len (mrs.ScopeMetrics ) <= 0 {
66
+ panic (" No http.server.request.duration metrics received!" )
67
+ }
68
+ point := mrs.ScopeMetrics [0 ].Metrics [0 ].Data .(metricdata.Histogram [float64 ])
69
+ if point.DataPoints [0 ].Count != 1 {
70
+ panic (" http.server.request.duration metrics count is not 1" )
71
+ }
72
+ verifier.VerifyHttpServerMetricsAttributes (point.DataPoints [0 ].Attributes .ToSlice (), " GET" , " /a" , " " , " http" , " 1.1" , " http" , 200 )
73
+ },
74
+ " http.client.request.duration" : func (mrs metricdata.ResourceMetrics ) {
75
+ if len (mrs.ScopeMetrics ) <= 0 {
76
+ panic (" No http.client.request.duration metrics received!" )
77
+ }
78
+ point := mrs.ScopeMetrics [0 ].Metrics [0 ].Data .(metricdata.Histogram [float64 ])
79
+ if point.DataPoints [0 ].Count != 1 {
80
+ panic (" http.client.request.duration metrics count is not 1" )
81
+ }
82
+ verifier.VerifyHttpClientMetricsAttributes (point.DataPoints [0 ].Attributes .ToSlice (), " GET" , " 127.0.0.1:" +strconv.Itoa (port), " " , " http" , " 1.1" , port, 200 )
83
+ },
84
+ })
85
+ ```
86
+ Users need to use ` WaitAndAssertMetrics ` method in verifier to verify the correctness of the metrics data. ` WaitAndAssertMetrics ` receives a map,
87
+ the key of the map is the name of the metric, the value is the validation function for this metrics data. Users can write their own validation logic in the callback function.
61
88
62
- Finally, you should register the test. You should write a ` _tests.go ` file in ` test ` directory to do the registration:
89
+ ### 4. Register tests
90
+
91
+ Finally, you should register the tests. You should write a ` _tests.go ` file in ` test ` directory to do the registration:
63
92
64
93
``` go
65
94
const redis_dependency_name = " github.com/redis/go-redis/v9"
@@ -80,7 +109,7 @@ func TestExecutingCommands(t *testing.T, env ...string) {
80
109
81
110
```
82
111
83
- In ` init ` function, you need to wrap your test case with ` NewGeneralTestCase ` , ` NewGeneralTestCase ` receives the
112
+ In the ` init ` function, you need to wrap your test case with ` NewGeneralTestCase ` , which receives the
84
113
following arguments:
85
114
86
115
testName, moduleName, minVersion, maxVersion, minGoVersion, maxGoVersion string, testFunc func(t * testing.T, env
@@ -96,11 +125,11 @@ testName, moduleName, minVersion, maxVersion, minGoVersion, maxGoVersion string,
96
125
97
126
You should build the test case with the ` opentelemetry-go-auto-instrumentation ` to make your test case able to produce
98
127
telemetry data. Firstly you should call ` UseApp ` method to change directory to the directory of your test cases, and
99
- then call ` RunInstrument ` to do hybrid compilation. Finally use the ` RunApp ` to run the instrumented test-case binary to
128
+ then call ` RunInstrument ` to do hybrid compilation. Finally, use the ` RunApp ` to run the instrumented test-case binary to
100
129
verify the telemetry data.
101
130
102
131
``` go
103
- func TestExecutingUnsupporetedCommands (t *testing .T , env ...string ) {
132
+ func TestExecutingUnsupportedCommands (t *testing .T , env ...string ) {
104
133
redisC , redisPort := initRedisContainer ()
105
134
defer clearRedisContainer (redisC)
106
135
UseApp (" redis/v9.0.5" )
@@ -132,6 +161,5 @@ the latest version of the library, as fetched from Maven, as part of a nightly b
132
161
not work with the agent, we find out through this build and can address it by the next release of the agent.
133
162
134
163
Users can add a muzzle check case by calling ` NewLatestDepthTestCase ` , the arguments taken by ` NewLatestDepthTestCase `
135
- are almost
136
- the same as ` NewGeneralTestCase ` . You need to additionally specify the dependency name of the plugin and the list of
164
+ are almost the same as ` NewGeneralTestCase ` . You need to additionally specify the dependency name of the plugin and the list of
137
165
classes that need to do the latest-depth check.
0 commit comments