40 | 40 |
return r.err
|
41 | 41 |
}
|
42 | 42 |
|
43 | |
func TestTraceServer(t *testing.T) {
|
44 | |
tracer := mocktracer.New()
|
45 | |
|
46 | |
// Initialize the ctx with a nameless Span.
|
47 | |
contextSpan := tracer.StartSpan("").(*mocktracer.MockSpan)
|
48 | |
ctx := opentracing.ContextWithSpan(context.Background(), contextSpan)
|
49 | |
|
50 | |
tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
|
|
43 |
func TestTraceEndpoint(t *testing.T) {
|
|
44 |
tracer := mocktracer.New()
|
|
45 |
|
|
46 |
// Initialize the ctx with a parent Span.
|
|
47 |
parentSpan := tracer.StartSpan("parent").(*mocktracer.MockSpan)
|
|
48 |
defer parentSpan.Finish()
|
|
49 |
ctx := opentracing.ContextWithSpan(context.Background(), parentSpan)
|
|
50 |
|
|
51 |
tracedEndpoint := kitot.TraceEndpoint(tracer, "testOp")(endpoint.Nop)
|
51 | 52 |
if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
|
52 | 53 |
t.Fatal(err)
|
53 | 54 |
}
|
54 | 55 |
|
|
56 |
// tracedEndpoint created a new Span.
|
55 | 57 |
finishedSpans := tracer.FinishedSpans()
|
56 | 58 |
if want, have := 1, len(finishedSpans); want != have {
|
57 | 59 |
t.Fatalf("Want %v span(s), found %v", want, have)
|
58 | 60 |
}
|
59 | 61 |
|
60 | |
// Test that the op name is updated
|
61 | 62 |
endpointSpan := finishedSpans[0]
|
62 | 63 |
if want, have := "testOp", endpointSpan.OperationName; want != have {
|
63 | 64 |
t.Fatalf("Want %q, have %q", want, have)
|
64 | 65 |
}
|
65 | |
contextContext := contextSpan.Context().(mocktracer.MockSpanContext)
|
66 | |
endpointContext := endpointSpan.Context().(mocktracer.MockSpanContext)
|
67 | |
// ...and that the ID is unmodified.
|
68 | |
if want, have := contextContext.SpanID, endpointContext.SpanID; want != have {
|
69 | |
t.Errorf("Want SpanID %q, have %q", want, have)
|
70 | |
}
|
71 | |
}
|
72 | |
|
73 | |
func TestTraceServerNoContextSpan(t *testing.T) {
|
|
66 |
|
|
67 |
parentContext := parentSpan.Context().(mocktracer.MockSpanContext)
|
|
68 |
endpointContext := parentSpan.Context().(mocktracer.MockSpanContext)
|
|
69 |
|
|
70 |
// ... and that the parent ID is set appropriately.
|
|
71 |
if want, have := parentContext.SpanID, endpointContext.SpanID; want != have {
|
|
72 |
t.Errorf("Want ParentID %q, have %q", want, have)
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
func TestTraceEndpointNoContextSpan(t *testing.T) {
|
74 | 77 |
tracer := mocktracer.New()
|
75 | 78 |
|
76 | 79 |
// Empty/background context.
|
77 | |
tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
|
|
80 |
tracedEndpoint := kitot.TraceEndpoint(tracer, "testOp")(endpoint.Nop)
|
78 | 81 |
if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
|
79 | 82 |
t.Fatal(err)
|
80 | 83 |
}
|
|
86 | 89 |
}
|
87 | 90 |
|
88 | 91 |
endpointSpan := finishedSpans[0]
|
|
92 |
|
89 | 93 |
if want, have := "testOp", endpointSpan.OperationName; want != have {
|
90 | 94 |
t.Fatalf("Want %q, have %q", want, have)
|
91 | 95 |
}
|
92 | 96 |
}
|
93 | 97 |
|
94 | |
func TestTraceServerWithOptions(t *testing.T) {
|
|
98 |
func TestTraceEndpointWithOptions(t *testing.T) {
|
95 | 99 |
tracer := mocktracer.New()
|
96 | 100 |
|
97 | 101 |
// span 1 without options
|
98 | |
mw := kitot.TraceServer(tracer, span1)
|
|
102 |
mw := kitot.TraceEndpoint(tracer, span1)
|
99 | 103 |
tracedEndpoint := mw(endpoint.Nop)
|
100 | 104 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
101 | 105 |
|
102 | 106 |
// span 2 with options
|
103 | |
mw = kitot.TraceServer(
|
|
107 |
mw = kitot.TraceEndpoint(
|
104 | 108 |
tracer,
|
105 | 109 |
span2,
|
106 | 110 |
kitot.WithOptions(kitot.EndpointOptions{}),
|
|
111 | 115 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
112 | 116 |
|
113 | 117 |
// span 3 with disabled IgnoreBusinessError option
|
114 | |
mw = kitot.TraceServer(
|
|
118 |
mw = kitot.TraceEndpoint(
|
115 | 119 |
tracer,
|
116 | 120 |
span3,
|
117 | 121 |
kitot.WithIgnoreBusinessError(false),
|
|
124 | 128 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
125 | 129 |
|
126 | 130 |
// span 4 with enabled IgnoreBusinessError option
|
127 | |
mw = kitot.TraceServer(tracer, span4, kitot.WithIgnoreBusinessError(true))
|
|
131 |
mw = kitot.TraceEndpoint(tracer, span4, kitot.WithIgnoreBusinessError(true))
|
128 | 132 |
tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
|
129 | 133 |
return failedResponse{
|
130 | 134 |
err: err3,
|
|
133 | 137 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
134 | 138 |
|
135 | 139 |
// span 5 with OperationNameFunc option
|
136 | |
mw = kitot.TraceServer(
|
|
140 |
mw = kitot.TraceEndpoint(
|
137 | 141 |
tracer,
|
138 | 142 |
span5,
|
139 | 143 |
kitot.WithOperationNameFunc(func(ctx context.Context, name string) string {
|
|
144 | 148 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
145 | 149 |
|
146 | 150 |
// span 6 with Tags options
|
147 | |
mw = kitot.TraceServer(
|
|
151 |
mw = kitot.TraceEndpoint(
|
148 | 152 |
tracer,
|
149 | 153 |
span6,
|
150 | 154 |
kitot.WithTags(map[string]interface{}{
|
|
159 | 163 |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
160 | 164 |
|
161 | 165 |
// span 7 with TagsFunc options
|
162 | |
mw = kitot.TraceServer(
|
|
166 |
mw = kitot.TraceEndpoint(
|
163 | 167 |
tracer,
|
164 | 168 |
span7,
|
165 | 169 |
kitot.WithTags(map[string]interface{}{
|
|
238 | 242 |
}
|
239 | 243 |
|
240 | 244 |
if want, have := map[string]interface{}{
|
241 | |
"span.kind": ext.SpanKindRPCServerEnum,
|
242 | |
"tag1": "tag1",
|
243 | |
"tag2": "tag2",
|
244 | |
"tag3": "tag3",
|
|
245 |
"tag1": "tag1",
|
|
246 |
"tag2": "tag2",
|
|
247 |
"tag3": "tag3",
|
245 | 248 |
}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
|
246 | 249 |
t.Fatalf("Want %q, have %q", want, have)
|
247 | 250 |
}
|
|
254 | 257 |
}
|
255 | 258 |
|
256 | 259 |
if want, have := map[string]interface{}{
|
257 | |
"span.kind": ext.SpanKindRPCServerEnum,
|
258 | |
"tag1": "tag1",
|
259 | |
"tag2": "tag2",
|
260 | |
"tag3": "tag3",
|
261 | |
"tag4": "tag4",
|
|
260 |
"tag1": "tag1",
|
|
261 |
"tag2": "tag2",
|
|
262 |
"tag3": "tag3",
|
|
263 |
"tag4": "tag4",
|
262 | 264 |
}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
|
263 | 265 |
t.Fatalf("Want %q, have %q", want, have)
|
264 | 266 |
}
|
265 | 267 |
}
|
266 | 268 |
|
|
269 |
func TestTraceServer(t *testing.T) {
|
|
270 |
tracer := mocktracer.New()
|
|
271 |
|
|
272 |
// Empty/background context.
|
|
273 |
tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
|
|
274 |
if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
|
|
275 |
t.Fatal(err)
|
|
276 |
}
|
|
277 |
|
|
278 |
// tracedEndpoint created a new Span.
|
|
279 |
finishedSpans := tracer.FinishedSpans()
|
|
280 |
if want, have := 1, len(finishedSpans); want != have {
|
|
281 |
t.Fatalf("Want %v span(s), found %v", want, have)
|
|
282 |
}
|
|
283 |
|
|
284 |
span := finishedSpans[0]
|
|
285 |
|
|
286 |
if want, have := "testOp", span.OperationName; want != have {
|
|
287 |
t.Fatalf("Want %q, have %q", want, have)
|
|
288 |
}
|
|
289 |
|
|
290 |
if want, have := map[string]interface{}{
|
|
291 |
ext.SpanKindRPCServer.Key: ext.SpanKindRPCServer.Value,
|
|
292 |
}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
|
|
293 |
t.Fatalf("Want %q, have %q", want, have)
|
|
294 |
}
|
|
295 |
}
|
|
296 |
|
267 | 297 |
func TestTraceClient(t *testing.T) {
|
268 | |
tracer := mocktracer.New()
|
269 | |
|
270 | |
// Initialize the ctx with a parent Span.
|
271 | |
parentSpan := tracer.StartSpan("parent").(*mocktracer.MockSpan)
|
272 | |
defer parentSpan.Finish()
|
273 | |
ctx := opentracing.ContextWithSpan(context.Background(), parentSpan)
|
274 | |
|
275 | |
tracedEndpoint := kitot.TraceClient(tracer, "testOp")(endpoint.Nop)
|
276 | |
if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
|
277 | |
t.Fatal(err)
|
278 | |
}
|
279 | |
|
280 | |
// tracedEndpoint created a new Span.
|
281 | |
finishedSpans := tracer.FinishedSpans()
|
282 | |
if want, have := 1, len(finishedSpans); want != have {
|
283 | |
t.Fatalf("Want %v span(s), found %v", want, have)
|
284 | |
}
|
285 | |
|
286 | |
endpointSpan := finishedSpans[0]
|
287 | |
if want, have := "testOp", endpointSpan.OperationName; want != have {
|
288 | |
t.Fatalf("Want %q, have %q", want, have)
|
289 | |
}
|
290 | |
|
291 | |
parentContext := parentSpan.Context().(mocktracer.MockSpanContext)
|
292 | |
endpointContext := parentSpan.Context().(mocktracer.MockSpanContext)
|
293 | |
|
294 | |
// ... and that the parent ID is set appropriately.
|
295 | |
if want, have := parentContext.SpanID, endpointContext.SpanID; want != have {
|
296 | |
t.Errorf("Want ParentID %q, have %q", want, have)
|
297 | |
}
|
298 | |
}
|
299 | |
|
300 | |
func TestTraceClientNoContextSpan(t *testing.T) {
|
301 | 298 |
tracer := mocktracer.New()
|
302 | 299 |
|
303 | 300 |
// Empty/background context.
|
|
312 | 309 |
t.Fatalf("Want %v span(s), found %v", want, have)
|
313 | 310 |
}
|
314 | 311 |
|
315 | |
endpointSpan := finishedSpans[0]
|
316 | |
if want, have := "testOp", endpointSpan.OperationName; want != have {
|
317 | |
t.Fatalf("Want %q, have %q", want, have)
|
318 | |
}
|
319 | |
}
|
320 | |
|
321 | |
func TestTraceClientWithOptions(t *testing.T) {
|
322 | |
tracer := mocktracer.New()
|
323 | |
|
324 | |
// span 1 without options
|
325 | |
mw := kitot.TraceClient(tracer, span1)
|
326 | |
tracedEndpoint := mw(endpoint.Nop)
|
327 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
328 | |
|
329 | |
// span 2 with options
|
330 | |
mw = kitot.TraceClient(
|
331 | |
tracer,
|
332 | |
span2,
|
333 | |
kitot.WithOptions(kitot.EndpointOptions{}),
|
334 | |
)
|
335 | |
tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
|
336 | |
return nil, err1
|
337 | |
})
|
338 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
339 | |
|
340 | |
// span 3 with disabled IgnoreBusinessError option
|
341 | |
mw = kitot.TraceClient(
|
342 | |
tracer,
|
343 | |
span3,
|
344 | |
kitot.WithIgnoreBusinessError(false),
|
345 | |
)
|
346 | |
tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
|
347 | |
return failedResponse{
|
348 | |
err: err2,
|
349 | |
}, nil
|
350 | |
})
|
351 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
352 | |
|
353 | |
// span 4 with enabled IgnoreBusinessError option
|
354 | |
mw = kitot.TraceClient(tracer, span4, kitot.WithIgnoreBusinessError(true))
|
355 | |
tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
|
356 | |
return failedResponse{
|
357 | |
err: err3,
|
358 | |
}, nil
|
359 | |
})
|
360 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
361 | |
|
362 | |
// span 5 with OperationNameFunc option
|
363 | |
mw = kitot.TraceClient(
|
364 | |
tracer,
|
365 | |
span5,
|
366 | |
kitot.WithOperationNameFunc(func(ctx context.Context, name string) string {
|
367 | |
return fmt.Sprintf("%s-%s", "new", name)
|
368 | |
}),
|
369 | |
)
|
370 | |
tracedEndpoint = mw(endpoint.Nop)
|
371 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
372 | |
|
373 | |
// span 6 with Tags options
|
374 | |
mw = kitot.TraceClient(
|
375 | |
tracer,
|
376 | |
span6,
|
377 | |
kitot.WithTags(map[string]interface{}{
|
378 | |
"tag1": "tag1",
|
379 | |
"tag2": "tag2",
|
380 | |
}),
|
381 | |
kitot.WithTags(map[string]interface{}{
|
382 | |
"tag3": "tag3",
|
383 | |
}),
|
384 | |
)
|
385 | |
tracedEndpoint = mw(endpoint.Nop)
|
386 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
387 | |
|
388 | |
// span 7 with TagsFunc options
|
389 | |
mw = kitot.TraceClient(
|
390 | |
tracer,
|
391 | |
span7,
|
392 | |
kitot.WithTags(map[string]interface{}{
|
393 | |
"tag1": "tag1",
|
394 | |
"tag2": "tag2",
|
395 | |
}),
|
396 | |
kitot.WithTags(map[string]interface{}{
|
397 | |
"tag3": "tag3",
|
398 | |
}),
|
399 | |
kitot.WithTagsFunc(func(ctx context.Context) opentracing.Tags {
|
400 | |
return map[string]interface{}{
|
401 | |
"tag4": "tag4",
|
402 | |
}
|
403 | |
}),
|
404 | |
)
|
405 | |
tracedEndpoint = mw(endpoint.Nop)
|
406 | |
_, _ = tracedEndpoint(context.Background(), struct{}{})
|
407 | |
|
408 | |
finishedSpans := tracer.FinishedSpans()
|
409 | |
if want, have := 7, len(finishedSpans); want != have {
|
410 | |
t.Fatalf("Want %v span(s), found %v", want, have)
|
411 | |
}
|
412 | |
|
413 | |
// test span 1
|
414 | 312 |
span := finishedSpans[0]
|
415 | 313 |
|
416 | |
if want, have := span1, span.OperationName; want != have {
|
417 | |
t.Fatalf("Want %q, have %q", want, have)
|
418 | |
}
|
419 | |
|
420 | |
// test span 2
|
421 | |
span = finishedSpans[1]
|
422 | |
|
423 | |
if want, have := span2, span.OperationName; want != have {
|
424 | |
t.Fatalf("Want %q, have %q", want, have)
|
425 | |
}
|
426 | |
|
427 | |
if want, have := true, span.Tag("error"); want != have {
|
428 | |
t.Fatalf("Want %v, have %v", want, have)
|
429 | |
}
|
430 | |
|
431 | |
// test span 3
|
432 | |
span = finishedSpans[2]
|
433 | |
|
434 | |
if want, have := span3, span.OperationName; want != have {
|
435 | |
t.Fatalf("Want %q, have %q", want, have)
|
436 | |
}
|
437 | |
|
438 | |
if want, have := true, span.Tag("error"); want != have {
|
439 | |
t.Fatalf("Want %v, have %v", want, have)
|
440 | |
}
|
441 | |
|
442 | |
// test span 4
|
443 | |
span = finishedSpans[3]
|
444 | |
|
445 | |
if want, have := span4, span.OperationName; want != have {
|
446 | |
t.Fatalf("Want %q, have %q", want, have)
|
447 | |
}
|
448 | |
|
449 | |
if want, have := (interface{})(nil), span.Tag("error"); want != have {
|
450 | |
t.Fatalf("Want %q, have %q", want, have)
|
451 | |
}
|
452 | |
|
453 | |
// test span 5
|
454 | |
span = finishedSpans[4]
|
455 | |
|
456 | |
if want, have := fmt.Sprintf("%s-%s", "new", span5), span.OperationName; want != have {
|
457 | |
t.Fatalf("Want %q, have %q", want, have)
|
458 | |
}
|
459 | |
|
460 | |
// test span 6
|
461 | |
span = finishedSpans[5]
|
462 | |
|
463 | |
if want, have := span6, span.OperationName; want != have {
|
|
314 |
if want, have := "testOp", span.OperationName; want != have {
|
464 | 315 |
t.Fatalf("Want %q, have %q", want, have)
|
465 | 316 |
}
|
466 | 317 |
|
467 | 318 |
if want, have := map[string]interface{}{
|
468 | |
"span.kind": ext.SpanKindRPCClientEnum,
|
469 | |
"tag1": "tag1",
|
470 | |
"tag2": "tag2",
|
471 | |
"tag3": "tag3",
|
|
319 |
ext.SpanKindRPCClient.Key: ext.SpanKindRPCClient.Value,
|
472 | 320 |
}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
|
473 | 321 |
t.Fatalf("Want %q, have %q", want, have)
|
474 | 322 |
}
|
475 | |
|
476 | |
// test span 7
|
477 | |
span = finishedSpans[6]
|
478 | |
|
479 | |
if want, have := span7, span.OperationName; want != have {
|
480 | |
t.Fatalf("Want %q, have %q", want, have)
|
481 | |
}
|
482 | |
|
483 | |
if want, have := map[string]interface{}{
|
484 | |
"span.kind": ext.SpanKindRPCClientEnum,
|
485 | |
"tag1": "tag1",
|
486 | |
"tag2": "tag2",
|
487 | |
"tag3": "tag3",
|
488 | |
"tag4": "tag4",
|
489 | |
}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
|
490 | |
t.Fatalf("Want %q, have %q", want, have)
|
491 | |
}
|
492 | |
}
|
|
323 |
}
|