lib/opentelemetry/common/utilities.rb in opentelemetry-common-0.19.6 vs lib/opentelemetry/common/utilities.rb in opentelemetry-common-0.19.7
- old
+ new
@@ -8,10 +8,13 @@
module Common
# Utilities contains common helpers.
module Utilities
extend self
+ UNTRACED_KEY = Context.create_key('untraced')
+ private_constant :UNTRACED_KEY
+
STRING_PLACEHOLDER = ''.encode(::Encoding::UTF_8).freeze
# Returns nil if timeout is nil, 0 if timeout has expired,
# or the remaining (positive) time left in seconds.
#
@@ -81,11 +84,20 @@
else
value
end
end
+ # Disables tracing within the provided block.
def untraced
- OpenTelemetry::Trace.with_span(OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new)) { yield }
+ Context.with_value(UNTRACED_KEY, true) do |ctx, _|
+ yield ctx
+ end
+ end
+
+ # Detects whether the current context has been set to disable tracing.
+ def untraced?(context = nil)
+ context ||= Context.current
+ !!context.value(UNTRACED_KEY)
end
# Returns a URL string with userinfo removed.
#
# @param [String] url The URL string to cleanse.