forked from DataDog/dd-trace-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textmap.go
980 lines (913 loc) · 31.4 KB
/
textmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package tracer
import (
"fmt"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"github.com/lannguyen-c0x12c/dd-trace-go/ddtrace"
"github.com/lannguyen-c0x12c/dd-trace-go/ddtrace/ext"
"github.com/lannguyen-c0x12c/dd-trace-go/internal/log"
"github.com/lannguyen-c0x12c/dd-trace-go/internal/samplernames"
)
// HTTPHeadersCarrier wraps an http.Header as a TextMapWriter and TextMapReader, allowing
// it to be used using the provided Propagator implementation.
type HTTPHeadersCarrier http.Header
var _ TextMapWriter = (*HTTPHeadersCarrier)(nil)
var _ TextMapReader = (*HTTPHeadersCarrier)(nil)
// Set implements TextMapWriter.
func (c HTTPHeadersCarrier) Set(key, val string) {
http.Header(c).Set(key, val)
}
// ForeachKey implements TextMapReader.
func (c HTTPHeadersCarrier) ForeachKey(handler func(key, val string) error) error {
for k, vals := range c {
for _, v := range vals {
if err := handler(k, v); err != nil {
return err
}
}
}
return nil
}
// TextMapCarrier allows the use of a regular map[string]string as both TextMapWriter
// and TextMapReader, making it compatible with the provided Propagator.
type TextMapCarrier map[string]string
var _ TextMapWriter = (*TextMapCarrier)(nil)
var _ TextMapReader = (*TextMapCarrier)(nil)
// Set implements TextMapWriter.
func (c TextMapCarrier) Set(key, val string) {
c[key] = val
}
// ForeachKey conforms to the TextMapReader interface.
func (c TextMapCarrier) ForeachKey(handler func(key, val string) error) error {
for k, v := range c {
if err := handler(k, v); err != nil {
return err
}
}
return nil
}
const (
headerPropagationStyleInject = "DD_TRACE_PROPAGATION_STYLE_INJECT"
headerPropagationStyleExtract = "DD_TRACE_PROPAGATION_STYLE_EXTRACT"
headerPropagationStyle = "DD_TRACE_PROPAGATION_STYLE"
headerPropagationStyleInjectDeprecated = "DD_PROPAGATION_STYLE_INJECT" // deprecated
headerPropagationStyleExtractDeprecated = "DD_PROPAGATION_STYLE_EXTRACT" // deprecated
)
const (
// DefaultBaggageHeaderPrefix specifies the prefix that will be used in
// HTTP headers or text maps to prefix baggage keys.
DefaultBaggageHeaderPrefix = "ot-baggage-"
// DefaultTraceIDHeader specifies the key that will be used in HTTP headers
// or text maps to store the trace ID.
DefaultTraceIDHeader = "x-datadog-trace-id"
// DefaultParentIDHeader specifies the key that will be used in HTTP headers
// or text maps to store the parent ID.
DefaultParentIDHeader = "x-datadog-parent-id"
// DefaultPriorityHeader specifies the key that will be used in HTTP headers
// or text maps to store the sampling priority value.
DefaultPriorityHeader = "x-datadog-sampling-priority"
)
// originHeader specifies the name of the header indicating the origin of the trace.
// It is used with the Synthetics product and usually has the value "synthetics".
const originHeader = "x-datadog-origin"
// traceTagsHeader holds the propagated trace tags
const traceTagsHeader = "x-datadog-tags"
// propagationExtractMaxSize limits the total size of incoming propagated tags to parse
const propagationExtractMaxSize = 512
// PropagatorConfig defines the configuration for initializing a propagator.
type PropagatorConfig struct {
// BaggagePrefix specifies the prefix that will be used to store baggage
// items in a map. It defaults to DefaultBaggageHeaderPrefix.
BaggagePrefix string
// TraceHeader specifies the map key that will be used to store the trace ID.
// It defaults to DefaultTraceIDHeader.
TraceHeader string
// ParentHeader specifies the map key that will be used to store the parent ID.
// It defaults to DefaultParentIDHeader.
ParentHeader string
// PriorityHeader specifies the map key that will be used to store the sampling priority.
// It defaults to DefaultPriorityHeader.
PriorityHeader string
// MaxTagsHeaderLen specifies the maximum length of trace tags header value.
// It defaults to defaultMaxTagsHeaderLen, a value of 0 disables propagation of tags.
MaxTagsHeaderLen int
// B3 specifies if B3 headers should be added for trace propagation.
// See https://github.com/openzipkin/b3-propagation
B3 bool
}
// NewPropagator returns a new propagator which uses TextMap to inject
// and extract values. It propagates trace and span IDs and baggage.
// To use the defaults, nil may be provided in place of the config.
//
// The inject and extract propagators are determined using environment variables
// with the following order of precedence:
// 1. DD_TRACE_PROPAGATION_STYLE_INJECT
// 2. DD_PROPAGATION_STYLE_INJECT (deprecated)
// 3. DD_TRACE_PROPAGATION_STYLE (applies to both inject and extract)
// 4. If none of the above, use default values
func NewPropagator(cfg *PropagatorConfig, propagators ...Propagator) Propagator {
if cfg == nil {
cfg = new(PropagatorConfig)
}
if cfg.BaggagePrefix == "" {
cfg.BaggagePrefix = DefaultBaggageHeaderPrefix
}
if cfg.TraceHeader == "" {
cfg.TraceHeader = DefaultTraceIDHeader
}
if cfg.ParentHeader == "" {
cfg.ParentHeader = DefaultParentIDHeader
}
if cfg.PriorityHeader == "" {
cfg.PriorityHeader = DefaultPriorityHeader
}
if len(propagators) > 0 {
return &chainedPropagator{
injectors: propagators,
extractors: propagators,
}
}
injectorsPs := os.Getenv(headerPropagationStyleInject)
if injectorsPs == "" {
if injectorsPs = os.Getenv(headerPropagationStyleInjectDeprecated); injectorsPs != "" {
log.Warn("%v is deprecated. Please use %v or %v instead.\n", headerPropagationStyleInjectDeprecated, headerPropagationStyleInject, headerPropagationStyle)
}
}
extractorsPs := os.Getenv(headerPropagationStyleExtract)
if extractorsPs == "" {
if extractorsPs = os.Getenv(headerPropagationStyleExtractDeprecated); extractorsPs != "" {
log.Warn("%v is deprecated. Please use %v or %v instead.\n", headerPropagationStyleExtractDeprecated, headerPropagationStyleExtract, headerPropagationStyle)
}
}
return &chainedPropagator{
injectors: getPropagators(cfg, injectorsPs),
extractors: getPropagators(cfg, extractorsPs),
}
}
// chainedPropagator implements Propagator and applies a list of injectors and extractors.
// When injecting, all injectors are called to propagate the span context.
// When extracting, it tries each extractor, selecting the first successful one.
type chainedPropagator struct {
injectors []Propagator
extractors []Propagator
}
// getPropagators returns a list of propagators based on ps, which is a comma seperated
// list of propagators. If the list doesn't contain any valid values, the
// default propagator will be returned. Any invalid values in the list will log
// a warning and be ignored.
func getPropagators(cfg *PropagatorConfig, ps string) []Propagator {
dd := &propagator{cfg}
defaultPs := []Propagator{&propagatorW3c{}, dd}
if cfg.B3 {
defaultPs = append(defaultPs, &propagatorB3{})
}
if ps == "" {
if prop := os.Getenv(headerPropagationStyle); prop != "" {
ps = prop // use the generic DD_TRACE_PROPAGATION_STYLE if set
} else {
return defaultPs // no env set, so use default from configuration
}
}
ps = strings.ToLower(ps)
if ps == "none" {
return nil
}
var list []Propagator
if cfg.B3 {
list = append(list, &propagatorB3{})
}
for _, v := range strings.Split(ps, ",") {
switch strings.ToLower(v) {
case "datadog":
list = append(list, dd)
case "tracecontext":
list = append([]Propagator{&propagatorW3c{}}, list...)
case "b3", "b3multi":
if !cfg.B3 {
// propagatorB3 hasn't already been added, add a new one.
list = append(list, &propagatorB3{})
}
case "b3 single header":
list = append(list, &propagatorB3SingleHeader{})
case "none":
log.Warn("Propagator \"none\" has no effect when combined with other propagators. " +
"To disable the propagator, set to `none`")
default:
log.Warn("unrecognized propagator: %s\n", v)
}
}
if len(list) == 0 {
return defaultPs // no valid propagators, so return default
}
return list
}
// Inject defines the Propagator to propagate SpanContext data
// out of the current process. The implementation propagates the
// TraceID and the current active SpanID, as well as the Span baggage.
func (p *chainedPropagator) Inject(spanCtx ddtrace.SpanContext, carrier interface{}) error {
for _, v := range p.injectors {
err := v.Inject(spanCtx, carrier)
if err != nil {
return err
}
}
return nil
}
// Extract implements Propagator.
func (p *chainedPropagator) Extract(carrier interface{}) (ddtrace.SpanContext, error) {
for _, v := range p.extractors {
ctx, err := v.Extract(carrier)
if ctx != nil {
// first extractor returns
log.Debug("Extracted span context: %#v", ctx)
return ctx, nil
}
if err == ErrSpanContextNotFound {
continue
}
return nil, err
}
return nil, ErrSpanContextNotFound
}
// propagator implements Propagator and injects/extracts span contexts
// using datadog headers. Only TextMap carriers are supported.
type propagator struct {
cfg *PropagatorConfig
}
func (p *propagator) Inject(spanCtx ddtrace.SpanContext, carrier interface{}) error {
switch c := carrier.(type) {
case TextMapWriter:
return p.injectTextMap(spanCtx, c)
default:
return ErrInvalidCarrier
}
}
func (p *propagator) injectTextMap(spanCtx ddtrace.SpanContext, writer TextMapWriter) error {
ctx, ok := spanCtx.(*spanContext)
if !ok || ctx.traceID.Empty() || ctx.spanID == 0 {
return ErrInvalidSpanContext
}
// propagate the TraceID and the current active SpanID
if ctx.traceID.HasUpper() {
setPropagatingTag(ctx, keyTraceID128, ctx.traceID.UpperHex())
} else if ctx.trace != nil {
ctx.trace.unsetPropagatingTag(keyTraceID128)
}
writer.Set(p.cfg.TraceHeader, strconv.FormatUint(ctx.traceID.Lower(), 10))
writer.Set(p.cfg.ParentHeader, strconv.FormatUint(ctx.spanID, 10))
if sp, ok := ctx.samplingPriority(); ok {
writer.Set(p.cfg.PriorityHeader, strconv.Itoa(sp))
}
if ctx.origin != "" {
writer.Set(originHeader, ctx.origin)
}
// propagate OpenTracing baggage
for k, v := range ctx.baggage {
writer.Set(p.cfg.BaggagePrefix+k, v)
}
if p.cfg.MaxTagsHeaderLen <= 0 {
return nil
}
if s := p.marshalPropagatingTags(ctx); len(s) > 0 {
writer.Set(traceTagsHeader, s)
}
return nil
}
// marshalPropagatingTags marshals all propagating tags included in ctx to a comma separated string
func (p *propagator) marshalPropagatingTags(ctx *spanContext) string {
var sb strings.Builder
if ctx.trace == nil {
return ""
}
ctx.trace.mu.Lock()
defer ctx.trace.mu.Unlock()
for k, v := range ctx.trace.propagatingTags {
if err := isValidPropagatableTag(k, v); err != nil {
log.Warn("Won't propagate tag '%s': %v", k, err.Error())
ctx.trace.setTag(keyPropagationError, "encoding_error")
continue
}
if sb.Len()+len(k)+len(v) > p.cfg.MaxTagsHeaderLen {
sb.Reset()
log.Warn("Won't propagate tag: maximum trace tags header len (%d) reached.", p.cfg.MaxTagsHeaderLen)
ctx.trace.setTag(keyPropagationError, "inject_max_size")
break
}
if sb.Len() > 0 {
sb.WriteByte(',')
}
sb.WriteString(k)
sb.WriteByte('=')
sb.WriteString(v)
}
return sb.String()
}
func (p *propagator) Extract(carrier interface{}) (ddtrace.SpanContext, error) {
switch c := carrier.(type) {
case TextMapReader:
return p.extractTextMap(c)
default:
return nil, ErrInvalidCarrier
}
}
func (p *propagator) extractTextMap(reader TextMapReader) (ddtrace.SpanContext, error) {
var ctx spanContext
err := reader.ForeachKey(func(k, v string) error {
var err error
key := strings.ToLower(k)
switch key {
case p.cfg.TraceHeader:
var lowerTid uint64
lowerTid, err = parseUint64(v)
if err != nil {
return ErrSpanContextCorrupted
}
ctx.traceID.SetLower(lowerTid)
case p.cfg.ParentHeader:
ctx.spanID, err = parseUint64(v)
if err != nil {
return ErrSpanContextCorrupted
}
case p.cfg.PriorityHeader:
priority, err := strconv.Atoi(v)
if err != nil {
return ErrSpanContextCorrupted
}
ctx.setSamplingPriority(priority, samplernames.Unknown)
case originHeader:
ctx.origin = v
case traceTagsHeader:
unmarshalPropagatingTags(&ctx, v)
default:
if strings.HasPrefix(key, p.cfg.BaggagePrefix) {
ctx.setBaggageItem(strings.TrimPrefix(key, p.cfg.BaggagePrefix), v)
}
}
return nil
})
if err != nil {
return nil, err
}
if ctx.trace != nil {
// TODO: this always assumed it was valid so I copied that logic here, maybe we shouldn't
ctx.traceID.SetUpperFromHex(ctx.trace.propagatingTags[keyTraceID128])
}
if ctx.traceID.Empty() || (ctx.spanID == 0 && ctx.origin != "synthetics") {
return nil, ErrSpanContextNotFound
}
return &ctx, nil
}
// unmarshalPropagatingTags unmarshals tags from v into ctx
func unmarshalPropagatingTags(ctx *spanContext, v string) {
if ctx.trace == nil {
ctx.trace = newTrace()
}
ctx.trace.mu.Lock()
defer ctx.trace.mu.Unlock()
if len(v) > propagationExtractMaxSize {
log.Warn("Did not extract %s, size limit exceeded: %d. Incoming tags will not be propagated further.", traceTagsHeader, propagationExtractMaxSize)
ctx.trace.setTag(keyPropagationError, "extract_max_size")
return
}
var err error
ctx.trace.propagatingTags, err = parsePropagatableTraceTags(v)
if err != nil {
log.Warn("Did not extract %s: %v. Incoming tags will not be propagated further.", traceTagsHeader, err.Error())
ctx.trace.setTag(keyPropagationError, "decoding_error")
}
}
// setPropagatingTag adds the key value pair to the map of propagating tags on the trace,
// creating the map if one is not initialized.
func setPropagatingTag(ctx *spanContext, k, v string) {
if ctx.trace == nil {
// extractors initialize a new spanContext, so the trace might be nil
ctx.trace = newTrace()
}
ctx.trace.setPropagatingTag(k, v)
}
const (
b3TraceIDHeader = "x-b3-traceid"
b3SpanIDHeader = "x-b3-spanid"
b3SampledHeader = "x-b3-sampled"
b3SingleHeader = "b3"
)
// propagatorB3 implements Propagator and injects/extracts span contexts
// using B3 headers. Only TextMap carriers are supported.
type propagatorB3 struct{}
func (p *propagatorB3) Inject(spanCtx ddtrace.SpanContext, carrier interface{}) error {
switch c := carrier.(type) {
case TextMapWriter:
return p.injectTextMap(spanCtx, c)
default:
return ErrInvalidCarrier
}
}
func (*propagatorB3) injectTextMap(spanCtx ddtrace.SpanContext, writer TextMapWriter) error {
ctx, ok := spanCtx.(*spanContext)
if !ok || ctx.traceID.Empty() || ctx.spanID == 0 {
return ErrInvalidSpanContext
}
if !ctx.traceID.HasUpper() { // 64-bit trace id
writer.Set(b3TraceIDHeader, fmt.Sprintf("%016x", ctx.traceID.Lower()))
} else { // 128-bit trace id
var w3Cctx ddtrace.SpanContextW3C
if w3Cctx, ok = spanCtx.(ddtrace.SpanContextW3C); !ok {
return ErrInvalidSpanContext
}
writer.Set(b3TraceIDHeader, w3Cctx.TraceID128())
}
writer.Set(b3SpanIDHeader, fmt.Sprintf("%016x", ctx.spanID))
if p, ok := ctx.samplingPriority(); ok {
if p >= ext.PriorityAutoKeep {
writer.Set(b3SampledHeader, "1")
} else {
writer.Set(b3SampledHeader, "0")
}
}
return nil
}
func (p *propagatorB3) Extract(carrier interface{}) (ddtrace.SpanContext, error) {
switch c := carrier.(type) {
case TextMapReader:
return p.extractTextMap(c)
default:
return nil, ErrInvalidCarrier
}
}
func (*propagatorB3) extractTextMap(reader TextMapReader) (ddtrace.SpanContext, error) {
var ctx spanContext
err := reader.ForeachKey(func(k, v string) error {
var err error
key := strings.ToLower(k)
switch key {
case b3TraceIDHeader:
if err := extractTraceID128(&ctx, v); err != nil {
return nil
}
case b3SpanIDHeader:
ctx.spanID, err = strconv.ParseUint(v, 16, 64)
if err != nil {
return ErrSpanContextCorrupted
}
case b3SampledHeader:
priority, err := strconv.Atoi(v)
if err != nil {
return ErrSpanContextCorrupted
}
ctx.setSamplingPriority(priority, samplernames.Unknown)
default:
}
return nil
})
if err != nil {
return nil, err
}
if ctx.traceID.Empty() || ctx.spanID == 0 {
return nil, ErrSpanContextNotFound
}
return &ctx, nil
}
// propagatorB3 implements Propagator and injects/extracts span contexts
// using B3 headers. Only TextMap carriers are supported.
type propagatorB3SingleHeader struct{}
func (p *propagatorB3SingleHeader) Inject(spanCtx ddtrace.SpanContext, carrier interface{}) error {
switch c := carrier.(type) {
case TextMapWriter:
return p.injectTextMap(spanCtx, c)
default:
return ErrInvalidCarrier
}
}
func (*propagatorB3SingleHeader) injectTextMap(spanCtx ddtrace.SpanContext, writer TextMapWriter) error {
ctx, ok := spanCtx.(*spanContext)
if !ok || ctx.traceID.Empty() || ctx.spanID == 0 {
return ErrInvalidSpanContext
}
sb := strings.Builder{}
var traceID string
if !ctx.traceID.HasUpper() { // 64-bit trace id
traceID = fmt.Sprintf("%016x", ctx.traceID.Lower())
} else { // 128-bit trace id
var w3Cctx ddtrace.SpanContextW3C
if w3Cctx, ok = spanCtx.(ddtrace.SpanContextW3C); !ok {
return ErrInvalidSpanContext
}
traceID = w3Cctx.TraceID128()
}
sb.WriteString(fmt.Sprintf("%s-%016x", traceID, ctx.spanID))
if p, ok := ctx.samplingPriority(); ok {
if p >= ext.PriorityAutoKeep {
sb.WriteString("-1")
} else {
sb.WriteString("-0")
}
}
writer.Set(b3SingleHeader, sb.String())
return nil
}
func (p *propagatorB3SingleHeader) Extract(carrier interface{}) (ddtrace.SpanContext, error) {
switch c := carrier.(type) {
case TextMapReader:
return p.extractTextMap(c)
default:
return nil, ErrInvalidCarrier
}
}
func (*propagatorB3SingleHeader) extractTextMap(reader TextMapReader) (ddtrace.SpanContext, error) {
var ctx spanContext
err := reader.ForeachKey(func(k, v string) error {
var err error
key := strings.ToLower(k)
switch key {
case b3SingleHeader:
b3Parts := strings.Split(v, "-")
if len(b3Parts) >= 2 {
if err = extractTraceID128(&ctx, b3Parts[0]); err != nil {
return err
}
ctx.spanID, err = strconv.ParseUint(b3Parts[1], 16, 64)
if err != nil {
return ErrSpanContextCorrupted
}
if len(b3Parts) >= 3 {
switch b3Parts[2] {
case "":
break
case "1", "d": // Treat 'debug' traces as priority 1
ctx.setSamplingPriority(1, samplernames.Unknown)
case "0":
ctx.setSamplingPriority(0, samplernames.Unknown)
default:
return ErrSpanContextCorrupted
}
}
} else {
return ErrSpanContextCorrupted
}
default:
}
return nil
})
if err != nil {
return nil, err
}
if ctx.traceID.Empty() || ctx.spanID == 0 {
return nil, ErrSpanContextNotFound
}
return &ctx, nil
}
const (
traceparentHeader = "traceparent"
tracestateHeader = "tracestate"
w3cTraceIDTag = "w3cTraceID"
)
// propagatorW3c implements Propagator and injects/extracts span contexts
// using W3C tracecontext/traceparent headers. Only TextMap carriers are supported.
type propagatorW3c struct{}
func (p *propagatorW3c) Inject(spanCtx ddtrace.SpanContext, carrier interface{}) error {
switch c := carrier.(type) {
case TextMapWriter:
return p.injectTextMap(spanCtx, c)
default:
return ErrInvalidCarrier
}
}
// injectTextMap propagates span context attributes into the writer,
// in the format of the traceparentHeader and tracestateHeader.
// traceparentHeader encodes W3C Trace Propagation version, 128-bit traceID,
// spanID, and a flags field, which supports 8 unique flags.
// The current specification only supports a single flag called sampled,
// which is equal to 00000001 when no other flag is present.
// tracestateHeader is a comma-separated list of list-members with a <key>=<value> format,
// where each list-member is managed by a vendor or instrumentation library.
func (*propagatorW3c) injectTextMap(spanCtx ddtrace.SpanContext, writer TextMapWriter) error {
ctx, ok := spanCtx.(*spanContext)
if !ok || ctx.traceID.Empty() || ctx.spanID == 0 {
return ErrInvalidSpanContext
}
flags := ""
p, ok := ctx.samplingPriority()
if ok && p >= ext.PriorityAutoKeep {
flags = "01"
} else {
flags = "00"
}
var traceID string
if ctx.traceID.HasUpper() {
setPropagatingTag(ctx, keyTraceID128, ctx.traceID.UpperHex())
if w3Cctx, ok := spanCtx.(ddtrace.SpanContextW3C); ok {
traceID = w3Cctx.TraceID128()
}
} else {
traceID = fmt.Sprintf("%032x", ctx.traceID)
if ctx.trace != nil {
ctx.trace.unsetPropagatingTag(keyTraceID128)
}
}
writer.Set(traceparentHeader, fmt.Sprintf("00-%s-%016x-%v", traceID, ctx.spanID, flags))
// if context priority / origin / tags were updated after extraction,
// or the tracestateHeader doesn't start with `dd=`
// we need to recreate tracestate
if ctx.updated ||
(ctx.trace != nil && ctx.trace.propagatingTags != nil && !strings.HasPrefix(ctx.trace.propagatingTags[tracestateHeader], "dd=")) ||
len(ctx.trace.propagatingTags[tracestateHeader]) == 0 {
writer.Set(tracestateHeader, composeTracestate(ctx, p, ctx.trace.propagatingTags[tracestateHeader]))
} else {
writer.Set(tracestateHeader, ctx.trace.propagatingTags[tracestateHeader])
}
return nil
}
var (
// keyRgx is used to sanitize the keys of the datadog propagating tags.
// Disallowed characters are comma (reserved as a list-member separator),
// equals (reserved for list-member key-value separator),
// space and characters outside the ASCII range 0x20 to 0x7E.
// Disallowed characters must be replaced with the underscore.
keyRgx = regexp.MustCompile(",|=|[^\\x20-\\x7E]+")
// valueRgx is used to sanitize the values of the datadog propagating tags.
// Disallowed characters are comma (reserved as a list-member separator),
// semi-colon (reserved for separator between entries in the dd list-member),
// tilde (reserved, will represent 0x3D (equals) in the encoded tag value,
// and characters outside the ASCII range 0x20 to 0x7E.
// Equals character must be encoded with a tilde.
// Other disallowed characters must be replaced with the underscore.
valueRgx = regexp.MustCompile(",|;|~|[^\\x20-\\x7E]+")
// originRgx is used to sanitize the value of the datadog origin tag.
// Disallowed characters are comma (reserved as a list-member separator),
// semi-colon (reserved for separator between entries in the dd list-member),
// equals (reserved for list-member key-value separator),
// and characters outside the ASCII range 0x21 to 0x7E.
// Equals character must be encoded with a tilde.
// Other disallowed characters must be replaced with the underscore.
originRgx = regexp.MustCompile(",|~|;|[^\\x21-\\x7E]+")
// validIDRgx is used to verify that the input is a valid hex string.
// The input must match the pattern from start to end.
// validIDRgx is applicable for both trace and span IDs.
validIDRgx = regexp.MustCompile("^[a-f0-9]+$")
)
// composeTracestate creates a tracestateHeader from the spancontext.
// The Datadog tracing library is only responsible for managing the list member with key dd,
// which holds the values of the sampling decision(`s:<value>`), origin(`o:<origin>`),
// and propagated tags prefixed with `t.`(e.g. _dd.p.usr.id:usr_id tag will become `t.usr.id:usr_id`).
func composeTracestate(ctx *spanContext, priority int, oldState string) string {
var b strings.Builder
b.Grow(128)
b.WriteString(fmt.Sprintf("dd=s:%d", priority))
listLength := 1
if ctx.origin != "" {
oWithSub := originRgx.ReplaceAllString(ctx.origin, "_")
b.WriteString(fmt.Sprintf(";o:%s",
strings.ReplaceAll(oWithSub, "=", "~")))
}
for k, v := range ctx.trace.propagatingTags {
if !strings.HasPrefix(k, "_dd.p.") {
continue
}
// Datadog propagating tags must be appended to the tracestateHeader
// with the `t.` prefix. Tag value must have all `=` signs replaced with a tilde (`~`).
tag := fmt.Sprintf("t.%s:%s",
keyRgx.ReplaceAllString(k[len("_dd.p."):], "_"),
strings.ReplaceAll(valueRgx.ReplaceAllString(v, "_"), "=", "~"))
if b.Len()+len(tag) > 256 {
break
}
b.WriteString(";")
b.WriteString(tag)
}
// the old state is split by vendors, must be concatenated with a `,`
if len(oldState) == 0 {
return b.String()
}
for _, s := range strings.Split(strings.Trim(oldState, " \t"), ",") {
if strings.HasPrefix(s, "dd=") {
continue
}
listLength++
// if the resulting tracestateHeader exceeds 32 list-members,
// remove the rightmost list-member(s)
if listLength > 32 {
break
}
b.WriteString("," + strings.Trim(s, " \t"))
}
return b.String()
}
func (p *propagatorW3c) Extract(carrier interface{}) (ddtrace.SpanContext, error) {
switch c := carrier.(type) {
case TextMapReader:
return p.extractTextMap(c)
default:
return nil, ErrInvalidCarrier
}
}
func (*propagatorW3c) extractTextMap(reader TextMapReader) (ddtrace.SpanContext, error) {
var parentHeader string
var stateHeader string
var ctx spanContext
// to avoid parsing tracestate header(s) if traceparent is invalid
if err := reader.ForeachKey(func(k, v string) error {
key := strings.ToLower(k)
switch key {
case traceparentHeader:
if parentHeader != "" {
return ErrSpanContextCorrupted
}
parentHeader = v
case tracestateHeader:
stateHeader = v
default:
if strings.HasPrefix(key, DefaultBaggageHeaderPrefix) {
ctx.setBaggageItem(strings.TrimPrefix(key, DefaultBaggageHeaderPrefix), v)
}
}
return nil
}); err != nil {
return nil, err
}
if err := parseTraceparent(&ctx, parentHeader); err != nil {
return nil, err
}
parseTracestate(&ctx, stateHeader)
return &ctx, nil
}
// parseTraceparent attempts to parse traceparentHeader which describes the position
// of the incoming request in its trace graph in a portable, fixed-length format.
// The format of the traceparentHeader is `-` separated string with in the
// following format: `version-traceId-spanID-flags`, with an optional `-<prefix>` if version > 0.
// where:
// - version - represents the version of the W3C Tracecontext Propagation format in hex format.
// - traceId - represents the propagated traceID in the format of 32 hex-encoded digits.
// - spanID - represents the propagated spanID (parentID) in the format of 16 hex-encoded digits.
// - flags - represents the propagated flags in the format of 2 hex-encoded digits, and supports 8 unique flags.
// Example value of HTTP `traceparent` header: `00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01`,
// Currently, Go tracer doesn't support 128-bit traceIDs, so the full traceID (32 hex-encoded digits) must be
// stored into a field that is accessible from the span’s context. TraceId will be parsed from the least significant 16
// hex-encoded digits into a 64-bit number.
func parseTraceparent(ctx *spanContext, header string) error {
nonWordCutset := "_-\t \n"
header = strings.ToLower(strings.Trim(header, "\t -"))
headerLen := len(header)
if headerLen == 0 {
return ErrSpanContextNotFound
}
if headerLen < 55 {
return ErrSpanContextCorrupted
}
parts := strings.SplitN(header, "-", 5) // 5 because we expect 4 required + 1 optional substrings
if len(parts) < 4 {
return ErrSpanContextCorrupted
}
version := strings.Trim(parts[0], nonWordCutset)
if len(version) != 2 {
return ErrSpanContextCorrupted
}
v, err := strconv.ParseUint(version, 16, 64)
if err != nil || v == 255 {
// version 255 (0xff) is invalid
return ErrSpanContextCorrupted
}
if v == 0 && headerLen != 55 {
// The header length in v0 has to be 55.
// It's allowed to be longer in other versions.
return ErrSpanContextCorrupted
}
// parsing traceID
fullTraceID := strings.Trim(parts[1], nonWordCutset)
if len(fullTraceID) != 32 {
return ErrSpanContextCorrupted
}
// checking that the entire TraceID is a valid hex string
if ok := validIDRgx.MatchString(fullTraceID); !ok {
return ErrSpanContextCorrupted
}
if ctx.trace != nil {
// Ensure that the 128-bit trace id tag doesn't propagate
ctx.trace.unsetPropagatingTag(keyTraceID128)
}
if err := extractTraceID128(ctx, fullTraceID); err != nil {
return err
}
// parsing spanID
spanID := strings.Trim(parts[2], nonWordCutset)
if len(spanID) != 16 {
return ErrSpanContextCorrupted
}
if ok := validIDRgx.MatchString(spanID); !ok {
return ErrSpanContextCorrupted
}
if ctx.spanID, err = strconv.ParseUint(spanID, 16, 64); err != nil {
return ErrSpanContextCorrupted
}
if ctx.spanID == 0 {
return ErrSpanContextNotFound
}
// parsing flags
flags := parts[3]
f, err := strconv.ParseInt(flags, 16, 8)
if err != nil {
return ErrSpanContextCorrupted
}
ctx.setSamplingPriority(int(f)&0x1, samplernames.Unknown)
return nil
}
// parseTracestate attempts to parse tracestateHeader which is a list
// with up to 32 comma-separated (,) list-members.
// An example value would be: `vendorname1=opaqueValue1,vendorname2=opaqueValue2,dd=s:1;o:synthetics`,
// Where `dd` list contains values that would be in x-datadog-tags as well as those needed for propagation information.
// The keys to the “dd“ values have been shortened as follows to save space:
// `sampling_priority` = `s`
// `origin` = `o`
// `_dd.p.` prefix = `t.`
func parseTracestate(ctx *spanContext, header string) {
if header == "" {
// The W3C spec says tracestate can be empty but should avoid sending it.
// https://www.w3.org/TR/trace-context-1/#tracestate-header-field-values
return
}
// if multiple headers are present, they must be combined and stored
setPropagatingTag(ctx, tracestateHeader, header)
combined := strings.Split(strings.Trim(header, "\t "), ",")
for _, group := range combined {
if !strings.HasPrefix(group, "dd=") {
continue
}
ddMembers := strings.Split(group[len("dd="):], ";")
dropDM := false
for _, member := range ddMembers {
keyVal := strings.SplitN(member, ":", 2)
if len(keyVal) != 2 {
continue
}
key, val := keyVal[0], keyVal[1]
if key == "o" {
ctx.origin = strings.ReplaceAll(val, "~", "=")
} else if key == "s" {
stateP, err := strconv.Atoi(val)
if err != nil {
// If the tracestate priority is absent,
// we rely on the traceparent sampled flag
// set in the parseTraceparent function.
continue
}
// The sampling priority and decision maker values are set based on
// the specification in the internal W3C context propagation RFC.
// See the document for more details.
parentP, _ := ctx.samplingPriority()
if (parentP == 1 && stateP > 0) || (parentP == 0 && stateP <= 0) {
// As extracted from tracestate
ctx.setSamplingPriority(stateP, samplernames.Unknown)
}
if parentP == 1 && stateP <= 0 {
// Auto keep (1) and set the decision maker to default
ctx.setSamplingPriority(1, samplernames.Default)
}
if parentP == 0 && stateP > 0 {
// Auto drop (0) and drop the decision maker
ctx.setSamplingPriority(0, samplernames.Unknown)
dropDM = true
}
} else if strings.HasPrefix(key, "t.dm") {
if ctx.trace.hasPropagatingTag(keyDecisionMaker) || dropDM {
continue
}
setPropagatingTag(ctx, keyDecisionMaker, val)
} else if strings.HasPrefix(key, "t.") {
keySuffix := key[len("t."):]
val = strings.ReplaceAll(val, "~", "=")
setPropagatingTag(ctx, "_dd.p."+keySuffix, val)
}
}
}
}
// extractTraceID128 extracts the trace id from v and populates the traceID
// field, and the traceID128 field (if applicable) of the provided ctx,
// returning an error if v is invalid.
func extractTraceID128(ctx *spanContext, v string) error {
if len(v) > 32 {
v = v[len(v)-32:]
}
v = strings.TrimLeft(v, "0")
var err error
if len(v) <= 16 { // 64-bit trace id
var tid uint64
tid, err = strconv.ParseUint(v, 16, 64)
ctx.traceID.SetLower(tid)
} else { // 128-bit trace id
idUpper := v[:len(v)-16]
ctx.traceID.SetUpperFromHex(idUpper)
var l uint64
l, err = strconv.ParseUint(v[len(idUpper):], 16, 64)
ctx.traceID.SetLower(l)
}
if err != nil {
return ErrSpanContextCorrupted
}
return nil
}