Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add spans to Trace::ExportError #1582

Merged
merged 4 commits into from
Jan 25, 2024
Merged

Conversation

fbogsany
Copy link
Contributor

@fbogsany fbogsany commented Jan 25, 2024

This adds a spans attribute to SDK::Trace::ExportError, containing the array of spans that could not be exported. It refactors the BSP a little to ensure an array of Span rather than (sometimes) an array of SpanData is wrapped by the exception.

The intent is to allow a pluggable error handler to recognize the error type, retrieve the array of spans, and (for example) report more detailed information (such as the trace IDs of the spans that are dropped).

Example:

OpenTelemetry.error_handler = ->(exception: nil, message: nil) do
  case exception
  when OpenTelemetry::SDK::Trace::ExportError
    logger.error("OpenTelemetry error: #{exception.message} from trace IDs: #{exception.spans.collect {|span| span.context.trace_id }.uniq.join(', ')}")
  else
    logger.error("OpenTelemetry error: #{[message, exception&.message].compact.join(' - ')}")
  end
end

Comment on lines 15 to 17
attr_reader :spans

def initialize(spans)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be useful to provide documentation for the spans arg to initialize and the type of the spans attribute.

@plantfansam
Copy link
Contributor

Proving to myself that you can do instance vars + attr_reader on an exception

class FooError < StandardError
  attr_reader :str
  def initialize(str)
    super("str")
    @str = str
  end
end

begin
  raise FooError.new("hi")
rescue FooError => e
  pp "in rescue block"
  pp e.str
end

ruby foo.rb
"in rescue block"
"hi"

@@ -183,35 +183,32 @@ def reset_on_fork(restart_thread: true)
OpenTelemetry.handle_error(exception: e, message: 'unexpected error in BatchSpanProcessor#reset_on_fork')
end

def export_batch(batch, timeout: @exporter_timeout_seconds)
def export_batch(span_array, timeout: @exporter_timeout_seconds)
batch = span_array.map(&:to_span_data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ calling to_span_data here instead of elsewhere in bsp
✅ no longer mutating span_array so that we can pass an array of Spans to report_result

@fbogsany fbogsany merged commit 200ea02 into main Jan 25, 2024
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants