Skip to content

Commit

Permalink
Record :network_error client reports for send_envelope (#2295)
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Apr 11, 2024
1 parent 0257494 commit 377cbdd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- Update key, unit and tags sanitization logic for metrics [#2292](https://github.com/getsentry/sentry-ruby/pull/2292)
- Consolidate client report and rate limit handling with data categories [#2294](https://github.com/getsentry/sentry-ruby/pull/2294)
- Record `:network_error` client reports for `send_envelope` [#2295](https://github.com/getsentry/sentry-ruby/pull/2295)

### Bug Fixes

Expand Down
7 changes: 5 additions & 2 deletions sentry-ruby/lib/sentry/client.rb
Expand Up @@ -206,9 +206,12 @@ 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)

envelope.items.map(&:data_category).each do |data_category|
transport.record_lost_event(:network_error, data_category)
end

raise
end

Expand Down
40 changes: 29 additions & 11 deletions sentry-ruby/spec/sentry/client_spec.rb
Expand Up @@ -98,7 +98,9 @@ def sentry_context
describe '#send_envelope' do
let(:envelope) do
envelope = Sentry::Envelope.new({ env_header: 1 })
envelope.add_item({ item_header: 42 }, { payload: 'test' })
envelope.add_item({ type: 'event' }, { payload: 'test' })
envelope.add_item({ type: 'statsd' }, { payload: 'test2' })
envelope.add_item({ type: 'transaction' }, { payload: 'test3' })
envelope
end

Expand All @@ -122,18 +124,34 @@ def sentry_context
subject.send_envelope(envelope)
end

it 'logs error when transport failure' do
string_io = StringIO.new
configuration.debug = true
configuration.logger = ::Logger.new(string_io)
expect(subject.transport).to receive(:send_envelope).and_raise(Sentry::ExternalError.new("networking error"))
context 'when transport failure' do
let(:string_io) { StringIO.new }

before do
configuration.debug = true
configuration.logger = ::Logger.new(string_io)

allow(subject.transport).to receive(:send_envelope).and_raise(Sentry::ExternalError.new("networking error"))
end

expect do
subject.send_envelope(envelope)
end.to raise_error(Sentry::ExternalError)
it 'logs error' do
expect do
subject.send_envelope(envelope)
end.to raise_error(Sentry::ExternalError)

expect(string_io.string).to match(/Envelope sending failed: networking error/)
expect(string_io.string).to match(__FILE__)
expect(string_io.string).to match(/Envelope sending failed: networking error/)
expect(string_io.string).to match(__FILE__)
end

it 'records client reports for network errors' do
expect do
subject.send_envelope(envelope)
end.to raise_error(Sentry::ExternalError)

expect(subject.transport).to have_recorded_lost_event(:network_error, 'error')
expect(subject.transport).to have_recorded_lost_event(:network_error, 'metric_bucket')
expect(subject.transport).to have_recorded_lost_event(:network_error, 'transaction')
end
end
end

Expand Down

0 comments on commit 377cbdd

Please sign in to comment.