-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathspan.go
More file actions
1294 lines (1193 loc) · 40.9 KB
/
Copy pathspan.go
File metadata and controls
1294 lines (1193 loc) · 40.9 KB
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
//msgp:ignore inheritedData
//go:generate go run github.com/tinylib/msgp -unexported -marshal=false -o=span_msgp.go -tests=false
//go:generate go run ../../scripts/msgp_span_meta_omitempty.go -file span_msgp.go
//go:generate go run ../../scripts/msgp_checklocks_ignore.go -type Span -file span_msgp.go
package tracer
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"maps"
"math"
"reflect"
"runtime/pprof"
rt "runtime/trace"
"strconv"
"strings"
"time"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
traceinternal "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer/internal"
"github.com/DataDog/dd-trace-go/v2/instrumentation/errortrace"
sharedinternal "github.com/DataDog/dd-trace-go/v2/internal"
"github.com/DataDog/dd-trace-go/v2/internal/env"
"github.com/DataDog/dd-trace-go/v2/internal/globalconfig"
illmobs "github.com/DataDog/dd-trace-go/v2/internal/llmobs"
"github.com/DataDog/dd-trace-go/v2/internal/locking"
"github.com/DataDog/dd-trace-go/v2/internal/locking/assert"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/orchestrion"
"github.com/DataDog/dd-trace-go/v2/internal/samplernames"
"github.com/DataDog/dd-trace-go/v2/internal/stacktrace"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"
"github.com/DataDog/dd-trace-go/v2/internal/traceprof"
"github.com/tinylib/msgp/msgp"
"golang.org/x/xerrors"
"github.com/DataDog/datadog-agent/pkg/obfuscate"
)
type (
// spanList implements msgp.Encodable on top of a slice of spans.
spanList []*Span
// spanLists implements msgp.Decodable on top of a slice of spanList.
// This type is only used in tests.
spanLists []spanList
)
var (
_ msgp.Encodable = (*spanList)(nil)
_ msgp.Decodable = (*spanLists)(nil)
)
// errorConfig holds customization options for setting error tags.
type errorConfig struct {
noDebugStack bool
stackFrames uint
stackSkip uint
}
// AsMap places tags and span properties into a map and returns it.
//
// Note that this is not performant, nor are spans guaranteed to have all of their
// properties set at any time during normal operation! This is used for testing only,
// and should not be used in non-test code, or you may run into performance or other
// issues.
// +checklocksignore — Test-only, not safe for concurrent use.
func (s *Span) AsMap() map[string]any {
m := make(map[string]any)
if s == nil {
return m
}
m[ext.SpanName] = s.name
m[ext.ServiceName] = s.service
m[ext.ResourceName] = s.resource
m[ext.SpanType] = s.spanType
m[ext.MapSpanStart] = s.start
m[ext.MapSpanDuration] = s.duration
for k, v := range s.meta.All() {
m[k] = v
}
for k, v := range s.metrics {
m[k] = v
}
maps.Copy(m, s.metaStruct)
m[ext.MapSpanID] = s.spanID
m[ext.MapSpanTraceID] = s.traceID
m[ext.MapSpanParentID] = s.parentID
m[ext.MapSpanError] = s.error
if events := s.spanEventsAsJSONString(); events != "" {
m[ext.MapSpanEvents] = events
}
return m
}
// +checklocksignore — Called from AsMap (test-only, not concurrent).
func (s *Span) spanEventsAsJSONString() string {
if !s.supportsEvents {
v, _ := s.meta.Get("events")
return v
}
if s.spanEvents == nil {
return ""
}
events, err := json.Marshal(s.spanEvents)
if err != nil {
log.Error("failed to marshal span events: %s", err.Error())
return ""
}
return string(events)
}
// Span represents a computation. Callers must call Finish when a Span is
// complete to ensure it's submitted.
type Span struct {
// guards below fields
mu locking.RWMutex `msg:"-"`
// +checklocks:mu
name string `msg:"name"` // operation name
// +checklocks:mu
service string `msg:"service"` // service name (i.e. "grpc.server", "http.request")
// +checklocks:mu
resource string `msg:"resource"` // resource name (i.e. "/user?id=123", "SELECT * FROM users")
// +checklocks:mu
spanType string `msg:"type"` // protocol associated with the span (i.e. "web", "db", "cache")
// +checklocks:mu
start int64 `msg:"start"` // span start time expressed in nanoseconds since epoch
// +checklocks:mu
duration int64 `msg:"duration"` // duration of the span expressed in nanoseconds
// +checklocks:mu
// meta holds string metadata. Promoted attributes (env, version, component,
// span.kind) live in meta.attrs and are excluded from meta.m; the custom
// msgp codec merges both for wire encoding.
meta traceinternal.SpanMeta `msg:"meta,omitempty"` // arbitrary map of metadata + promoted attrs
// +checklocks:mu
metaStruct metaStructMap `msg:"meta_struct,omitempty"` // arbitrary map of metadata with structured values
// +checklocks:mu
metrics map[string]float64 `msg:"metrics,omitempty"` // arbitrary map of numeric metrics
// +checklocks:mu
spanID uint64 `msg:"span_id"` // identifier of this span
// +checklocks:mu
traceID uint64 `msg:"trace_id"` // lower 64-bits of the root span identifier
// +checklocks:mu
parentID uint64 `msg:"parent_id"` // identifier of the span's direct parent
// +checklocks:mu
error int32 `msg:"error"` // error status of the span; 0 means no errors
// +checklocks:mu
spanLinks []SpanLink `msg:"span_links,omitempty"` // links to other spans
// +checklocks:mu
spanEvents []spanEvent `msg:"span_events,omitempty"` // events produced related to this span
goExecTraced bool `msg:"-"`
noDebugStack bool `msg:"-"` // disables debug stack traces
context *SpanContext `msg:"-"` // span propagation context
// +checklocks:mu
supportsEvents bool `msg:"-"` // whether the span supports native span events or not
// +checklocks:mu
finished bool `msg:"-"` // true if the span has been submitted to a tracer. Can only be read/modified if the trace is locked.
// +checklocks:mu
integration string `msg:"-"` // where the span was started from, such as a specific contrib or "manual"
// +checklocks:mu
serviceSource string `msg:"-"` // tracks the source of service name override; set to serviceSourceManual when SetTag overrides it
// +checklocks:mu
pprofCtxActive context.Context `msg:"-"` // contains pprof.WithLabel labels to tell the profiler more about this span
// +checklocks:mu
pprofCtxRestore context.Context `msg:"-"` // contains pprof.WithLabel labels of the parent span (if any) that need to be restored when this span finishes
// +checklocks:mu
taskEnd func() // ends execution tracer (runtime/trace) task, if started
}
// Context yields the SpanContext for this Span. Note that the return
// value of Context() is still valid after a call to Finish(). This is
// called the span context and it is different from Go's context.
func (s *Span) Context() *SpanContext {
if s == nil {
return nil
}
return s.context
}
type inheritedData struct {
service string
serviceSource string
pprofCtx context.Context
}
func (s *Span) inheritedData() inheritedData {
s.mu.RLock()
defer s.mu.RUnlock()
return inheritedData{
service: s.service,
serviceSource: s.serviceSource,
pprofCtx: s.pprofCtxActive,
}
}
// getSpanID concurrency safe reads the spanID field.
func (s *Span) getSpanID() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.spanID
}
// getResource concurrency safe reads the resource field.
func (s *Span) getResource() string {
s.mu.RLock()
defer s.mu.RUnlock()
return s.resource
}
// getAndRemoveMeta retrieves and removes metadata by key.
// Used by civisibility_tslv.go for test span level tag processing.
func (s *Span) getAndRemoveMeta(key string) string {
s.mu.Lock()
defer s.mu.Unlock()
if v, ok := s.meta.Get(key); ok {
s.meta.Delete(key)
delete(s.metrics, key)
return v
}
return ""
}
// applyTraceRuleSampling applies trace rule sampling to the span.
// It sets the applied rate metric, evaluates Knuth rate-based sampling,
// applies rate limiting, and sets the appropriate sampling priority.
// Returns false if span is already finished, true otherwise.
func (s *Span) applyTraceRuleSampling(rate float64, sampler samplernames.SamplerName, limiter *rateLimiter, now time.Time) bool {
s.mu.Lock()
defer s.mu.Unlock()
if s.finished {
return false
}
s.setMetricLocked(keyRulesSamplerAppliedRate, rate)
delete(s.metrics, keySamplingPriorityRate)
s.setMetaLocked(keyKnuthSamplingRate, formatKnuthSamplingRate(rate))
if !sampledByRate(s.traceID, rate) {
s.setSamplingPriorityLocked(ext.PriorityUserReject, sampler)
return true
}
if limiter == nil {
s.setSamplingPriorityLocked(ext.PriorityUserKeep, sampler)
return true
}
sampled, limiterRate := limiter.allowOne(now)
if sampled {
s.setSamplingPriorityLocked(ext.PriorityUserKeep, sampler)
} else {
s.setSamplingPriorityLocked(ext.PriorityUserReject, sampler)
}
s.setMetricLocked(keyRulesSamplerLimiterRate, limiterRate)
return true
}
// applySingleSpanSamplingWithLock applies single-span sampling rule to the span with the lock held.
// Used by rules_sampler.go when applying single span sampling rules.
// The maxPerSecond parameter should be 0 if there's no rate limit for the rule.
// Note: This is called on finished spans during trace flushing, so we don't check s.finished.
func (s *Span) applySingleSpanSamplingWithLock(rate, maxPerSecond float64) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.metrics, keySamplingPriorityRate)
s.setMetricLocked(keySpanSamplingMechanism, float64(samplernames.SingleSpan))
s.setMetricLocked(keySingleSpanSamplingRuleRate, rate)
if maxPerSecond != 0 {
s.setMetricLocked(keySingleSpanSamplingMPS, maxPerSecond)
}
}
// debugInfo returns span information for debugging and logging.
// Used by test code for formatting span information.
func (s *Span) debugInfo() (name string, spanID, traceID uint64, integration string) {
s.mu.RLock()
defer s.mu.RUnlock()
name = s.name
spanID = s.spanID
traceID = s.traceID
if v, ok := s.meta.Get(ext.Component); ok {
integration = v
} else {
integration = "manual"
}
return
}
// matchTagsForSampling checks if span tags match the sampling rule tag patterns.
// Used by rules_sampler.go for tag-based sampling rule matching.
// Returns true if all tag patterns match, false otherwise.
func (s *Span) matchTagsForSampling(tagPatterns map[string]func(string) bool) bool {
s.mu.RLock()
defer s.mu.RUnlock()
for k, matchFunc := range tagPatterns {
if v, ok := s.meta.Get(k); ok && matchFunc(v) {
continue
}
if s.metrics != nil {
if v, ok := s.metrics[k]; ok {
// sampling on numbers with floating point is not supported,
// thus 'math.Floor(v) != v'
if math.Floor(v) == v {
strVal := strconv.FormatFloat(v, 'g', -1, 64)
if matchFunc(strVal) {
continue
}
}
}
}
// Tag pattern didn't match in either meta or metrics
return false
}
return true
}
// SetBaggageItem sets a key/value pair as baggage on the span. Baggage items
// are propagated down to descendant spans and injected cross-process. Use with
// care as it adds extra load onto your tracing layer.
func (s *Span) SetBaggageItem(key, val string) {
if s == nil {
return
}
s.context.setBaggageItem(key, val)
}
// BaggageItem gets the value for a baggage item given its key. Returns the
// empty string if the value isn't found in this Span.
func (s *Span) BaggageItem(key string) string {
if s == nil {
return ""
}
return s.context.baggageItem(key)
}
// safeStringerValue safely calls v.String(), returning "<nil>" if it panics
// due to a nil pointer receiver. All other panics are re-raised.
func safeStringerValue(v fmt.Stringer, original any) (result string) {
defer func() {
if e := recover(); e != nil {
if rv := reflect.ValueOf(original); rv.Kind() == reflect.Pointer && rv.IsNil() {
result = "<nil>"
return
}
panic(e)
}
}()
return v.String()
}
// SetTag adds a set of key/value metadata to the span.
func (s *Span) SetTag(key string, value any) {
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.setTagLocked(key, value)
}
// setTags sets multiple tags on the span during initialization. It acquires
// the span lock internally and returns early without locking if tags is empty.
func (s *Span) setTags(tags map[string]any) {
if len(tags) == 0 {
return
}
s.mu.Lock()
for k, v := range tags {
s.setTagLocked(k, v)
}
s.mu.Unlock()
}
// setTagLocked sets a tag on the span. This method assumes the span lock is already held.
// +checklocks:s.mu
func (s *Span) setTagLocked(key string, value any) {
assert.RWMutexLocked(&s.mu)
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if s.finished {
return
}
// To avoid dumping the memory address in case value is a pointer, we dereference it.
// Any pointer value that is a pointer to a pointer will be dumped as a string.
value = dereference(value)
switch key {
case ext.Error:
s.setTagErrorLocked(value, errorConfig{
noDebugStack: s.noDebugStack,
})
return
case ext.ErrorNoStackTrace:
s.setTagErrorLocked(value, errorConfig{
noDebugStack: true,
})
return
case ext.Component:
integration, ok := value.(string)
if ok {
s.integration = integration
}
case ext.KeyServiceSource:
if so, ok := value.(sharedinternal.ServiceOverride); ok {
s.service = so.Name
s.serviceSource = so.Source
return
}
}
if v, ok := value.(bool); ok {
s.setTagBoolLocked(key, v)
return
}
if v, ok := value.(string); ok {
if key == ext.ResourceName && s.pprofCtxActive != nil && spanResourcePIISafe(s) {
// If the user overrides the resource name for the span,
// update the endpoint label for the runtime profilers.
//
// We don't change s.pprofCtxRestore since that should
// stay as the original parent span context regardless
// of what we change at a lower level.
s.pprofCtxActive = pprof.WithLabels(s.pprofCtxActive, pprof.Labels(traceprof.TraceEndpoint, v))
pprof.SetGoroutineLabels(s.pprofCtxActive)
}
s.setMetaLocked(key, v)
return
}
if v, ok := sharedinternal.ToFloat64(value); ok {
s.setMetricLocked(key, v)
return
}
if v, ok := value.(fmt.Stringer); ok {
s.setMetaLocked(key, safeStringerValue(v, value))
return
}
if v, ok := value.([]byte); ok {
s.setMetaLocked(key, string(v))
return
}
if value != nil {
// Arrays will be translated to dot notation. e.g.
// {"myarr.0": "foo", "myarr.1": "bar"}
// which will be displayed as an array in the UI.
switch reflect.TypeOf(value).Kind() {
case reflect.Slice:
slice := reflect.ValueOf(value)
for i := 0; i < slice.Len(); i++ {
key := fmt.Sprintf("%s.%d", key, i)
v := slice.Index(i)
if num, ok := sharedinternal.ToFloat64(v.Interface()); ok {
s.setMetricLocked(key, num)
} else {
s.setMetaLocked(key, fmt.Sprintf("%v", v))
}
}
return
}
// Can be sent as messagepack in `meta_struct` instead of `meta`
// reserved for internal use only
if v, ok := value.(sharedinternal.MetaStructValue); ok {
s.setMetaStructLocked(key, v.Value)
return
}
// Support for v1 shim meta struct values (only _dd.stack uses this)
if key == "_dd.stack" {
s.setMetaStructLocked(key, value)
return
}
// Add this trace source tag to propagating tags and to span tags
// reserved for internal use only
if v, ok := value.(sharedinternal.TraceSourceTagValue); ok {
s.context.trace.setTraceSourcePropagatingTag(key, v.Value)
}
}
// not numeric, not a string, not a fmt.Stringer, not a bool, and not an error
s.setMetaLocked(key, fmt.Sprint(value))
}
// setSamplingPriority locks the span, then updates the sampling priority.
// It also updates the trace's sampling priority.
func (s *Span) setSamplingPriority(priority int, sampler samplernames.SamplerName) {
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.setSamplingPriorityLocked(priority, sampler)
}
func (s *Span) setProcessTags(pTags string) {
s.mu.Lock()
defer s.mu.Unlock()
s.setMetaLocked(keyProcessTags, pTags)
}
// root returns the root span of the span's trace. The return value shouldn't be
// nil as long as the root span is valid and not finished.
func (s *Span) Root() *Span {
if s == nil || s.context == nil {
return nil
}
if s.context.trace == nil {
return nil
}
return s.context.trace.root
}
// SetUser associates user information to the current trace which the
// provided span belongs to. The options can be used to tune which user
// bit of information gets monitored. In case of distributed traces,
// the user id can be propagated across traces using the WithPropagation() option.
// See https://docs.datadoghq.com/security_platform/application_security/setup_and_configure/?tab=set_user#add-user-information-to-traces
func (s *Span) SetUser(id string, opts ...UserMonitoringOption) {
if s == nil {
return
}
cfg := UserMonitoringConfig{
Metadata: make(map[string]string),
}
for _, fn := range opts {
fn(&cfg)
}
root := s.Root()
trace := root.context.trace
root.mu.Lock()
defer root.mu.Unlock()
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if root.finished {
return
}
if cfg.PropagateID {
// Delete usr.id from the tags since _dd.p.usr.id takes precedence
root.meta.Delete(keyUserID)
idenc := base64.StdEncoding.EncodeToString([]byte(id))
trace.setPropagatingTag(keyPropagatedUserID, idenc)
s.context.updated = true
} else {
if trace.hasPropagatingTag(keyPropagatedUserID) {
// Unset the propagated user ID so that a propagated user ID coming from upstream won't be propagated anymore.
trace.unsetPropagatingTag(keyPropagatedUserID)
s.context.updated = true
}
root.meta.Delete(keyPropagatedUserID)
}
usrData := map[string]string{
keyUserID: id,
keyUserLogin: cfg.Login,
keyUserEmail: cfg.Email,
keyUserName: cfg.Name,
keyUserScope: cfg.Scope,
keyUserRole: cfg.Role,
keyUserSessionID: cfg.SessionID,
}
for k, v := range cfg.Metadata {
usrData[fmt.Sprintf("usr.%s", k)] = v
}
for k, v := range usrData {
if v != "" {
// setMeta is used since the span is already locked
root.setMetaLocked(k, v)
}
}
}
// StartChild starts a new child span with the given operation name and options.
func (s *Span) StartChild(operationName string, opts ...StartSpanOption) *Span {
if s == nil {
return nil
}
opts = append(opts, ChildOf(s.Context()))
return getGlobalTracer().StartSpan(operationName, opts...)
}
// setSamplingPriorityLocked updates the sampling priority.
// It also updates the trace's sampling priority.
// s.mu must be held for writing.
// +checklocks:s.mu
func (s *Span) setSamplingPriorityLocked(priority int, sampler samplernames.SamplerName) {
assert.RWMutexLocked(&s.mu)
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if s.finished {
return
}
s.setMetricLocked(keySamplingPriority, float64(priority))
s.context.setSamplingPriority(priority, sampler)
}
// forceSetSamplingPriorityLocked updates the sampling priority.
// If the trace is locked, the sampling priority is forced to the given value.
//
// This function is should only be used when applying a manual keep or drop decision.
// s.mu must be held for writing.
// +checklocks:s.mu
func (s *Span) forceSetSamplingPriorityLocked(priority int, sampler samplernames.SamplerName) {
assert.RWMutexLocked(&s.mu)
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if s.finished {
return
}
s.setMetricLocked(keySamplingPriority, float64(priority))
s.context.forceSetSamplingPriority(priority, sampler)
}
// setErrorFlagLocked sets the error flag on the span and adjusts the trace error count.
// s.mu must be held for writing.
// +checklocks:s.mu
func (s *Span) setErrorFlagLocked(yes bool) {
assert.RWMutexLocked(&s.mu)
if yes {
if s.error == 0 {
// new error
s.context.errors.Add(1)
}
s.error = 1
} else {
if s.error > 0 {
// flip from active to inactive
s.context.errors.Add(-1)
}
s.error = 0
}
}
// setTagErrorLocked sets the error tag. It accounts for various valid scenarios.
// s.mu must be held for writing.
// +checklocks:s.mu
func (s *Span) setTagErrorLocked(value any, cfg errorConfig) {
assert.RWMutexLocked(&s.mu)
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if s.finished {
return
}
switch v := value.(type) {
case bool:
// bool value as per Opentracing spec.
s.setErrorFlagLocked(v)
case error:
// if anyone sets an error value as the tag, be nice here
// and provide all the benefits.
s.setErrorFlagLocked(true)
s.setMetaLocked(ext.ErrorMsg, v.Error())
s.setMetaLocked(ext.ErrorType, reflect.TypeOf(v).String())
if cfg.noDebugStack {
return
}
switch v.(type) {
case xerrors.Formatter, fmt.Formatter, *errortrace.TracerError:
s.setMetaLocked(ext.ErrorStack, fmt.Sprintf("%+v", v))
}
handlingStack := takeStacktrace(cfg.stackFrames, cfg.stackSkip)
s.setMetaLocked(ext.ErrorHandlingStack, handlingStack)
case nil:
// no error
s.setErrorFlagLocked(false)
default:
// in all other cases, let's assume that setting this tag
// is the result of an error.
s.setErrorFlagLocked(true)
}
}
// takeStacktrace takes a stack trace of maximum n entries, skipping the first skip entries.
// If n is 0, the default depth from internal/stacktrace is used.
// Uses the centralized internal/stacktrace implementation while preserving telemetry tracking.
func takeStacktrace(depth uint, skip uint) string {
telemetry.Count(telemetry.NamespaceTracers, "errorstack.source", []string{"source:takeStacktrace"}).Submit(1)
now := time.Now()
defer func() {
dur := float64(time.Since(now))
telemetry.Distribution(telemetry.NamespaceTracers, "errorstack.duration", []string{"source:takeStacktrace"}).Submit(dur)
}()
// This is necessary for span error stacktraces where we want complete visibility.
// Skip +4: The old implementation used runtime.Callers(2+skip, ...) which skipped runtime.Callers
// and takeStacktrace. The internal/stacktrace package auto-filters its own frames, but we still
// need to account for: runtime.Callers(1) + takeStacktrace(1) + setTagError(1) + additional frame(1)
stack := stacktrace.SkipAndCaptureWithInternalFrames(int(depth), int(skip)+4)
return stacktrace.Format(stack)
}
// setMeta sets a string tag during span initialization (before the span is published).
// This method should only be used during span construction in spanStart and StartSpan.
func (s *Span) setMeta(key, v string) {
s.mu.Lock()
defer s.mu.Unlock()
s.setMetaLocked(key, v)
}
// setMetaLocked sets a string tag. This method assumes the span lock is already held.
// +checklocks:s.mu
func (s *Span) setMetaLocked(key, v string) {
assert.RWMutexLocked(&s.mu)
s.setMetaInit(key, v)
}
// setMetaInit sets a string tag without acquiring the lock and asserting the lock is held.
// +checklocksignore — Initialization time, span not yet shared.
func (s *Span) setMetaInit(key, v string) {
delete(s.metrics, key)
switch key {
case ext.SpanName:
s.name = v
case ext.ServiceName:
s.service = v
s.serviceSource = serviceSourceManual
case ext.ResourceName:
s.resource = v
case ext.SpanType:
s.spanType = v
default:
s.meta.Set(key, v)
}
}
// setMetaStructLocked sets structured metadata. This method assumes the span lock is already held.
// +checklocks:s.mu
func (s *Span) setMetaStructLocked(key string, v any) {
assert.RWMutexLocked(&s.mu)
if s.metaStruct == nil {
s.metaStruct = make(metaStructMap, 1)
}
s.metaStruct[key] = v
}
// setTagBoolLocked sets a boolean tag on the span. This method assumes the span lock is already held.
// +checklocks:s.mu
func (s *Span) setTagBoolLocked(key string, v bool) {
assert.RWMutexLocked(&s.mu)
switch key {
case ext.AnalyticsEvent:
if v {
s.setMetricLocked(ext.EventSampleRate, 1.0)
} else {
s.setMetricLocked(ext.EventSampleRate, 0.0)
}
case ext.ManualDrop:
if v {
s.forceSetSamplingPriorityLocked(ext.PriorityUserReject, samplernames.Manual)
}
case ext.ManualKeep:
if v {
s.forceSetSamplingPriorityLocked(ext.PriorityUserKeep, samplernames.Manual)
}
default:
if v {
s.setMetaLocked(key, "true")
} else {
s.setMetaLocked(key, "false")
}
}
}
// setMetric sets a numeric tag during span initialization (before the span is published).
// This method should only be used during span construction in spanStart and StartSpan.
// +checklocksignore — Initialization time, span not yet shared.
func (s *Span) setMetricInit(key string, v float64) {
if s.metrics == nil {
s.metrics = make(map[string]float64, 1)
}
s.meta.Delete(key)
// Note: We don't handle ManualKeep or _sampling_priority_v1shim during init
// because those require modifying trace-level state which needs locking
s.metrics[key] = v
}
func (s *Span) setMetric(key string, v float64) {
s.mu.Lock()
defer s.mu.Unlock()
s.setMetricLocked(key, v)
}
// setMetricLocked sets a numeric tag, in our case called a metric. This method
// assumes the span lock is already held.
// +checklocks:s.mu
func (s *Span) setMetricLocked(key string, v float64) {
assert.RWMutexLocked(&s.mu)
if s.metrics == nil {
s.metrics = make(map[string]float64, 1)
}
s.meta.Delete(key)
switch key {
case ext.ManualKeep:
if v == float64(samplernames.AppSec) {
s.setSamplingPriorityLocked(ext.PriorityUserKeep, samplernames.AppSec)
}
case "_sampling_priority_v1shim":
// We have this for backward compatibility with the v1 shim.
s.setSamplingPriorityLocked(int(v), samplernames.Manual)
default:
s.metrics[key] = v
}
}
// AddLink appends the given link to the span's span links.
func (s *Span) AddLink(link SpanLink) {
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if s.finished {
// already finished
return
}
s.spanLinks = append(s.spanLinks, link)
}
// serializeSpanLinksInMeta saves span links as a JSON string under `Span[meta][_dd.span_links]`.
// +checklocks:s.mu
func (s *Span) serializeSpanLinksInMeta() {
assert.RWMutexLocked(&s.mu)
if len(s.spanLinks) == 0 {
return
}
spanLinkBytes, err := json.Marshal(s.spanLinks)
if err != nil {
log.Debug("Unable to marshal span links. Not adding span links to span meta.")
return
}
s.meta.Set("_dd.span_links", string(spanLinkBytes))
}
// serializeSpanEvents sets the span events from the current span in the correct transport, depending on whether the
// agent supports the native method or not.
// +checklocks:s.mu
func (s *Span) serializeSpanEvents() {
assert.RWMutexLocked(&s.mu)
if len(s.spanEvents) == 0 {
return
}
// if span events are natively supported by the agent, there's nothing to do
// as the events will be already included when the span is serialized.
if s.supportsEvents {
return
}
// otherwise, we need to serialize them as a string tag and remove them from the struct
// so they are not sent twice.
b, err := json.Marshal(s.spanEvents)
s.spanEvents = nil
if err != nil {
log.Debug("Unable to marshal span events; events dropped from span meta\n%s", err.Error())
return
}
s.meta.Set("events", string(b))
}
// Finish closes this Span (but not its children) providing the duration
// of its part of the tracing session.
func (s *Span) Finish(opts ...FinishOption) {
if s == nil {
return
}
t := now()
if len(opts) > 0 {
cfg := FinishConfig{
NoDebugStack: s.noDebugStack,
}
for _, fn := range opts {
if fn == nil {
continue
}
fn(&cfg)
}
if !cfg.FinishTime.IsZero() {
t = cfg.FinishTime.UnixNano()
}
if cfg.Error != nil {
s.mu.Lock()
s.setTagErrorLocked(cfg.Error, errorConfig{
noDebugStack: cfg.NoDebugStack,
stackFrames: cfg.StackFrames,
stackSkip: cfg.SkipStackFrames,
})
s.mu.Unlock()
}
}
if s.goExecTraced && rt.IsEnabled() {
// Only tag spans as traced if they both started & ended with
// execution tracing enabled. This is technically not sufficient
// for spans which could straddle the boundary between two
// execution traces, but there's really nothing we can do in
// those cases since execution tracing tasks aren't recorded in
// traces if they started before the trace.
s.SetTag("go_execution_traced", "yes")
} else if s.goExecTraced {
// If the span started with tracing enabled, but tracing wasn't
// enabled when the span finished, we still have some data to
// show. If tracing wasn't enabled when the span started, we
// won't have data in the execution trace to identify it so
// there's nothign we can show.
s.SetTag("go_execution_traced", "partial")
}
if s.Root() == s {
if tr, ok := getGlobalTracer().(*tracer); ok && tr.rulesSampling.traces.enabled() {
if !s.context.trace.isLocked() && s.context.trace.propagatingTag(keyDecisionMaker) != "-4" {
tr.rulesSampling.SampleTrace(s)
}
}
}
s.finish(t)
orchestrion.GLSPopValue(sharedinternal.ActiveSpanKey)
}
// SetOperationName sets or changes the operation name.
func (s *Span) SetOperationName(operationName string) {
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if s.finished {
// already finished
return
}
s.name = operationName
}
// enrichServiceSource writes the _dd.svc_src meta tag at finish time.
// No tag is written when the span's service matches the global DD_SERVICE (no override)
// or when no source was determined.
// +checklocks:s.mu
func (s *Span) enrichServiceSource() {
if s.serviceSource == "" || s.service == globalconfig.ServiceName() {
return
}
s.meta.Set(ext.KeyServiceSource, s.serviceSource)
}
func (s *Span) finish(finishTime int64) {
s.mu.Lock()
defer s.mu.Unlock()
// We don't lock spans when flushing, so we could have a data race when
// modifying a span as it's being flushed. This protects us against that
// race, since spans are marked `finished` before we flush them.
if s.finished {
// already finished
return
}
s.serializeSpanLinksInMeta()
s.serializeSpanEvents()
s.enrichServiceSource()
if s.duration == 0 {
s.duration = finishTime - s.start
}
if s.duration < 0 {
s.duration = 0
}
if s.taskEnd != nil {
s.taskEnd()
}
keep := true