Class: Langfuse::OtelExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/langfuse/otel_exporter.rb

Overview

Converts batched Langfuse events into OTLP/HTTP JSON (ExportTraceServiceRequest) and sends them to the Langfuse OTEL endpoint for v4-compatible ingestion.

Constant Summary collapse

OTEL_ENDPOINT =
'/api/public/otel/v1/traces'
OBSERVATION_EVENT_TYPES =

Event types that carry the full state of an observation on every emit, so a later event supersedes the earlier one for the same observation id.

%w[span-create span-update generation-create generation-update].freeze
USAGE_INPUT_KEYS =

Token keys accepted on the legacy usage object, in priority order.

%w[promptTokens prompt_tokens inputTokens input_tokens input].freeze
USAGE_OUTPUT_KEYS =
%w[completionTokens completion_tokens outputTokens output_tokens output].freeze
USAGE_TOTAL_KEYS =
%w[totalTokens total_tokens total].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:, debug: false, logger: nil) ⇒ OtelExporter

Returns a new instance of OtelExporter.

Parameters:

  • connection (Faraday::Connection) —

    HTTP connection to Langfuse host

  • debug (Boolean) (defaults to: false) —

    whether to print debug output

  • logger (Logger, nil) (defaults to: nil) —

    logger for debug output



44
45
46
47
48
# File 'lib/langfuse/otel_exporter.rb', line 44

def initialize(connection:, debug: false, logger: nil)
  @connection = connection
  @debug = debug
  @logger = logger
end

Class Method Details

.to_otel_span_id(id_str) ⇒ Object

Convert an ID (UUID or hex string) to an OTEL 16-char hex span ID. OTEL span IDs are 8 bytes (16 hex chars). Native hex IDs pass through unchanged.



33
34
35
36
37
38
# File 'lib/langfuse/otel_exporter.rb', line 33

def to_otel_span_id(id_str)
  return '0' * 16 unless id_str

  hex = id_str.to_s.delete('-')
  hex[0, 16]
end

.to_otel_trace_id(id_str) ⇒ Object

Convert an ID (UUID or hex string) to an OTEL 32-char hex trace ID. OTEL trace IDs are 16 bytes (32 hex chars). Native hex IDs pass through unchanged.



24
25
26
27
28
29
# File 'lib/langfuse/otel_exporter.rb', line 24

def to_otel_trace_id(id_str)
  return '0' * 32 unless id_str

  hex = id_str.to_s.delete('-')
  hex.ljust(32, '0')[0, 32]
end

Instance Method Details

#export(events) ⇒ Faraday::Response

Export a batch of Langfuse events as OTLP spans. Note: score-create events are not part of the OTLP mapping and are handled separately by the client via the ingestion API.

Parameters:

  • events (Array<Hash>) —

    array of event hashes from the event queue

Returns:

  • (Faraday::Response)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/langfuse/otel_exporter.rb', line 55

def export(events)
  resource_spans = build_resource_spans(events)
  payload = { resourceSpans: resource_spans }

  log_debug { "OTEL export payload: #{JSON.pretty_generate(payload)}" }

  @connection.post(OTEL_ENDPOINT) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = JSON.generate(payload)
  end
end