Class: Braintrust::Trace::SpanExporter
- Inherits:
-
OpenTelemetry::Exporter::OTLP::Exporter
- Object
- OpenTelemetry::Exporter::OTLP::Exporter
- Braintrust::Trace::SpanExporter
- 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
- #export(span_data, timeout: nil) ⇒ Object
-
#initialize(endpoint:, api_key:) ⇒ SpanExporter
constructor
A new instance of SpanExporter.
Constructor Details
#initialize(endpoint:, api_key:) ⇒ SpanExporter
Returns a new instance of SpanExporter.
21 22 23 24 25 |
# File 'lib/braintrust/trace/span_exporter.rb', line 21 def initialize(endpoint:, api_key:) raise State::MissingAPIKeyError, "api_key is required" if api_key.nil? || api_key.empty? super(endpoint: endpoint, headers: {"Authorization" => "Bearer #{api_key}"}) end |
Instance Method Details
#export(span_data, timeout: nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/braintrust/trace/span_exporter.rb', line 27 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 |