Class: Braintrust::Trace::SpanExporter

Inherits:
OpenTelemetry::Exporter::OTLP::Exporter
  • Object
show all
Defined in:
lib/braintrust/trace/span_exporter.rb

Overview

Custom OTLP exporter that groups spans by braintrust.parent attribute and sets the x-bt-parent HTTP header per group. This is required for the Braintrust OTLP backend to route spans to the correct experiment/project.

Thread safety: BatchSpanProcessor serializes export() calls via its @export_mutex, so @headers mutation here is safe.

Constant Summary collapse

PARENT_ATTR_KEY =
SpanProcessor::PARENT_ATTR_KEY
PARENT_HEADER =
"x-bt-parent"
SUCCESS =
OpenTelemetry::SDK::Trace::Export::SUCCESS
FAILURE =
OpenTelemetry::SDK::Trace::Export::FAILURE

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, api_key:) ⇒ SpanExporter

Returns a new instance of SpanExporter.



20
21
22
# File 'lib/braintrust/trace/span_exporter.rb', line 20

def initialize(endpoint:, api_key:)
  super(endpoint: endpoint, headers: {"Authorization" => "Bearer #{api_key}"})
end

Instance Method Details

#export(span_data, timeout: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/braintrust/trace/span_exporter.rb', line 24

def export(span_data, timeout: nil)
  failed = false
  span_data.group_by { |sd| sd.attributes&.[](PARENT_ATTR_KEY) }.each do |parent_value, spans|
    @headers[PARENT_HEADER] = parent_value if parent_value
    failed = true unless super(spans, timeout: timeout) == SUCCESS
  ensure
    @headers.delete(PARENT_HEADER)
  end
  failed ? FAILURE : SUCCESS
end