Skip to content

Commit

Permalink
feat: add untraced ctx method (#1634)
Browse files Browse the repository at this point in the history
* feat: Support non block structured untraced context
  • Loading branch information
robertlaurin authored May 8, 2024
1 parent 7162e34 commit 84d64ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions common/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Bundler/OrderedGems:
Style/FrozenStringLiteralComment:
Exclude:
- gemfiles/**/*
Style/ExplicitBlockArgument:
Enabled: false
Style/StringLiterals:
Exclude:
- gemfiles/**/*
Expand Down
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

0 comments on commit 84d64ca

Please sign in to comment.