Codebase list golang-github-go-kit-kit / af43370
refactor(tracing.opentracing): options improvements - some renaming - more flexible tags option: support adding tags with multiple options Alexander Babai 3 years ago
2 changed file(s) with 87 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
4343 }
4444 }
4545
46 // WithOperationName allows to set function that can set the span operation name based on the existing one
46 // WithOperationNameFunc allows to set function that can set the span operation name based on the existing one
4747 // for the endpoint and information in the context.
48 func WithOperationName(getOperationName func(ctx context.Context, name string) string) EndpointOption {
48 func WithOperationNameFunc(getOperationName func(ctx context.Context, name string) string) EndpointOption {
4949 return func(o *EndpointOptions) {
5050 o.GetOperationName = getOperationName
5151 }
5252 }
5353
54 // WithTags sets the default tags for the spans created by the Endpoint tracer.
54 // WithTags adds default tags for the spans created by the Endpoint tracer.
5555 func WithTags(tags opentracing.Tags) EndpointOption {
5656 return func(o *EndpointOptions) {
57 o.Tags = tags
57 if o.Tags == nil {
58 o.Tags = make(opentracing.Tags)
59 }
60
61 for key, value := range tags {
62 o.Tags[key] = value
63 }
5864 }
5965 }
6066
61 // WithExtraTags extracts additional tags from the context.
62 func WithExtraTags(getTags func(ctx context.Context) opentracing.Tags) EndpointOption {
67 // WithTagsFunc set the func to extracts additional tags from the context.
68 func WithTagsFunc(getTags func(ctx context.Context) opentracing.Tags) EndpointOption {
6369 return func(o *EndpointOptions) {
6470 o.GetTags = getTags
6571 }
132132 })
133133 _, _ = tracedEndpoint(context.Background(), struct{}{})
134134
135 // span 5 with OperationName option
135 // span 5 with OperationNameFunc option
136136 mw = kitot.TraceServer(
137137 tracer,
138138 span5,
139 kitot.WithOperationName(func(ctx context.Context, name string) string {
139 kitot.WithOperationNameFunc(func(ctx context.Context, name string) string {
140140 return fmt.Sprintf("%s-%s", "new", name)
141141 }),
142142 )
149149 span6,
150150 kitot.WithTags(map[string]interface{}{
151151 "tag1": "tag1",
152 }),
153 )
154 tracedEndpoint = mw(endpoint.Nop)
155 _, _ = tracedEndpoint(context.Background(), struct{}{})
156
157 // span 7 with ExtraTags options
152 "tag2": "tag2",
153 }),
154 kitot.WithTags(map[string]interface{}{
155 "tag3": "tag3",
156 }),
157 )
158 tracedEndpoint = mw(endpoint.Nop)
159 _, _ = tracedEndpoint(context.Background(), struct{}{})
160
161 // span 7 with TagsFunc options
158162 mw = kitot.TraceServer(
159163 tracer,
160164 span7,
161165 kitot.WithTags(map[string]interface{}{
162166 "tag1": "tag1",
163 }),
164 kitot.WithExtraTags(func(ctx context.Context) opentracing.Tags {
167 "tag2": "tag2",
168 }),
169 kitot.WithTags(map[string]interface{}{
170 "tag3": "tag3",
171 }),
172 kitot.WithTagsFunc(func(ctx context.Context) opentracing.Tags {
165173 return map[string]interface{}{
166 "tag2": "tag2",
174 "tag4": "tag4",
167175 }
168176 }),
169177 )
226234 span = finishedSpans[5]
227235
228236 if want, have := span6, span.OperationName; want != have {
229 t.Fatalf("Want %q, have %q", want, have)
230 }
231
232 if want, have := map[string]interface{}{
233 "span.kind": ext.SpanKindRPCServerEnum,
234 "tag1": "tag1",
235 }, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
236 t.Fatalf("Want %q, have %q", want, have)
237 }
238
239 // test span 7
240 span = finishedSpans[6]
241
242 if want, have := span7, span.OperationName; want != have {
243237 t.Fatalf("Want %q, have %q", want, have)
244238 }
245239
247241 "span.kind": ext.SpanKindRPCServerEnum,
248242 "tag1": "tag1",
249243 "tag2": "tag2",
244 "tag3": "tag3",
245 }, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
246 t.Fatalf("Want %q, have %q", want, have)
247 }
248
249 // test span 7
250 span = finishedSpans[6]
251
252 if want, have := span7, span.OperationName; want != have {
253 t.Fatalf("Want %q, have %q", want, have)
254 }
255
256 if want, have := map[string]interface{}{
257 "span.kind": ext.SpanKindRPCServerEnum,
258 "tag1": "tag1",
259 "tag2": "tag2",
260 "tag3": "tag3",
261 "tag4": "tag4",
250262 }, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
251263 t.Fatalf("Want %q, have %q", want, have)
252264 }
347359 })
348360 _, _ = tracedEndpoint(context.Background(), struct{}{})
349361
350 // span 5 with OperationName option
362 // span 5 with OperationNameFunc option
351363 mw = kitot.TraceClient(
352364 tracer,
353365 span5,
354 kitot.WithOperationName(func(ctx context.Context, name string) string {
366 kitot.WithOperationNameFunc(func(ctx context.Context, name string) string {
355367 return fmt.Sprintf("%s-%s", "new", name)
356368 }),
357369 )
364376 span6,
365377 kitot.WithTags(map[string]interface{}{
366378 "tag1": "tag1",
367 }),
368 )
369 tracedEndpoint = mw(endpoint.Nop)
370 _, _ = tracedEndpoint(context.Background(), struct{}{})
371
372 // span 7 with ExtraTags options
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
373389 mw = kitot.TraceClient(
374390 tracer,
375391 span7,
376392 kitot.WithTags(map[string]interface{}{
377393 "tag1": "tag1",
378 }),
379 kitot.WithExtraTags(func(ctx context.Context) opentracing.Tags {
394 "tag2": "tag2",
395 }),
396 kitot.WithTags(map[string]interface{}{
397 "tag3": "tag3",
398 }),
399 kitot.WithTagsFunc(func(ctx context.Context) opentracing.Tags {
380400 return map[string]interface{}{
381 "tag2": "tag2",
401 "tag4": "tag4",
382402 }
383403 }),
384404 )
441461 span = finishedSpans[5]
442462
443463 if want, have := span6, span.OperationName; want != have {
444 t.Fatalf("Want %q, have %q", want, have)
445 }
446
447 if want, have := map[string]interface{}{
448 "span.kind": ext.SpanKindRPCClientEnum,
449 "tag1": "tag1",
450 }, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
451 t.Fatalf("Want %q, have %q", want, have)
452 }
453
454 // test span 7
455 span = finishedSpans[6]
456
457 if want, have := span7, span.OperationName; want != have {
458464 t.Fatalf("Want %q, have %q", want, have)
459465 }
460466
462468 "span.kind": ext.SpanKindRPCClientEnum,
463469 "tag1": "tag1",
464470 "tag2": "tag2",
471 "tag3": "tag3",
465472 }, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
466473 t.Fatalf("Want %q, have %q", want, have)
467474 }
468 }
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 }