Skip to content

Commit 076a26c

Browse files
committed
feat: handle disabled state better
1 parent e99ee07 commit 076a26c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/agents/tracing.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ module Tracing
2626
class << self
2727
# Initialize OpenTelemetry SDK with OTLP exporter.
2828
# Called automatically during Agents.configure if tracing is enabled.
29+
# If tracing is disabled, tears down any existing tracing infrastructure.
2930
#
3031
# @param config [Agents::Configuration] The configuration object
3132
def setup(config)
32-
return unless config.enable_tracing
33+
unless config.enable_tracing
34+
# Explicitly disable tracing if it was previously enabled
35+
teardown if @enabled
36+
return
37+
end
3338

3439
# Configure the OTLP exporter
3540
exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(
@@ -187,13 +192,21 @@ def tool_span(tool_name, arguments:, attributes: {})
187192
# Reset tracing state (mainly for testing)
188193
# @private
189194
def reset!
195+
teardown
196+
end
197+
198+
private
199+
200+
# Tear down tracing infrastructure
201+
# Clears internal state but does not shutdown the OpenTelemetry tracer provider
202+
# (which is a global singleton and cannot be reinitialized in the same process).
203+
# @private
204+
def teardown
190205
@tracer = nil
191206
@enabled = false
192207
Thread.current[:agents_trace_context_stack] = nil
193208
end
194209

195-
private
196-
197210
# Thread-local storage for trace context stack
198211
def trace_context_stack
199212
Thread.current[:agents_trace_context_stack] ||= []

spec/agents/tracing_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@
4141
expect(described_class.enabled?).to be false
4242
end
4343
end
44+
45+
context "when reconfiguring" do
46+
let(:disabled_config) do
47+
instance_double(
48+
Agents::Configuration,
49+
enable_tracing: false
50+
)
51+
end
52+
53+
it "disables tracing when reconfigured with enable_tracing: false" do
54+
# First enable tracing
55+
described_class.setup(config)
56+
expect(described_class.enabled?).to be true
57+
58+
# Then disable it
59+
described_class.setup(disabled_config)
60+
expect(described_class.enabled?).to be false
61+
end
62+
end
4463
end
4564

4665
describe ".with_trace" do

0 commit comments

Comments
 (0)