concurrent-ruby / Thread integration
Also, in the longer term this is also a feature request for first class
concurrent-rubyandThreadsupport that accounts for various use cases like this one, so I will file that separately.
Originally posted by @sl0thentr0py in #2593
see also the ThreadingIntegration and AsyncioIntegration in sentry-python as references.
This pull request provides a small patch to fix the hub propagation and span management issue. It removes the need to explicitly implement child span handling on every invocation with Concurrent Ruby’s futures and promises.
The solution remains lightweight for now, and can evolve this into a more robust integration or a dedicated gem in future iterations.
https://github.com/getsentry/sentry-ruby/pull/2735
Implementation Example
# sentry_no_instrumentation.rb
require 'sentry-ruby'
require 'concurrent'
require_relative 'lib/concurrent_ruby.rb'. #patch
# Initialize Sentry
Sentry.init do |config|
config.dsn = <DSN> # just for testing
config.traces_sample_rate = 1.0
config.logger.level = :debug
end
transaction = Sentry.start_transaction(name: "demo_transaction")
Sentry.get_current_scope.set_span(transaction)
hub_to_propagate = Sentry.get_current_hub
# Create futures and now hub is automatically propagated
futures = 3.times.map do |i|
Concurrent::Promises.future do
# No need for `with_child_span`
puts "Inside future #{i}"
sleep 0.1
i * 100
end
end
puts "Results: #{futures.map(&:value)}"
transaction.finish