open-telemetry/opentelemetry-ruby

Adding backtraces to error logs

gabriel1402 opened this issue · 2 comments

Hello!

First, thanks for all the hard work done in this library. Some time ago, we were experiencing an error within our app. The error was correctly logged, however, it was a bit cryptic itself, and it helped shed some light when we monkey-patched this method from the library, by adding the backtrace:

def error_handler
@error_handler ||= ->(exception: nil, message: nil) { logger.error("OpenTelemetry error: #{[message, exception&.message].compact.join(' - ')}") }
end

This is how we monkey-patched it:

module OpenTelemetryExtensions
  module OpenTelemetry
    def error_handler
      @error_handler = ->(exception: nil, message: nil) { logger.error("OpenTelemetry error: #{[message, exception&.message, exception&.backtrace&.join("\n")].compact.join(' - ')}") }
    end
  end
end

Was this originally conceived by design, or could this be an addition that might be useful for other developers?

You should be able to override the error handler without monkey patching:

attr_writer :propagation, :logger, :error_handler

You can see an examples of how we do it as part of initialization for our test helpers:

https://github.com/search?q=repo%3Aopen-telemetry%2Fopentelemetry-ruby-contrib%20error_handler&type=code

👋 This issue has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this issue will be closed eventually by the stale bot.