diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index 79417d40f..439d83ce5 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -257,7 +257,7 @@ def close end if client = get_current_client - client.transport.flush + client.flush if client.configuration.include_local_variables exception_locals_tp.disable diff --git a/sentry-ruby/lib/sentry/client.rb b/sentry-ruby/lib/sentry/client.rb index d5e633739..a337e02a4 100644 --- a/sentry-ruby/lib/sentry/client.rb +++ b/sentry-ruby/lib/sentry/client.rb @@ -77,6 +77,25 @@ def capture_event(event, scope, hint = {}) nil end + # Capture an envelope directly. + # @param envelope [Envelope] the envelope to be captured. + # @return [void] + def capture_envelope(envelope) + Sentry.background_worker.perform do + send_envelope(envelope) + end + rescue => e + log_error("Envelope capturing failed", e, debug: configuration.debug) + nil + end + + # Flush pending events to Sentry. + # @return [void] + def flush + transport.flush if configuration.sending_to_dsn_allowed? + spotlight_transport.flush if spotlight_transport + end + # Initializes an Event object with the given exception. Returns `nil` if the exception's class is excluded from reporting. # @param exception [Exception] the exception to be reported. # @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors. @@ -183,6 +202,19 @@ def send_event(event, hint = nil) raise end + # Send an envelope directly to Sentry. + # @param envelope [Envelope] the envelope to be sent. + # @return [void] + def send_envelope(envelope) + transport.send_envelope(envelope) if configuration.sending_to_dsn_allowed? + spotlight_transport.send_envelope(envelope) if spotlight_transport + rescue => e + # note that we don't record client reports for direct envelope types + # such as metrics, sessions etc + log_error("Envelope sending failed", e, debug: configuration.debug) + raise + end + # @deprecated use Sentry.get_traceparent instead. # # Generates a Sentry trace for distribted tracing from the given Span. diff --git a/sentry-ruby/lib/sentry/metrics/aggregator.rb b/sentry-ruby/lib/sentry/metrics/aggregator.rb index 4624a3559..f8e134a97 100644 --- a/sentry-ruby/lib/sentry/metrics/aggregator.rb +++ b/sentry-ruby/lib/sentry/metrics/aggregator.rb @@ -107,9 +107,7 @@ def flush(force: false) end end - Sentry.background_worker.perform do - @client.transport.send_envelope(envelope) - end + @client.capture_envelope(envelope) end def kill diff --git a/sentry-ruby/lib/sentry/session_flusher.rb b/sentry-ruby/lib/sentry/session_flusher.rb index ea75d4d6f..256320ca7 100644 --- a/sentry-ruby/lib/sentry/session_flusher.rb +++ b/sentry-ruby/lib/sentry/session_flusher.rb @@ -20,12 +20,8 @@ def initialize(configuration, client) def flush return if @pending_aggregates.empty? - envelope = pending_envelope - - Sentry.background_worker.perform do - @client.transport.send_envelope(envelope) - end + @client.capture_envelope(pending_envelope) @pending_aggregates = {} end diff --git a/sentry-ruby/spec/sentry/session_flusher_spec.rb b/sentry-ruby/spec/sentry/session_flusher_spec.rb index b2bf91f82..5f9f53cdf 100644 --- a/sentry-ruby/spec/sentry/session_flusher_spec.rb +++ b/sentry-ruby/spec/sentry/session_flusher_spec.rb @@ -5,6 +5,7 @@ let(:configuration) do Sentry::Configuration.new.tap do |config| + config.dsn = Sentry::TestHelper::DUMMY_DSN config.release = 'test-release' config.environment = 'test' config.transport.transport_class = Sentry::DummyTransport