Skip to content

Commit 2300ef5

Browse files
authored
polish document (#178)
1 parent f6f0da4 commit 2300ef5

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

docs/how-to-write-tests-for-plugins.md

+39-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ version of the plugin. If you want to add tests for redis, you should do the fol
1414
### 1. Add lowest-version dependency for your rule
1515

1616
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`:
1918

2019
```
2120
module redis/v9.0.5
@@ -24,9 +23,11 @@ go 1.22
2423
2524
replace github.com/alibaba/opentelemetry-go-auto-instrumentation => ../../../../opentelemetry-go-auto-instrumentation
2625
26+
replace github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier => ../../../../opentelemetry-go-auto-instrumentation/test/verifier
27+
2728
require (
2829
// 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
3031
github.com/redis/go-redis/v9 v9.0.5
3132
go.opentelemetry.io/otel v1.30.0
3233
go.opentelemetry.io/otel/sdk v1.30.0
@@ -42,7 +43,7 @@ possible.
4243
### 3. Write verification code
4344

4445
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
4647
represents for the `get` redis operation. You should use the `verifier` to verify the correctness:
4748

4849
```go
@@ -57,9 +58,37 @@ verifier.WaitAndAssertTraces(func (stubs []tracetest.SpanStubs) {
5758
The `WaitAndAssertTraces` of the verifier accept a callback function, which provides all the traces that are produced.
5859
You should verify the attribute, the parent context and all other key information of every span in all the traces.
5960

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.
6188

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:
6392

6493
```go
6594
const redis_dependency_name = "github.com/redis/go-redis/v9"
@@ -80,7 +109,7 @@ func TestExecutingCommands(t *testing.T, env ...string) {
80109

81110
```
82111

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
84113
following arguments:
85114

86115
testName, moduleName, minVersion, maxVersion, minGoVersion, maxGoVersion string, testFunc func(t *testing.T, env
@@ -96,11 +125,11 @@ testName, moduleName, minVersion, maxVersion, minGoVersion, maxGoVersion string,
96125

97126
You should build the test case with the `opentelemetry-go-auto-instrumentation` to make your test case able to produce
98127
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
100129
verify the telemetry data.
101130

102131
```go
103-
func TestExecutingUnsupporetedCommands(t *testing.T, env ...string) {
132+
func TestExecutingUnsupportedCommands(t *testing.T, env ...string) {
104133
redisC, redisPort := initRedisContainer()
105134
defer clearRedisContainer(redisC)
106135
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
132161
not work with the agent, we find out through this build and can address it by the next release of the agent.
133162

134163
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
137165
classes that need to do the latest-depth check.

0 commit comments

Comments
 (0)