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 23, 2024
1 parent c16a0f2 commit 7c0b880
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 85 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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))

### Bug Fixes

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, 'span', 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 @@ -168,6 +178,7 @@ def event_from_transaction(transaction)
def send_event(event, hint = nil)
event_type = event.is_a?(Event) ? event.type : event["type"]
data_category = Envelope::Item.data_category(event_type)
spans_before = event.is_a?(TransactionEvent) ? event.spans.size : 0

if event_type != TransactionEvent::TYPE && configuration.before_send
event = configuration.before_send.call(event, hint)
Expand All @@ -184,8 +195,13 @@ def send_event(event, hint = nil)

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, 'span', 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
Loading

0 comments on commit 7c0b880

Please sign in to comment.