Skip to content

Commit 84d64ca

Browse files
authored
feat: add untraced ctx method (#1634)
* feat: Support non block structured untraced context
1 parent 7162e34 commit 84d64ca

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

common/.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Bundler/OrderedGems:
66
Style/FrozenStringLiteralComment:
77
Exclude:
88
- gemfiles/**/*
9+
Style/ExplicitBlockArgument:
10+
Enabled: false
911
Style/StringLiterals:
1012
Exclude:
1113
- gemfiles/**/*

common/lib/opentelemetry/common/utilities.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,17 @@ def truncate_attribute_value(value, limit)
8888
end
8989
end
9090

91-
# Disables tracing within the provided block.
92-
def untraced
93-
Context.with_value(UNTRACED_KEY, true) do |ctx, _|
94-
yield ctx
91+
# Disables tracing within the provided block
92+
# If no block is provided instead returns an
93+
# untraced ctx.
94+
#
95+
# @param [optional Context] context Accepts an explicit context, defaults to current
96+
def untraced(context = Context.current)
97+
context = context.set_value(UNTRACED_KEY, true)
98+
if block_given?
99+
Context.with_current(context) { |ctx| yield ctx }
100+
else
101+
context
95102
end
96103
end
97104

common/test/opentelemetry/common/utilities_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def shutdown(timeout: nil); end
2626
common_utils.untraced {}
2727
assert_equal(false, common_utils.untraced?)
2828
end
29+
30+
it 'supports non block format' do
31+
token = OpenTelemetry::Context.attach(common_utils.untraced)
32+
assert_equal(true, common_utils.untraced?)
33+
OpenTelemetry::Context.detach(token)
34+
assert_equal(false, common_utils.untraced?)
35+
end
2936
end
3037

3138
describe '#utf8_encode' do

0 commit comments

Comments
 (0)