Class: Foam::Otel::SessionStitching::LogRecordProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/foam/otel/session_stitching.rb

Overview

Stamps session baggage onto every log record at emit (pipelines.rb registers it right after foam's batch processor, on the emitting thread — before export can serialize the record).

Instance Method Summary collapse

Instance Method Details

#force_flush(timeout: nil) ⇒ Object



107
# File 'lib/foam/otel/session_stitching.rb', line 107

def force_flush(timeout: nil) = OpenTelemetry::SDK::Logs::Export::SUCCESS

#on_emit(log_record, context) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/foam/otel/session_stitching.rb', line 80

def on_emit(log_record, context)
  stamp = SessionStitching.session_attributes(context)
  return if stamp.empty?

  existing = log_record.attributes || {}
  added = stamp.count { |key, _| !existing.key?(key) }
  # Assign a REBUILT hash — the record's current attributes may be
  # frozen or shared; never mutate them in place. Fill-only: existing
  # keys win (fleet uniformity with js/python — never overwrite an
  # app-set session.id).
  log_record.attributes = stamp.merge(existing)
  # Keep the SDK's dropped-count accounting consistent: the exporter
  # computes dropped_attributes_count = total_recorded_attributes -
  # attributes.size (logs-sdk log_record.rb:92 / logs_exporter.rb:304
  # — an unsigned protobuf field), so growing the hash WITHOUT
  # bumping the total makes the count NEGATIVE and the encoder drops
  # the whole export request. Ivar poke, pinned to the capped
  # logs-sdk range (gemspec: >= 0.6, < 0.7); a record shape without
  # the ivar is left alone.
  if added.positive? && log_record.instance_variable_defined?(:@total_recorded_attributes)
    total = log_record.instance_variable_get(:@total_recorded_attributes).to_i
    log_record.instance_variable_set(:@total_recorded_attributes, total + added)
  end
rescue StandardError, SystemStackError
  nil
end

#shutdown(timeout: nil) ⇒ Object



108
# File 'lib/foam/otel/session_stitching.rb', line 108

def shutdown(timeout: nil) = OpenTelemetry::SDK::Logs::Export::SUCCESS