Class: Tracelit::ErrorSpanProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/tracelit/error_span_processor.rb

Overview

ErrorSpanProcessor ensures error spans are always exported regardless of the sampling decision made at span creation time.

How it works:

  • ErrorAlwaysOnSampler returns RECORD_ONLY (not DROP) for unsampled spans, which ensures this processor’s on_finish is called for every span

  • On span finish, if the span has status ERROR, this processor forces it through the exporter directly, bypassing the BatchSpanProcessor

  • BatchSpanProcessor ignores RECORD_ONLY spans (trace_flags.sampled? false) so there is no double-export for sampled error spans

NOTE: opentelemetry-sdk 1.x uses on_finish (not on_end) as the hook name.

Instance Method Summary collapse

Constructor Details

#initialize(exporter) ⇒ ErrorSpanProcessor

Returns a new instance of ErrorSpanProcessor.



17
18
19
# File 'lib/tracelit/error_span_processor.rb', line 17

def initialize(exporter)
  @exporter = exporter
end

Instance Method Details

#force_flush(timeout: nil) ⇒ Object



39
40
41
# File 'lib/tracelit/error_span_processor.rb', line 39

def force_flush(timeout: nil)
  @exporter.force_flush(timeout: timeout)
end

#on_finish(span) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tracelit/error_span_processor.rb', line 25

def on_finish(span)
  # Skip spans that are not in error — only intervene for errors
  return if span.status.ok?

  # Skip spans that were fully sampled — BatchSpanProcessor handles those.
  # This prevents double-export of error spans on traces that were sampled.
  return if span.context.trace_flags.sampled?

  # Force-export this error span regardless of sampling decision
  @exporter.export([span.to_span_data])
rescue StandardError
  # Never let processor errors propagate to the application
end

#on_start(span, parent_context) ⇒ Object



21
22
23
# File 'lib/tracelit/error_span_processor.rb', line 21

def on_start(span, parent_context)
  # nothing to do at start
end

#shutdown(timeout: nil) ⇒ Object



43
44
45
46
# File 'lib/tracelit/error_span_processor.rb', line 43

def shutdown(timeout: nil)
  # Do not shut down the shared exporter here —
  # the BatchSpanProcessor owns its lifecycle
end