Class: OpenTelemetry::Trace::Tracer
- Inherits:
-
Object
- Object
- OpenTelemetry::Trace::Tracer
- Defined in:
- lib/opentelemetry/trace/tracer.rb
Overview
No-op implementation of Tracer.
Instance Method Summary collapse
-
#context_with_span(span, parent_context: Context.current) ⇒ Object
Returns a context containing the span, derived from the optional parent context, or the current context if one was not provided.
-
#current_span(context = nil) ⇒ Object
Returns the current span from the current or provided context.
-
#in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, with_parent: nil) {|span, context| ... } ⇒ Object
This is a helper for the default use-case of extending the current trace with a span.
- #start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil) ⇒ Object
-
#start_span(name, with_parent: 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.
-
#with_span(span) {|span, context| ... } ⇒ Object
Activates/deactivates the Span within the current Context, which makes the "current span" available implicitly.
Instance Method Details
#context_with_span(span, parent_context: Context.current) ⇒ Object
Returns a context containing the span, derived from the optional parent context, or the current context if one was not provided.
29 30 31 |
# File 'lib/opentelemetry/trace/tracer.rb', line 29 def context_with_span(span, parent_context: Context.current) parent_context.set_value(CURRENT_SPAN_KEY, span) end |
#current_span(context = nil) ⇒ Object
Returns the current span from the current or provided context
19 20 21 22 |
# File 'lib/opentelemetry/trace/tracer.rb', line 19 def current_span(context = nil) 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) {|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.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/opentelemetry/trace/tracer.rb', line 48 def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, with_parent: nil) span = nil span = start_span(name, attributes: attributes, links: links, start_timestamp: , kind: kind, with_parent: with_parent) with_span(span) { |s, c| yield s, c } rescue Exception => e # rubocop:disable Lint/RescueException span&.record_exception(e) span&.status = Status.new(Status::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
72 73 74 |
# File 'lib/opentelemetry/trace/tracer.rb', line 72 def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil) Span.new end |
#start_span(name, with_parent: 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.
84 85 86 87 88 89 90 91 92 |
# File 'lib/opentelemetry/trace/tracer.rb', line 84 def start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil) span_context = current_span(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.
68 69 70 |
# File 'lib/opentelemetry/trace/tracer.rb', line 68 def with_span(span) Context.with_value(CURRENT_SPAN_KEY, span) { |c, s| yield s, c } end |