Module: Traces::Backend::OpenTelemetry::Interface
- Defined in:
- lib/traces/backend/open_telemetry/interface.rb
Overview
Provides the interface implementation for OpenTelemetry tracing backend.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Checks if there is currently an active trace span.
-
#current_context ⇒ Object
Gets the current OpenTelemetry context.
-
#extract(headers) ⇒ Object
Extracts trace context from headers.
-
#inject(headers = nil, context = nil) ⇒ Object
Injects trace context into headers for propagation.
-
#trace(name, attributes: nil, &block) ⇒ Object
Creates a trace span with the given name and attributes.
-
#trace_context(span = ::OpenTelemetry::Trace.current_span) ⇒ Object
Gets the current trace context from the active span.
-
#trace_context=(context) ⇒ Object
Sets the current trace context.
-
#with_context(context) ⇒ Object
Executes code within the given context or attaches the context.
Instance Method Details
#active? ⇒ Boolean
Checks if there is currently an active trace span.
49 50 51 52 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 49 def active? # Check if there's a real active trace using OpenTelemetry's INVALID span: ::OpenTelemetry::Trace.current_span != ::OpenTelemetry::Trace::Span::INVALID end |
#current_context ⇒ Object
Gets the current OpenTelemetry context.
80 81 82 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 80 def current_context ::OpenTelemetry::Context.current end |
#extract(headers) ⇒ Object
Extracts trace context from headers.
121 122 123 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 121 def extract(headers) ::OpenTelemetry.propagation.extract(headers) end |
#inject(headers = nil, context = nil) ⇒ Object
Injects trace context into headers for propagation.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 102 def inject(headers = nil, context = nil) context ||= ::OpenTelemetry::Context.current headers ||= Hash.new count = headers.count ::OpenTelemetry.propagation.inject(headers, context: context) if count == headers.count # No injection was performed, so return nil: headers = nil end return headers end |
#trace(name, attributes: nil, &block) ⇒ Object
Creates a trace span with the given name and attributes.
25 26 27 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 25 def trace(name, attributes: nil, &block) TRACER.in_span(name, attributes: attributes&.transform_keys(&:to_s), &block) end |
#trace_context(span = ::OpenTelemetry::Trace.current_span) ⇒ Object
Gets the current trace context from the active span.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 57 def trace_context(span = ::OpenTelemetry::Trace.current_span) # Return nil if no active trace (using INVALID span check): return nil if span == ::OpenTelemetry::Trace::Span::INVALID if span_context = span.context flags = 0 if span_context.trace_flags.sampled? flags |= Context::SAMPLED end return Context.new( span_context.trace_id, span_context.span_id, flags, span_context.tracestate, remote: span_context.remote? ) end end |
#trace_context=(context) ⇒ Object
Sets the current trace context.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 31 def trace_context=(context) if context span_context = ::OpenTelemetry::Trace::SpanContext.new( trace_id: context.trace_id, span_id: context.parent_id, trace_flags: ::OpenTelemetry::Trace::TraceFlags.from_byte(context.flags), tracestate: context.state, remote: context.remote? ) span = ::OpenTelemetry::Trace.non_recording_span(span_context) context = ::OpenTelemetry::Trace.context_with_span(span) ::OpenTelemetry::Context.attach(context) end end |
#with_context(context) ⇒ Object
Executes code within the given context or attaches the context.
88 89 90 91 92 93 94 95 96 |
# File 'lib/traces/backend/open_telemetry/interface.rb', line 88 def with_context(context) if block_given? ::OpenTelemetry::Context.with_current(context) do yield end else ::OpenTelemetry::Context.attach(context) end end |