File tree Expand file tree Collapse file tree 4 files changed +75
-1
lines changed
lib/traces/backend/open_telemetry Expand file tree Collapse file tree 4 files changed +75
-1
lines changed Original file line number Diff line number Diff line change 21
21
gem "sus"
22
22
gem "covered"
23
23
gem "decode"
24
-
24
+
25
25
gem "rubocop"
26
26
gem "rubocop-socketry"
27
27
Original file line number Diff line number Diff line change @@ -36,7 +36,15 @@ def trace_context=(context)
36
36
end
37
37
end
38
38
39
+ def active?
40
+ # Check if there's a real active trace using OpenTelemetry's INVALID span:
41
+ ::OpenTelemetry ::Trace . current_span != ::OpenTelemetry ::Trace ::Span ::INVALID
42
+ end
43
+
39
44
def trace_context ( span = ::OpenTelemetry ::Trace . current_span )
45
+ # Return nil if no active trace (using INVALID span check):
46
+ return nil if span == ::OpenTelemetry ::Trace ::Span ::INVALID
47
+
40
48
if span_context = span . context
41
49
flags = 0
42
50
@@ -87,6 +95,24 @@ def inject(headers = nil, context = nil)
87
95
def extract ( headers )
88
96
::OpenTelemetry . propagation . extract ( headers )
89
97
end
98
+
99
+ def active?
100
+ # Check if there's a real active trace by examining the current span
101
+ span = ::OpenTelemetry ::Trace . current_span
102
+ return false unless span
103
+
104
+ span_context = span . context
105
+ return false unless span_context
106
+
107
+ # A trace is only "active" if it has a non-zero trace_id
108
+ # An all-zero trace_id indicates no real trace is active
109
+ trace_id = span_context . trace_id
110
+ return false if trace_id . nil?
111
+
112
+ # Check if trace_id is all zeros (16 bytes of zeros)
113
+ null_trace_id = "\x00 " * 16
114
+ return trace_id != null_trace_id
115
+ end
90
116
end
91
117
end
92
118
Original file line number Diff line number Diff line change 1
1
# Releases
2
2
3
+ ## Unreleased
4
+
5
+ - Fixed ` Traces.active? ` to correctly return ` false ` when there is no active trace, instead of always returning ` true ` .
6
+ - Fixed ` Traces.trace_context ` to return ` nil ` when there is no active trace, instead of returning invalid Context objects.
7
+
3
8
## v0.3.0
4
9
5
10
### New Context Propagation Interface
Original file line number Diff line number Diff line change @@ -117,6 +117,49 @@ def my_span_and_context
117
117
)
118
118
end
119
119
end
120
+
121
+ with "#active?" do
122
+ it "returns true when there is an active trace" do
123
+ Traces . trace ( "test" ) do
124
+ expect ( Traces . active? ) . to be == true
125
+ end
126
+ end
127
+
128
+ it "returns false when there is no active trace" do
129
+ # Use a new Fiber to create a clean context without any active traces
130
+ result = Fiber . new do
131
+ Traces . active?
132
+ end . resume
133
+
134
+ expect ( result ) . to be == false
135
+ end
136
+
137
+ it "returns false after a trace completes in a new execution context" do
138
+ Traces . trace ( "test" ) do |span |
139
+ # Should be active inside the trace
140
+ expect ( Traces . active? ) . to be == true
141
+ end
142
+
143
+ # In a new Fiber, should not be active
144
+ result = Fiber . new do
145
+ Traces . active?
146
+ end . resume
147
+
148
+ expect ( result ) . to be == false
149
+ end
150
+ end
151
+
152
+ with "#trace_context when no active trace" do
153
+ it "returns nil when there is no active trace" do
154
+ # Use a new Fiber to create a clean context without any active traces
155
+ result = Fiber . new do
156
+ # trace_context should return nil when there's no active trace
157
+ Traces . trace_context
158
+ end . resume
159
+
160
+ expect ( result ) . to be_nil
161
+ end
162
+ end
120
163
end
121
164
122
165
describe "Context Propagation Methods" do
You can’t perform that action at this time.
0 commit comments