Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record :network_error client reports for send_envelope #2295

Merged
merged 2 commits into from Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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)

Check warning on line 212 in sentry-ruby/lib/sentry/client.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/client.rb#L211-L212

Added lines #L211 - L212 were not covered by tests
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