5
5
package ocagent
6
6
7
7
import (
8
+ "context"
8
9
"encoding/json"
9
10
"errors"
10
11
"reflect"
@@ -17,20 +18,23 @@ import (
17
18
func TestConvert_annotation (t * testing.T ) {
18
19
tests := []struct {
19
20
name string
20
- event telemetry.Event
21
+ event func ( ctx context. Context ) telemetry.Event
21
22
want string
22
23
}{
23
24
{
24
- name : "no tags" ,
25
- want : "null" ,
25
+ name : "no tags" ,
26
+ event : func (ctx context.Context ) telemetry.Event { return telemetry.Event {} },
27
+ want : "null" ,
26
28
},
27
29
{
28
30
name : "description no error" ,
29
- event : telemetry.Event {
30
- Message : "cache miss" ,
31
- Tags : telemetry.TagList {
32
- tag .Of ("db" , "godb" ),
33
- },
31
+ event : func (ctx context.Context ) telemetry.Event {
32
+ return telemetry.Event {
33
+ Message : "cache miss" ,
34
+ Tags : telemetry.TagList {
35
+ tag .Of ("db" , "godb" ),
36
+ },
37
+ }
34
38
},
35
39
want : `{
36
40
"description": {
@@ -50,12 +54,14 @@ func TestConvert_annotation(t *testing.T) {
50
54
51
55
{
52
56
name : "description and error" ,
53
- event : telemetry.Event {
54
- Message : "cache miss" ,
55
- Error : errors .New ("no network connectivity" ),
56
- Tags : telemetry.TagList {
57
- tag .Of ("db" , "godb" ),
58
- },
57
+ event : func (ctx context.Context ) telemetry.Event {
58
+ return telemetry.Event {
59
+ Message : "cache miss" ,
60
+ Error : errors .New ("no network connectivity" ),
61
+ Tags : telemetry.TagList {
62
+ tag .Of ("db" , "godb" ),
63
+ },
64
+ }
59
65
},
60
66
want : `{
61
67
"description": {
@@ -79,11 +85,13 @@ func TestConvert_annotation(t *testing.T) {
79
85
},
80
86
{
81
87
name : "no description, but error" ,
82
- event : telemetry.Event {
83
- Error : errors .New ("no network connectivity" ),
84
- Tags : telemetry.TagList {
85
- tag .Of ("db" , "godb" ),
86
- },
88
+ event : func (ctx context.Context ) telemetry.Event {
89
+ return telemetry.Event {
90
+ Error : errors .New ("no network connectivity" ),
91
+ Tags : telemetry.TagList {
92
+ tag .Of ("db" , "godb" ),
93
+ },
94
+ }
87
95
},
88
96
want : `{
89
97
"description": {
@@ -102,30 +110,32 @@ func TestConvert_annotation(t *testing.T) {
102
110
},
103
111
{
104
112
name : "enumerate all attribute types" ,
105
- event : telemetry.Event {
106
- Message : "cache miss" ,
107
- Tags : telemetry.TagList {
108
- tag .Of ("db" , "godb" ),
113
+ event : func (ctx context.Context ) telemetry.Event {
114
+ return telemetry.Event {
115
+ Message : "cache miss" ,
116
+ Tags : telemetry.TagList {
117
+ tag .Of ("db" , "godb" ),
109
118
110
- tag .Of ("age" , 0.456 ), // Constant converted into "float64"
111
- tag .Of ("ttl" , float32 (5000 )),
112
- tag .Of ("expiry_ms" , float64 (1e3 )),
119
+ tag .Of ("age" , 0.456 ), // Constant converted into "float64"
120
+ tag .Of ("ttl" , float32 (5000 )),
121
+ tag .Of ("expiry_ms" , float64 (1e3 )),
113
122
114
- tag .Of ("retry" , false ),
115
- tag .Of ("stale" , true ),
123
+ tag .Of ("retry" , false ),
124
+ tag .Of ("stale" , true ),
116
125
117
- tag .Of ("max" , 0x7fff ), // Constant converted into "int"
118
- tag .Of ("opcode" , int8 (0x7e )),
119
- tag .Of ("base" , int16 (1 << 9 )),
120
- tag .Of ("checksum" , int32 (0x11f7e294 )),
121
- tag .Of ("mode" , int64 (0644 )),
126
+ tag .Of ("max" , 0x7fff ), // Constant converted into "int"
127
+ tag .Of ("opcode" , int8 (0x7e )),
128
+ tag .Of ("base" , int16 (1 << 9 )),
129
+ tag .Of ("checksum" , int32 (0x11f7e294 )),
130
+ tag .Of ("mode" , int64 (0644 )),
122
131
123
- tag .Of ("min" , uint (1 )),
124
- tag .Of ("mix" , uint8 (44 )),
125
- tag .Of ("port" , uint16 (55678 )),
126
- tag .Of ("min_hops" , uint32 (1 << 9 )),
127
- tag .Of ("max_hops" , uint64 (0xffffff )),
128
- },
132
+ tag .Of ("min" , uint (1 )),
133
+ tag .Of ("mix" , uint8 (44 )),
134
+ tag .Of ("port" , uint16 (55678 )),
135
+ tag .Of ("min_hops" , uint32 (1 << 9 )),
136
+ tag .Of ("max_hops" , uint64 (0xffffff )),
137
+ },
138
+ }
129
139
},
130
140
want : `{
131
141
"description": {
@@ -186,10 +196,10 @@ func TestConvert_annotation(t *testing.T) {
186
196
}` ,
187
197
},
188
198
}
189
-
199
+ ctx := context . TODO ()
190
200
for _ , tt := range tests {
191
201
t .Run (tt .name , func (t * testing.T ) {
192
- got := marshaled (convertAnnotation (tt .event ))
202
+ got := marshaled (convertAnnotation (tt .event ( ctx ) ))
193
203
if ! reflect .DeepEqual (got , tt .want ) {
194
204
t .Fatalf ("Got:\n %s\n Want:\n %s" , got , tt .want )
195
205
}
0 commit comments