Skip to content

Commit 200ea02

Browse files
feat: add spans to Trace::ExportError (#1582)
* feat: add spans to Trace::ExportError * add class comment * document initialization argument * Apply suggestions from code review --------- Co-authored-by: Sam Handler <[email protected]>
1 parent 4b37bb3 commit 200ea02

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

sdk/lib/opentelemetry/sdk/trace/export.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ module Trace
1010
# The Export module contains the built-in exporters and span processors for the OpenTelemetry
1111
# reference implementation.
1212
module Export
13-
ExportError = Class.new(OpenTelemetry::Error)
13+
# Raised when an export fails; spans are available via :spans accessor
14+
class ExportError < OpenTelemetry::Error
15+
# Returns the {Span} array for this exception
16+
#
17+
# @return [Array<OpenTelemetry::SDK::Trace::Span>]
18+
attr_reader :spans
19+
20+
# @param [Array<OpenTelemetry::SDK::Trace::Span>] spans the array of spans that failed to export
21+
def initialize(spans)
22+
super("Unable to export #{spans.size} spans")
23+
@spans = spans
24+
end
25+
end
1426

1527
# Result codes for the SpanExporter#export method and the SpanProcessor#force_flush and SpanProcessor#shutdown methods.
1628

sdk/lib/opentelemetry/sdk/trace/export/batch_span_processor.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def force_flush(timeout: nil) # rubocop:disable Metrics/MethodLength
110110
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
111111
return TIMEOUT if remaining_timeout&.zero?
112112

113-
batch = snapshot.shift(@batch_size).map!(&:to_span_data)
113+
batch = snapshot.shift(@batch_size)
114114
result_code = export_batch(batch, timeout: remaining_timeout)
115115
return result_code unless result_code == SUCCESS
116116
end
@@ -162,7 +162,7 @@ def work
162162
@condition.wait(@mutex, @delay_seconds) while spans.empty? && @keep_running
163163
return unless @keep_running
164164

165-
fetch_batch
165+
spans.shift(@batch_size)
166166
end
167167

168168
@metrics_reporter.observe_value('otel.bsp.buffer_utilization', value: spans.size / max_queue_size.to_f)
@@ -183,35 +183,32 @@ def reset_on_fork(restart_thread: true)
183183
OpenTelemetry.handle_error(exception: e, message: 'unexpected error in BatchSpanProcessor#reset_on_fork')
184184
end
185185

186-
def export_batch(batch, timeout: @exporter_timeout_seconds)
186+
def export_batch(span_array, timeout: @exporter_timeout_seconds)
187+
batch = span_array.map(&:to_span_data)
187188
result_code = @export_mutex.synchronize { @exporter.export(batch, timeout: timeout) }
188-
report_result(result_code, batch)
189+
report_result(result_code, span_array)
189190
result_code
190191
rescue StandardError => e
191-
report_result(FAILURE, batch)
192+
report_result(FAILURE, span_array)
192193
@metrics_reporter.add_to_counter('otel.bsp.error', labels: { 'reason' => e.class.to_s })
193194
FAILURE
194195
end
195196

196-
def report_result(result_code, batch)
197+
def report_result(result_code, span_array)
197198
if result_code == SUCCESS
198199
@metrics_reporter.add_to_counter('otel.bsp.export.success')
199-
@metrics_reporter.add_to_counter('otel.bsp.exported_spans', increment: batch.size)
200+
@metrics_reporter.add_to_counter('otel.bsp.exported_spans', increment: span_array.size)
200201
else
201-
OpenTelemetry.handle_error(exception: ExportError.new("Unable to export #{batch.size} spans"))
202+
OpenTelemetry.handle_error(exception: ExportError.new(span_array))
202203
@metrics_reporter.add_to_counter('otel.bsp.export.failure')
203-
report_dropped_spans(batch, reason: 'export-failure')
204+
report_dropped_spans(span_array, reason: 'export-failure')
204205
end
205206
end
206207

207208
def report_dropped_spans(dropped_spans, reason:, function: nil)
208209
@metrics_reporter.add_to_counter('otel.bsp.dropped_spans', increment: dropped_spans.size, labels: { 'reason' => reason, OpenTelemetry::SemanticConventions::Trace::CODE_FUNCTION => function }.compact)
209210
end
210211

211-
def fetch_batch
212-
spans.shift(@batch_size).map!(&:to_span_data)
213-
end
214-
215212
def lock
216213
@mutex.synchronize do
217214
yield

0 commit comments

Comments
 (0)