Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/dalli/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ class << self
# Uses the library name 'dalli' and current Dalli::VERSION.
#
# @return [OpenTelemetry::Trace::Tracer, nil] the tracer or nil if OTel unavailable
# rubocop:disable ThreadSafety/ClassInstanceVariable
# rubocop:disable ThreadSafety/ClassInstanceVariable, ThreadSafety/ClassAndModuleAttributes
def tracer
return @tracer if defined?(@tracer)

@tracer = (OpenTelemetry.tracer_provider.tracer('dalli', Dalli::VERSION) if defined?(OpenTelemetry))
end
# rubocop:enable ThreadSafety/ClassInstanceVariable

attr_writer :tracer

# Returns true if instrumentation is enabled (OpenTelemetry SDK is available).
#
Expand All @@ -76,6 +77,15 @@ def enabled?
!tracer.nil?
end

# Disable instrumentation.
#
# @return [nil]
def disable!
@tracer = nil
end

# rubocop:enable ThreadSafety/ClassInstanceVariable, ThreadSafety/ClassAndModuleAttributes

# Wraps a block with a span if instrumentation is enabled.
#
# Creates a client span with the given name and attributes merged with
Expand Down
12 changes: 12 additions & 0 deletions test/test_instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ def clear_tracer_cache
end
end

describe '.disable!' do
it 'causes .enabled? to return false' do
Dalli::Instrumentation.tracer = MockOpenTelemetry::MockTracer.new

assert_predicate Dalli::Instrumentation, :enabled?

Dalli::Instrumentation.disable!

refute_predicate Dalli::Instrumentation, :enabled?
end
end

describe '.trace without OpenTelemetry' do
it 'yields the block and returns its result when tracing is disabled' do
clear_tracer_cache
Expand Down
Loading