Class: OpenTelemetry::Trace::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/trace/tracer.rb

Overview

No-op implementation of Tracer.

Instance Method Summary collapse

Instance Method Details

#active_span_context(context = nil) ⇒ Object

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.

Parameters:

  • context (optional Context) (defaults to: nil)

    The context to lookup the active SpanContext from.



33
34
35
36
37
38
# File 'lib/opentelemetry/trace/tracer.rb', line 33

def active_span_context(context = nil)
  context ||= Context.current
  context.value(CURRENT_SPAN_KEY)&.context ||
    context.value(EXTRACTED_SPAN_CONTEXT_KEY) ||
    SpanContext::INVALID
end

#current_span(context = Context.current) ⇒ Object

Returns the current span from the current or provided context

Parameters:

  • context (optional Context) (defaults to: Context.current)

    The context to lookup the current Span from. Defaults to Context.current



20
21
22
# File 'lib/opentelemetry/trace/tracer.rb', line 20

def current_span(context = Context.current)
  context.value(CURRENT_SPAN_KEY) || Span::INVALID
end

#in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, with_parent: nil, with_parent_context: nil) {|span, context| ... } ⇒ Object

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. If an exception occurs during the execution of the provided block, it will be recorded on the span and reraised.

Yields:

  • (span, context)

    yields the newly created span and a context containing the span to the block.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opentelemetry/trace/tracer.rb', line 55

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

#start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil) ⇒ Object



78
79
80
# File 'lib/opentelemetry/trace/tracer.rb', line 78

def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
  Span.new
end

#start_span(name, with_parent: nil, with_parent_context: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil) ⇒ Span

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.

Parameters:

  • with_parent (optional Span) (defaults to: nil)

    Explicitly managed parent Span, overrides +with_parent_context+.

  • with_parent_context (optional Context) (defaults to: nil)

    Explicitly managed. Overridden by +with_parent+.

Returns:



93
94
95
96
97
98
99
100
# File 'lib/opentelemetry/trace/tracer.rb', line 93

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
end

#with_span(span) {|span, context| ... } ⇒ Object

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.

Parameters:

  • span (Span)

    the span to activate

Yields:

  • (span, context)

    yields span and a context containing the span to the block.



74
75
76
# File 'lib/opentelemetry/trace/tracer.rb', line 74

def with_span(span)
  Context.with_value(CURRENT_SPAN_KEY, span) { |c, s| yield s, c }
end