sentry-ruby icon indicating copy to clipboard operation
sentry-ruby copied to clipboard

concurrent-ruby / Thread integration

Open sl0thentr0py opened this issue 10 months ago • 1 comments

Also, in the longer term this is also a feature request for first class concurrent-ruby and Thread support 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.

sl0thentr0py avatar Apr 09 '25 12:04 sl0thentr0py

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

Ayush-developer avatar Oct 05 '25 10:10 Ayush-developer