Skip to content

Commit 2e9c6bb

Browse files
committed
Log message when events are not sent due to rate limiting
1 parent 8752f5d commit 2e9c6bb

File tree

6 files changed

+57
-21
lines changed

6 files changed

+57
-21
lines changed

sentry-ruby/lib/sentry/transport.rb

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Transport
77
USER_AGENT = "sentry-ruby/#{Sentry::VERSION}"
88

99
attr_accessor :configuration
10+
attr_reader :rate_limits
1011

1112
def initialize(configuration)
1213
@configuration = configuration
@@ -20,12 +21,24 @@ def send_data(data, options = {})
2021
end
2122

2223
def send_event(event)
24+
event_hash = event.to_hash
25+
item_type = get_item_type(event_hash)
26+
2327
unless configuration.sending_allowed?
24-
configuration.logger.debug(LOGGER_PROGNAME) { "Event not sent: #{configuration.error_messages}" }
28+
configuration.logger.debug(LOGGER_PROGNAME) do
29+
"Envelope [#{item_type}] not sent: #{configuration.error_messages}"
30+
end
31+
2532
return
2633
end
2734

28-
return if is_rate_limited?(event)
35+
if is_rate_limited?(item_type)
36+
configuration.logger.info(LOGGER_PROGNAME) do
37+
"Envelope [#{item_type}] not sent: rate limiting"
38+
end
39+
40+
return
41+
end
2942

3043
encoded_data = encode(event)
3144

@@ -36,18 +49,14 @@ def send_event(event)
3649
event
3750
end
3851

39-
def is_rate_limited?(event)
40-
event_hash = event.to_hash
41-
event_type = event_hash[:type] || event_hash['type']
42-
52+
def is_rate_limited?(item_type)
4353
# check category-specific limit
4454
delay =
45-
case event_type
46-
when "event"
47-
# confusing mapping, but it's decided by Sentry
48-
@rate_limits["error"]
55+
case item_type
4956
when "transaction"
5057
@rate_limits["transaction"]
58+
else
59+
@rate_limits["error"]
5160
end
5261

5362
# check universal limit if not category limit
@@ -73,7 +82,7 @@ def encode(event)
7382
event_hash = event.to_hash
7483

7584
event_id = event_hash[:event_id] || event_hash["event_id"]
76-
item_type = event_hash[:type] || event_hash["type"] || "event"
85+
item_type = get_item_type(event_hash)
7786

7887
envelope = <<~ENVELOPE
7988
{"event_id":"#{event_id}","dsn":"#{configuration.dsn.to_s}","sdk":#{Sentry.sdk_meta.to_json},"sent_at":"#{Sentry.utc_now.iso8601}"}
@@ -85,6 +94,12 @@ def encode(event)
8594

8695
envelope
8796
end
97+
98+
private
99+
100+
def get_item_type(event_hash)
101+
event_hash[:type] || event_hash["type"] || "event"
102+
end
88103
end
89104
end
90105

sentry-ruby/lib/sentry/transport/http_transport.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class HTTPTransport < Transport
1111
RETRY_AFTER_HEADER = "retry-after"
1212
RATE_LIMIT_HEADER = "x-sentry-rate-limits"
1313

14-
attr_reader :conn, :adapter, :rate_limits
14+
attr_reader :conn, :adapter
1515

1616
def initialize(*args)
1717
super

sentry-ruby/spec/sentry/hub_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
end
120120
end
121121
end
122+
122123
describe '#capture_message' do
123124
let(:message) { "Test message" }
124125

sentry-ruby/spec/sentry/transport/http_transport_rate_limiting_spec.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,37 @@
2121

2222
context "with only category limits" do
2323
it "returns true for still limited category" do
24-
subject.rate_limits.merge!("error" => Time.now + 60)
24+
subject.rate_limits.merge!("error" => Time.now + 60, "transaction" => Time.now + 60)
2525

26-
expect(subject.is_rate_limited?(event.to_hash)).to eq(true)
26+
expect(subject.is_rate_limited?("event")).to eq(true)
27+
expect(subject.is_rate_limited?("transaction")).to eq(true)
2728
end
2829

2930
it "returns false for passed limited category" do
30-
subject.rate_limits.merge!("error" => Time.now - 10)
31+
subject.rate_limits.merge!("error" => Time.now - 10, "transaction" => Time.now - 10)
3132

32-
expect(subject.is_rate_limited?(event.to_hash)).to eq(false)
33+
expect(subject.is_rate_limited?("event")).to eq(false)
34+
expect(subject.is_rate_limited?("transaction")).to eq(false)
3335
end
3436

3537
it "returns false for not listed category" do
3638
subject.rate_limits.merge!("transaction" => Time.now + 10)
3739

38-
expect(subject.is_rate_limited?(event.to_hash)).to eq(false)
40+
expect(subject.is_rate_limited?("event")).to eq(false)
3941
end
4042
end
4143

4244
context "with only universal limits" do
4345
it "returns true when still limited" do
4446
subject.rate_limits.merge!(nil => Time.now + 60)
4547

46-
expect(subject.is_rate_limited?(event.to_hash)).to eq(true)
48+
expect(subject.is_rate_limited?("event")).to eq(true)
4749
end
4850

4951
it "returns false when passed limit" do
5052
subject.rate_limits.merge!(nil => Time.now - 10)
5153

52-
expect(subject.is_rate_limited?(event.to_hash)).to eq(false)
54+
expect(subject.is_rate_limited?("event")).to eq(false)
5355
end
5456
end
5557

@@ -60,7 +62,7 @@
6062
nil => Time.now - 10
6163
)
6264

63-
expect(subject.is_rate_limited?(event.to_hash)).to eq(true)
65+
expect(subject.is_rate_limited?("event")).to eq(true)
6466
end
6567
end
6668
end

sentry-ruby/spec/sentry/transport_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
subject.send_event(event)
9191

9292
logs = string_io.string
93-
expect(logs).to match(/Event not sent: Excluded by random sample/)
93+
expect(logs).to match(/Envelope \[event\] not sent: Excluded by random sample/)
9494
end
9595
end
9696

sentry-ruby/spec/sentry_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@
9999
expect(subject.last_event_id).to eq(nil)
100100
end
101101
end
102+
103+
context "when rate limited" do
104+
let(:string_io) { StringIO.new }
105+
before do
106+
perform_basic_setup do |config|
107+
config.logger = Logger.new(string_io)
108+
config.transport.transport_class = Sentry::HTTPTransport
109+
end
110+
111+
Sentry.get_current_client.transport.rate_limits.merge!("error" => Time.now + 100)
112+
end
113+
114+
it "stops the event and logs correct message" do
115+
described_class.send(capture_helper, capture_subject)
116+
117+
expect(string_io.string).to match(/Envelope \[event\] not sent: rate limiting/)
118+
end
119+
end
102120
end
103121

104122
describe ".send_event" do

0 commit comments

Comments
 (0)