Sentry-ruby: Replicate Raven.capture using sentry-ruby

Created on 5 Feb 2021  Â·  2Comments  Â·  Source: getsentry/sentry-ruby

Describe the bug

Hello, I'm trying to migrate from sentry-raven to sentry-ruby, but I haven't been able to figure out how to replicate this behaviour:

Raven.capture do
  # capture any exceptions which happen during execution of this block
  1 / 0
end

https://github.com/getsentry/sentry-ruby/tree/master/sentry-raven#usage

It seems that there is not an equivalent Sentry.capture method and I couldn't find it in the migration guide.

I'm using this in a Ruby AWS Lambda function, which is why I'm looking to use a capture block to essentially wrap my entire application code, rather than being able to use something like the rails integration.

Is anyone able to provide some guidance? Thanks!

To Reproduce

Sentry.capture do
  # capture any exceptions which happen during execution of this block
  1 / 0
end

Expected behavior

Capture the exception.

Actual behavior

NoMethodError: undefined method `capture' for Sentry:Module

Environment

  • Ruby Version: 2.7
  • SDK Version: sentry-ruby 4.2.0
  • Integration Versions (if any): N/A

Raven Config

ruby Sentry.init do |config| config.dsn = ENV['SENTRY_DSN'] end

wontfix

Most helpful comment

For anyone else reading this, the best way I've found to do this so far is:

  # Your application code.
  1 / 0
rescue ScriptError, StandardError => e # Figure out what errors are right for you.
  Sentry.capture_exception(e)
  raise
end

And specific to using this is in AWS Lambda, I also found that I had to disable the default asynchronous sending to get it to work – presumably this is because the Lambda function would exit before my Sentry error was able to be logged, but I didn't spend the time to confirm the cause.

Sentry.init do |config|
  config.dsn = ENV['SENTRY_DSN']
  config.background_worker_threads = 0 # Force synchronous sending.
end

Docs: https://github.com/getsentry/sentry-ruby#blocking-vs-non-blocking

All 2 comments

For anyone else reading this, the best way I've found to do this so far is:

  # Your application code.
  1 / 0
rescue ScriptError, StandardError => e # Figure out what errors are right for you.
  Sentry.capture_exception(e)
  raise
end

And specific to using this is in AWS Lambda, I also found that I had to disable the default asynchronous sending to get it to work – presumably this is because the Lambda function would exit before my Sentry error was able to be logged, but I didn't spend the time to confirm the cause.

Sentry.init do |config|
  config.dsn = ENV['SENTRY_DSN']
  config.background_worker_threads = 0 # Force synchronous sending.
end

Docs: https://github.com/getsentry/sentry-ruby#blocking-vs-non-blocking

thanks for reporting this and provide an example. but to keep sentry-ruby's APIs align with other SDKs, we don't plan to bring Sentry.capture back atm 🙂

Was this page helpful?
0 / 5 - 0 ratings