Module: Sashiko::Ractor::Sink

Defined in:
lib/sashiko/ractor.rb

Overview

Main-Ractor replayer. Takes a batch of SpanEvents from a worker and re-emits them as real OTel spans with their recorded timing and the correct parent chain.

Class Method Summary collapse

Class Method Details

.replay(events, parent_carrier:, tracer: nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/sashiko/ractor.rb', line 162

def replay(events, parent_carrier:, tracer: nil)
  return if events.empty?
  tracer ||= Sashiko.tracer
  parent_ctx = OpenTelemetry.propagation.extract(parent_carrier)
  replayed = {} #: Hash[Integer, untyped]

  events.sort_by(&:id).each do |event|
    ctx = if event.parent_id.nil?
            parent_ctx
          elsif (parent = replayed[event.parent_id])
            OpenTelemetry::Trace.context_with_span(parent)
          else
            parent_ctx
          end

    OpenTelemetry::Context.with_current(ctx) do
      span = tracer.start_span(
        event.name,
        kind: event.kind,
        attributes: event.attributes,
        start_timestamp: Time.at(0, event.start_ns, :nanosecond),
      )
      if event.status_error
        span.status = OpenTelemetry::Trace::Status.error(event.status_error)
      end
      span.finish(end_timestamp: Time.at(0, event.end_ns, :nanosecond))
      replayed[event.id] = span
    end
  end
end