File tree Expand file tree Collapse file tree 4 files changed +73
-1
lines changed
lib/traces/backend/open_telemetry Expand file tree Collapse file tree 4 files changed +73
-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 @@ -100,6 +100,16 @@ def my_span_and_context
100
100
trace_id : be != nil
101
101
)
102
102
end
103
+
104
+ it "returns nil when there is no active trace" do
105
+ # Use a new Fiber to create a clean context without any active traces
106
+ result = Fiber . new do
107
+ # trace_context should return nil when there's no active trace
108
+ Traces . trace_context
109
+ end . resume
110
+
111
+ expect ( result ) . to be_nil
112
+ end
103
113
end
104
114
105
115
with "#trace_context=" do
@@ -117,6 +127,37 @@ def my_span_and_context
117
127
)
118
128
end
119
129
end
130
+
131
+ with "#active?" do
132
+ it "returns true when there is an active trace" do
133
+ Traces . trace ( "test" ) do
134
+ expect ( Traces . active? ) . to be == true
135
+ end
136
+ end
137
+
138
+ it "returns false when there is no active trace" do
139
+ # Use a new Fiber to create a clean context without any active traces
140
+ result = Fiber . new do
141
+ Traces . active?
142
+ end . resume
143
+
144
+ expect ( result ) . to be == false
145
+ end
146
+
147
+ it "returns false after a trace completes in a new execution context" do
148
+ Traces . trace ( "test" ) do |span |
149
+ # Should be active inside the trace
150
+ expect ( Traces . active? ) . to be == true
151
+ end
152
+
153
+ # In a new Fiber, should not be active
154
+ result = Fiber . new do
155
+ Traces . active?
156
+ end . resume
157
+
158
+ expect ( result ) . to be == false
159
+ end
160
+ end
120
161
end
121
162
122
163
describe "Context Propagation Methods" do
You can’t perform that action at this time.
0 commit comments