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'

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



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

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.



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

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.



15
16
17
18
19
20
# File 'lib/langfuse/otel_exporter.rb', line 15

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)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/langfuse/otel_exporter.rb', line 46

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