3 | 3 |
"context"
|
4 | 4 |
"errors"
|
5 | 5 |
"fmt"
|
|
6 |
"reflect"
|
6 | 7 |
"testing"
|
|
8 |
"time"
|
7 | 9 |
|
8 | 10 |
"github.com/opentracing/opentracing-go"
|
9 | |
"github.com/opentracing/opentracing-go/ext"
|
|
11 |
otext "github.com/opentracing/opentracing-go/ext"
|
|
12 |
otlog "github.com/opentracing/opentracing-go/log"
|
10 | 13 |
"github.com/opentracing/opentracing-go/mocktracer"
|
11 | 14 |
|
12 | 15 |
"github.com/go-kit/kit/endpoint"
|
|
16 |
"github.com/go-kit/kit/sd"
|
|
17 |
"github.com/go-kit/kit/sd/lb"
|
13 | 18 |
kitot "github.com/go-kit/kit/tracing/opentracing"
|
14 | 19 |
)
|
15 | 20 |
|
|
21 | 26 |
span5 = "SPAN-5"
|
22 | 27 |
span6 = "SPAN-6"
|
23 | 28 |
span7 = "SPAN-7"
|
|
29 |
span8 = "SPAN-8"
|
24 | 30 |
)
|
25 | 31 |
|
26 | 32 |
var (
|
|
114 | 120 |
})
|
115 | 121 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
116 | 122 |
|
117 | |
// span 3 with disabled IgnoreBusinessError option
|
|
123 |
// span 3 with lb error
|
118 | 124 |
mw = kitot.TraceEndpoint(
|
119 | 125 |
tracer,
|
120 | 126 |
span3,
|
|
127 |
kitot.WithOptions(kitot.EndpointOptions{}),
|
|
128 |
)
|
|
129 |
tracedEndpoint = mw(
|
|
130 |
lb.Retry(
|
|
131 |
5,
|
|
132 |
1*time.Second,
|
|
133 |
lb.NewRoundRobin(
|
|
134 |
sd.FixedEndpointer{
|
|
135 |
func(context.Context, interface{}) (interface{}, error) {
|
|
136 |
return nil, err1
|
|
137 |
},
|
|
138 |
},
|
|
139 |
),
|
|
140 |
),
|
|
141 |
)
|
|
142 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
|
143 |
|
|
144 |
// span 4 with disabled IgnoreBusinessError option
|
|
145 |
mw = kitot.TraceEndpoint(
|
|
146 |
tracer,
|
|
147 |
span4,
|
121 | 148 |
kitot.WithIgnoreBusinessError(false),
|
122 | 149 |
)
|
123 | 150 |
tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
|
|
127 | 154 |
})
|
128 | 155 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
129 | 156 |
|
130 | |
// span 4 with enabled IgnoreBusinessError option
|
131 | |
mw = kitot.TraceEndpoint(tracer, span4, kitot.WithIgnoreBusinessError(true))
|
|
157 |
// span 5 with enabled IgnoreBusinessError option
|
|
158 |
mw = kitot.TraceEndpoint(tracer, span5, kitot.WithIgnoreBusinessError(true))
|
132 | 159 |
tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
|
133 | 160 |
return failedResponse{
|
134 | 161 |
err: err3,
|
|
136 | 163 |
})
|
137 | 164 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
138 | 165 |
|
139 | |
// span 5 with OperationNameFunc option
|
140 | |
mw = kitot.TraceEndpoint(
|
141 | |
tracer,
|
142 | |
span5,
|
|
166 |
// span 6 with OperationNameFunc option
|
|
167 |
mw = kitot.TraceEndpoint(
|
|
168 |
tracer,
|
|
169 |
span6,
|
143 | 170 |
kitot.WithOperationNameFunc(func(ctx context.Context, name string) string {
|
144 | 171 |
return fmt.Sprintf("%s-%s", "new", name)
|
145 | 172 |
}),
|
|
147 | 174 |
tracedEndpoint = mw(endpoint.Nop)
|
148 | 175 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
149 | 176 |
|
150 | |
// span 6 with Tags options
|
151 | |
mw = kitot.TraceEndpoint(
|
152 | |
tracer,
|
153 | |
span6,
|
154 | |
kitot.WithTags(map[string]interface{}{
|
155 | |
"tag1": "tag1",
|
156 | |
"tag2": "tag2",
|
157 | |
}),
|
158 | |
kitot.WithTags(map[string]interface{}{
|
159 | |
"tag3": "tag3",
|
160 | |
}),
|
161 | |
)
|
162 | |
tracedEndpoint = mw(endpoint.Nop)
|
163 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
164 | |
|
165 | |
// span 7 with TagsFunc options
|
|
177 |
// span 7 with Tags options
|
166 | 178 |
mw = kitot.TraceEndpoint(
|
167 | 179 |
tracer,
|
168 | 180 |
span7,
|
|
173 | 185 |
kitot.WithTags(map[string]interface{}{
|
174 | 186 |
"tag3": "tag3",
|
175 | 187 |
}),
|
|
188 |
)
|
|
189 |
tracedEndpoint = mw(endpoint.Nop)
|
|
190 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
|
191 |
|
|
192 |
// span 8 with TagsFunc options
|
|
193 |
mw = kitot.TraceEndpoint(
|
|
194 |
tracer,
|
|
195 |
span8,
|
|
196 |
kitot.WithTags(map[string]interface{}{
|
|
197 |
"tag1": "tag1",
|
|
198 |
"tag2": "tag2",
|
|
199 |
}),
|
|
200 |
kitot.WithTags(map[string]interface{}{
|
|
201 |
"tag3": "tag3",
|
|
202 |
}),
|
176 | 203 |
kitot.WithTagsFunc(func(ctx context.Context) opentracing.Tags {
|
177 | 204 |
return map[string]interface{}{
|
178 | 205 |
"tag4": "tag4",
|
|
183 | 210 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
184 | 211 |
|
185 | 212 |
finishedSpans := tracer.FinishedSpans()
|
186 | |
if want, have := 7, len(finishedSpans); want != have {
|
|
213 |
if want, have := 8, len(finishedSpans); want != have {
|
187 | 214 |
t.Fatalf("Want %v span(s), found %v", want, have)
|
188 | 215 |
}
|
189 | 216 |
|
|
216 | 243 |
t.Fatalf("Want %v, have %v", want, have)
|
217 | 244 |
}
|
218 | 245 |
|
|
246 |
if want, have := 1, len(span.Logs()); want != have {
|
|
247 |
t.Fatalf("incorrect logs count, wanted %d, got %d", want, have)
|
|
248 |
}
|
|
249 |
|
|
250 |
if want, have := []otlog.Field{
|
|
251 |
otlog.String("event", "error"),
|
|
252 |
otlog.String("error.object", "some error (previously: some error; some error; some error; some error)"),
|
|
253 |
otlog.String("gokit.retry.error.1", "some error"),
|
|
254 |
otlog.String("gokit.retry.error.2", "some error"),
|
|
255 |
otlog.String("gokit.retry.error.3", "some error"),
|
|
256 |
otlog.String("gokit.retry.error.4", "some error"),
|
|
257 |
otlog.String("gokit.retry.error.5", "some error"),
|
|
258 |
}, span.Logs()[0].Fields; reflect.DeepEqual(want, have) {
|
|
259 |
t.Fatalf("Want %q, have %q", want, have)
|
|
260 |
}
|
|
261 |
|
219 | 262 |
// test span 4
|
220 | 263 |
span = finishedSpans[3]
|
221 | 264 |
|
|
223 | 266 |
t.Fatalf("Want %q, have %q", want, have)
|
224 | 267 |
}
|
225 | 268 |
|
226 | |
if want, have := (interface{})(nil), span.Tag("error"); want != have {
|
|
269 |
if want, have := true, span.Tag("error"); want != have {
|
|
270 |
t.Fatalf("Want %v, have %v", want, have)
|
|
271 |
}
|
|
272 |
|
|
273 |
if want, have := 2, len(span.Logs()); want != have {
|
|
274 |
t.Fatalf("incorrect logs count, wanted %d, got %d", want, have)
|
|
275 |
}
|
|
276 |
|
|
277 |
if want, have := []otlog.Field{
|
|
278 |
otlog.String("gokit.business.error", "some business error"),
|
|
279 |
}, span.Logs()[0].Fields; reflect.DeepEqual(want, have) {
|
|
280 |
t.Fatalf("Want %q, have %q", want, have)
|
|
281 |
}
|
|
282 |
|
|
283 |
if want, have := []otlog.Field{
|
|
284 |
otlog.String("event", "error"),
|
|
285 |
otlog.String("error.object", "some business error"),
|
|
286 |
}, span.Logs()[1].Fields; reflect.DeepEqual(want, have) {
|
227 | 287 |
t.Fatalf("Want %q, have %q", want, have)
|
228 | 288 |
}
|
229 | 289 |
|
230 | 290 |
// test span 5
|
231 | 291 |
span = finishedSpans[4]
|
232 | 292 |
|
233 | |
if want, have := fmt.Sprintf("%s-%s", "new", span5), span.OperationName; want != have {
|
|
293 |
if want, have := span5, span.OperationName; want != have {
|
|
294 |
t.Fatalf("Want %q, have %q", want, have)
|
|
295 |
}
|
|
296 |
|
|
297 |
if want, have := (interface{})(nil), span.Tag("error"); want != have {
|
|
298 |
t.Fatalf("Want %q, have %q", want, have)
|
|
299 |
}
|
|
300 |
|
|
301 |
if want, have := 1, len(span.Logs()); want != have {
|
|
302 |
t.Fatalf("incorrect logs count, wanted %d, got %d", want, have)
|
|
303 |
}
|
|
304 |
|
|
305 |
if want, have := []otlog.Field{
|
|
306 |
otlog.String("gokit.business.error", "some business error"),
|
|
307 |
}, span.Logs()[0].Fields; reflect.DeepEqual(want, have) {
|
234 | 308 |
t.Fatalf("Want %q, have %q", want, have)
|
235 | 309 |
}
|
236 | 310 |
|
237 | 311 |
// test span 6
|
238 | 312 |
span = finishedSpans[5]
|
239 | 313 |
|
240 | |
if want, have := span6, span.OperationName; want != have {
|
|
314 |
if want, have := fmt.Sprintf("%s-%s", "new", span6), span.OperationName; want != have {
|
|
315 |
t.Fatalf("Want %q, have %q", want, have)
|
|
316 |
}
|
|
317 |
|
|
318 |
// test span 7
|
|
319 |
span = finishedSpans[6]
|
|
320 |
|
|
321 |
if want, have := span7, span.OperationName; want != have {
|
241 | 322 |
t.Fatalf("Want %q, have %q", want, have)
|
242 | 323 |
}
|
243 | 324 |
|
|
249 | 330 |
t.Fatalf("Want %q, have %q", want, have)
|
250 | 331 |
}
|
251 | 332 |
|
252 | |
// test span 7
|
253 | |
span = finishedSpans[6]
|
254 | |
|
255 | |
if want, have := span7, span.OperationName; want != have {
|
|
333 |
// test span 8
|
|
334 |
span = finishedSpans[7]
|
|
335 |
|
|
336 |
if want, have := span8, span.OperationName; want != have {
|
256 | 337 |
t.Fatalf("Want %q, have %q", want, have)
|
257 | 338 |
}
|
258 | 339 |
|
|
288 | 369 |
}
|
289 | 370 |
|
290 | 371 |
if want, have := map[string]interface{}{
|
291 | |
ext.SpanKindRPCServer.Key: ext.SpanKindRPCServer.Value,
|
|
372 |
otext.SpanKindRPCServer.Key: otext.SpanKindRPCServer.Value,
|
292 | 373 |
}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
|
293 | 374 |
t.Fatalf("Want %q, have %q", want, have)
|
294 | 375 |
}
|
|
316 | 397 |
}
|
317 | 398 |
|
318 | 399 |
if want, have := map[string]interface{}{
|
319 | |
ext.SpanKindRPCClient.Key: ext.SpanKindRPCClient.Value,
|
|
400 |
otext.SpanKindRPCClient.Key: otext.SpanKindRPCClient.Value,
|
320 | 401 |
}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
|
321 | 402 |
t.Fatalf("Want %q, have %q", want, have)
|
322 | 403 |
}
|