Skip to content

Commit

Permalink
Make sure isolated envelopes respect enabled_environments
Browse files Browse the repository at this point in the history
Envelopes for metrics/sessions and so on were queued directly before and
thus did not respect the config. This introduces a dedicated
`capture_envelope` on the client to centralize these checks.

Closes #2287
  • Loading branch information
sl0thentr0py committed Apr 8, 2024
1 parent 17959d8 commit 7bbbb4c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry-ruby.rb
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions sentry-ruby/lib/sentry/client.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions sentry-ruby/lib/sentry/metrics/aggregator.rb
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions sentry-ruby/lib/sentry/session_flusher.rb
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/spec/sentry/session_flusher_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 7bbbb4c

Please sign in to comment.