@@ -11,6 +11,10 @@ module Contrib
11
11
# For contrib, we only allow one tracer to be active:
12
12
# the global tracer in +Datadog::Tracing+.
13
13
module TracerHelpers
14
+ def mutex
15
+ @mutex ||= Mutex . new
16
+ end
17
+
14
18
# Returns the current tracer instance
15
19
def tracer
16
20
Datadog ::Tracing . send ( :tracer )
@@ -23,7 +27,9 @@ def traces
23
27
24
28
# Returns spans and caches it (similar to +let(:spans)+).
25
29
def spans
26
- @spans ||= fetch_spans
30
+ mutex . synchronize do
31
+ @spans ||= fetch_spans_without_sorting
32
+ end
27
33
end
28
34
29
35
# Retrieves all traces in the current tracer instance.
@@ -35,39 +41,44 @@ def fetch_traces(tracer = self.tracer)
35
41
# Retrieves and sorts all spans in the current tracer instance.
36
42
# This method does not cache its results.
37
43
def fetch_spans ( tracer = self . tracer )
38
- traces = fetch_traces ( tracer )
39
- traces . collect ( &:spans ) . flatten . sort! do |a , b |
40
- if a . name == b . name
41
- if a . resource == b . resource
42
- if a . start_time == b . start_time
43
- a . end_time <=> b . end_time
44
+ mutex . synchronize do
45
+ traces = fetch_traces ( tracer )
46
+ spans = traces . collect ( &:spans )
47
+ spans . flatten . sort! do |a , b |
48
+ if a . name == b . name
49
+ if a . resource == b . resource
50
+ if a . start_time == b . start_time
51
+ a . end_time <=> b . end_time
52
+ else
53
+ a . start_time <=> b . start_time
54
+ end
44
55
else
45
- a . start_time <=> b . start_time
56
+ a . resource <=> b . resource
46
57
end
47
58
else
48
- a . resource <=> b . resource
59
+ a . name <=> b . name
49
60
end
50
- else
51
- a . name <=> b . name
52
61
end
53
62
end
54
63
end
55
64
56
65
def fetch_spans_without_sorting ( tracer = self . tracer )
57
66
traces = fetch_traces ( tracer )
58
- spans = traces . collect ( & : spans)
67
+ spans = traces . map { | trace | trace . instance_variable_get ( :@ spans) || [ ] }
59
68
spans . flatten # gets spans for every trace in the tracer instance
60
69
end
61
70
62
71
# Remove all traces from the current tracer instance and
63
72
# busts cache of +#spans+ and +#span+.
64
73
def clear_traces!
65
- tracer . instance_variable_set ( :@traces , [ ] )
74
+ mutex . synchronize do
75
+ tracer . instance_variable_set ( :@traces , [ ] )
66
76
67
- @traces = nil
68
- @trace = nil
69
- @spans = nil
70
- @span = nil
77
+ @traces = nil
78
+ @trace = nil
79
+ @spans = nil
80
+ @span = nil
81
+ end
71
82
end
72
83
73
84
RSpec . configure do |config |
0 commit comments