lib/opentelemetry/trace/tracer.rb in opentelemetry-api-0.2.0 vs lib/opentelemetry/trace/tracer.rb in opentelemetry-api-0.3.0

- old
+ new

@@ -6,59 +6,93 @@ module OpenTelemetry module Trace # No-op implementation of Tracer. class Tracer - CONTEXT_SPAN_KEY = :__span__ - private_constant(:CONTEXT_SPAN_KEY) + EXTRACTED_SPAN_CONTEXT_KEY = Propagation::ContextKeys.extracted_span_context_key + CURRENT_SPAN_KEY = Propagation::ContextKeys.current_span_key - def current_span - Context.get(CONTEXT_SPAN_KEY) || Span::INVALID + private_constant :EXTRACTED_SPAN_CONTEXT_KEY, :CURRENT_SPAN_KEY + + # Returns the current span from the current or provided context + # + # @param [optional Context] context The context to lookup the current + # {Span} from. Defaults to Context.current + def current_span(context = Context.current) + context.value(CURRENT_SPAN_KEY) || Span::INVALID end + # Returns the the active span context from the given {Context}, or current + # if one is not explicitly passed in. The active span context may refer to + # a {SpanContext} that has been extracted. If both a current {Span} and an + # extracted, {SpanContext} exist, the context of the current {Span} will be + # returned. + # + # @param [optional Context] context The context to lookup the active + # {SpanContext} from. + # + def active_span_context(context = nil) + context ||= Context.current + context.value(CURRENT_SPAN_KEY)&.context || + context.value(EXTRACTED_SPAN_CONTEXT_KEY) || + SpanContext::INVALID + end + # This is a helper for the default use-case of extending the current trace with a span. # # With this helper: # # OpenTelemetry.tracer.in_span('do-the-thing') do ... end # # Equivalent without helper: # # OpenTelemetry.tracer.with_span(OpenTelemetry.tracer.start_span('do-the-thing')) do ... end # - # On exit, the Span that was active before calling this method will be reactivated. - def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, sampling_hint: nil, with_parent: nil, with_parent_context: nil) - span = start_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind, sampling_hint: sampling_hint, with_parent: with_parent, with_parent_context: with_parent_context) - with_span(span) { |s| yield s } + # On exit, the Span that was active before calling this method will be reactivated. If an + # exception occurs during the execution of the provided block, it will be recorded on the + # span and reraised. + # @yield [span, context] yields the newly created span and a context containing the + # span to the block. + def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, with_parent: nil, with_parent_context: nil) + span = start_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind, with_parent: with_parent, with_parent_context: with_parent_context) + with_span(span) { |s, c| yield s, c } + rescue Exception => e # rubocop:disable Lint/RescueException + span.record_error(e) + span.status = Status.new(Status::UNKNOWN_ERROR, + description: "Unhandled exception of type: #{e.class}") + raise e ensure span.finish end # Activates/deactivates the Span within the current Context, which makes the "current span" # available implicitly. # # On exit, the Span that was active before calling this method will be reactivated. + # + # @param [Span] span the span to activate + # @yield [span, context] yields span and a context containing the span to the block. def with_span(span) - Context.with(CONTEXT_SPAN_KEY, span) { |s| yield s } + Context.with_value(CURRENT_SPAN_KEY, span) { |c, s| yield s, c } end - def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, sampling_hint: nil) + def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil) Span.new end # Used when a caller wants to manage the activation/deactivation and lifecycle of # the Span and its parent manually. # # Parent context can be either passed explicitly, or inferred from currently activated span. # # @param [optional Span] with_parent Explicitly managed parent Span, overrides # +with_parent_context+. - # @param [optional SpanContext] with_parent_context Explicitly managed. Overridden by + # @param [optional Context] with_parent_context Explicitly managed. Overridden by # +with_parent+. # # @return [Span] - def start_span(name, with_parent: nil, with_parent_context: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil, sampling_hint: nil) - span_context = with_parent&.context || with_parent_context || current_span.context + def start_span(name, with_parent: nil, with_parent_context: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil) + span_context = with_parent&.context || active_span_context(with_parent_context) if span_context.valid? Span.new(span_context: span_context) else Span.new end