Skip to content

Commit

Permalink
Add client reports for span drop counts
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Jul 19, 2024
1 parent 36866c5 commit a7c42c9
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

### Internal

- Use Concurrent.usable_processor_count when it is available ([#2339](https://github.com/getsentry/sentry-ruby/pull/2339))
- Use `Concurrent.usable_processor_count` when it is available ([#2339](https://github.com/getsentry/sentry-ruby/pull/2339))
- Report dropped spans in Client Reports ([#2346](https://github.com/getsentry/sentry-ruby/pull/2346))

## 5.18.1

Expand Down
23 changes: 20 additions & 3 deletions sentry-ruby/lib/sentry/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,29 @@ def capture_event(event, scope, hint = {})

event_type = event.is_a?(Event) ? event.type : event["type"]
data_category = Envelope::Item.data_category(event_type)

is_transaction = event.is_a?(TransactionEvent)
spans_before = is_transaction ? event.spans.size : 0

event = scope.apply_to_event(event, hint)

if event.nil?
log_debug("Discarded event because one of the event processors returned nil")
transport.record_lost_event(:event_processor, data_category)
transport.record_lost_event(:event_processor, 'span', num: spans_before + 1) if is_transaction
return
elsif is_transaction
spans_delta = spans_before - event.spans.size
transport.record_lost_event(:event_processor, data_category, num: spans_delta) if spans_delta > 0
end

if async_block = configuration.async
dispatch_async_event(async_block, event, hint)
elsif configuration.background_worker_threads != 0 && hint.fetch(:background, true)
queued = dispatch_background_event(event, hint)
transport.record_lost_event(:queue_overflow, data_category) unless queued
unless dispatch_background_event(event, hint)
transport.record_lost_event(:queue_overflow, data_category)
transport.record_lost_event(:queue_overflow, 'span', num: spans_before + 1) if is_transaction
end
else
send_event(event, hint)
end
Expand Down Expand Up @@ -180,12 +190,18 @@ def send_event(event, hint = nil)
end

if event_type == TransactionEvent::TYPE && configuration.before_send_transaction
spans_before = event.is_a?(TransactionEvent) ? event.spans.size : 0
event = configuration.before_send_transaction.call(event, hint)

if event.nil?
log_debug("Discarded event because before_send_transaction returned nil")
transport.record_lost_event(:before_send, data_category)
transport.record_lost_event(:before_send, 'transaction')
transport.record_lost_event(:before_send, 'span', num: spans_before + 1)
return
else
spans_after = event.is_a?(TransactionEvent) ? event.spans.size : 0
spans_delta = spans_before - spans_after
transport.record_lost_event(:before_send, data_category, num: spans_delta) if spans_delta > 0
end
end

Expand All @@ -196,6 +212,7 @@ def send_event(event, hint = nil)
rescue => e
log_error("Event sending failed", e, debug: configuration.debug)
transport.record_lost_event(:network_error, data_category)
transport.record_lost_event(:network_error, 'span', num: spans_before + 1) if event.is_a?(TransactionEvent)
raise
end

Expand Down
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/envelope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def type
# rate limits and client reports use the data_category rather than envelope item type
def self.data_category(type)
case type
when 'session', 'attachment', 'transaction', 'profile' then type
when 'session', 'attachment', 'transaction', 'profile', 'span' then type
when 'sessions' then 'session'
when 'check_in' then 'monitor'
when 'statsd', 'metric_meta' then 'metric_bucket'
Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def finish(hub: nil, end_timestamp: nil)
is_backpressure = Sentry.backpressure_monitor&.downsample_factor&.positive?
reason = is_backpressure ? :backpressure : :sample_rate
hub.current_client.transport.record_lost_event(reason, 'transaction')
hub.current_client.transport.record_lost_event(reason, 'span')
end
end

Expand Down
4 changes: 2 additions & 2 deletions sentry-ruby/lib/sentry/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ def envelope_from_event(event)
envelope
end

def record_lost_event(reason, data_category)
def record_lost_event(reason, data_category, num: 1)
return unless @send_client_reports
return unless CLIENT_REPORT_REASONS.include?(reason)

@discarded_events[[reason, data_category]] += 1
@discarded_events[[reason, data_category]] += num
end

def flush
Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/spec/sentry/envelope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
['sessions', 'session'],
['attachment', 'attachment'],
['transaction', 'transaction'],
['span', 'span'],
['profile', 'profile'],
['check_in', 'monitor'],
['statsd', 'metric_bucket'],
Expand Down
2 changes: 2 additions & 0 deletions sentry-ruby/spec/sentry/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@
it "records lost event with reason sample_rate" do
subject.finish
expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:sample_rate, 'transaction')
expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:sample_rate, 'span')
end
end

Expand All @@ -514,6 +515,7 @@

subject.finish
expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:backpressure, 'transaction')
expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:backpressure, 'span')
end
end

Expand Down
4 changes: 3 additions & 1 deletion sentry-ruby/spec/sentry/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
before do
5.times { subject.record_lost_event(:ratelimit_backoff, 'error') }
3.times { subject.record_lost_event(:queue_overflow, 'transaction') }
2.times { subject.record_lost_event(:network_error, 'span', num: 5) }
end

it "incudes client report in envelope" do
Expand All @@ -170,7 +171,8 @@
timestamp: Time.now.utc.iso8601,
discarded_events: [
{ reason: :ratelimit_backoff, category: 'error', quantity: 5 },
{ reason: :queue_overflow, category: 'transaction', quantity: 3 }
{ reason: :queue_overflow, category: 'transaction', quantity: 3 },
{ reason: :network_error, category: 'span', quantity: 10 }
]
}.to_json
)
Expand Down

0 comments on commit a7c42c9

Please sign in to comment.