Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add untraced ctx method #1634

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 11 additions & 4 deletions common/lib/opentelemetry/common/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ def truncate_attribute_value(value, limit)
end
end

# Disables tracing within the provided block.
def untraced
Context.with_value(UNTRACED_KEY, true) do |ctx, _|
yield ctx
# Disables tracing within the provided block
# If no block is provided instead returns an
# untraced ctx.
#
# @param [optional Context] context Accepts an explicit context, defaults to current
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
Loading