Skip to content

Commit

Permalink
feat: Support non block structured untraced ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlaurin committed May 8, 2024
1 parent a1f1629 commit 18e4fa3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions common/lib/opentelemetry/common/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ def truncate_attribute_value(value, limit)
end

# Disables tracing within the provided block.
def untraced
Context.with_value(UNTRACED_KEY, true) do |ctx, _|
yield ctx
def untraced(context = Context.current)
context = context.set_value(UNTRACED_KEY, true)
if block_given?
Context.with_current(context) { |ctx| yield ctx }
else
context
end
end

Expand Down
7 changes: 7 additions & 0 deletions common/test/opentelemetry/common/utilities_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def shutdown(timeout: nil); end
common_utils.untraced {}
assert_equal(false, common_utils.untraced?)
end

it 'supports non block format' do
token = OpenTelemetry::Context.attach(common_utils.untraced)
assert_equal(true, common_utils.untraced?)
OpenTelemetry::Context.detach(token)
assert_equal(false, common_utils.untraced?)
end
end

describe '#utf8_encode' do
Expand Down

0 comments on commit 18e4fa3

Please sign in to comment.