Skip to content

Commit 4cd4a52

Browse files
authored
fix: untraced only works with parent-based sampler (#1412)
* fix: untraced only works with parent-based sampler * Appease the cop * Now with more grease * Yet moar grease * Fix gemfiles/gemspec
1 parent 2bcb464 commit 4cd4a52

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

common/lib/opentelemetry/common/utilities.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module Common
1010
module Utilities
1111
extend self
1212

13+
UNTRACED_KEY = Context.create_key('untraced')
14+
private_constant :UNTRACED_KEY
15+
1316
STRING_PLACEHOLDER = ''.encode(::Encoding::UTF_8).freeze
1417

1518
# Returns nil if timeout is nil, 0 if timeout has expired,
@@ -83,8 +86,17 @@ def truncate_attribute_value(value, limit)
8386
end
8487
end
8588

89+
# Disables tracing within the provided block.
8690
def untraced
87-
OpenTelemetry::Trace.with_span(OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new)) { yield } # rubocop:disable Style/ExplicitBlockArgument
91+
Context.with_value(UNTRACED_KEY, true) do |ctx, _|
92+
yield ctx
93+
end
94+
end
95+
96+
# Detects whether the current context has been set to disable tracing.
97+
def untraced?(context = nil)
98+
context ||= Context.current
99+
!!context.value(UNTRACED_KEY)
88100
end
89101

90102
# Returns a URL string with userinfo removed.

common/test/opentelemetry/common/utilities_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ def shutdown(timeout: nil); end
1717

1818
let(:common_utils) { OpenTelemetry::Common::Utilities }
1919

20+
describe '#untraced?' do
21+
it 'returns true within an untraced block' do
22+
assert_equal(true, common_utils.untraced { common_utils.untraced? })
23+
end
24+
25+
it 'returns false outside an untraced block' do
26+
common_utils.untraced {}
27+
assert_equal(false, common_utils.untraced?)
28+
end
29+
end
30+
2031
describe '#utf8_encode' do
2132
it 'happy path' do
2233
str = 'pristine ¬'.encode(Encoding::UTF_8)

exporter/otlp-common/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gemspec
1010

1111
group :test, :development do
1212
gem 'opentelemetry-api', path: '../../api'
13+
gem 'opentelemetry-common', path: '../../common'
1314
gem 'opentelemetry-registry', path: '../../registry'
1415
gem 'opentelemetry-sdk', path: '../../sdk'
1516
gem 'opentelemetry-test-helpers', path: '../../test_helpers'

exporter/otlp-http/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gemspec
1212

1313
group :test, :development do
1414
gem 'opentelemetry-api', path: '../../api'
15+
gem 'opentelemetry-common', path: '../../common'
1516
gem 'opentelemetry-exporter-otlp-common', path: '../otlp-common'
1617
gem 'opentelemetry-registry', path: '../../registry'
1718
gem 'opentelemetry-sdk', path: '../../sdk'

sdk/lib/opentelemetry/sdk/trace/tracer.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kin
2828
end
2929

3030
def start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
31+
with_parent ||= Context.current
32+
return super(name, with_parent: with_parent, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind) if Common::Utilities.untraced?(with_parent)
33+
3134
name ||= 'empty'
3235
kind ||= :internal
33-
with_parent ||= Context.current
3436

3537
@tracer_provider.internal_start_span(name, kind, attributes, links, start_timestamp, with_parent, @instrumentation_scope)
3638
end

sdk/test/opentelemetry/sdk/trace/tracer_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@
192192
_(span).wont_be :recording?
193193
end
194194

195+
it 'returns a no-op span within an untraced block' do
196+
tracer_provider.sampler = Samplers::ALWAYS_ON
197+
span = OpenTelemetry::Common::Utilities.untraced { tracer.start_span('op') }
198+
_(span.context.trace_flags).wont_be :sampled?
199+
_(span).wont_be :recording?
200+
end
201+
195202
it 'returns an unsampled span if sampler says record, but do not sample' do
196203
tracer_provider.sampler = record_sampler
197204
span = tracer.start_span('op', with_parent: context)

0 commit comments

Comments
 (0)