Skip to content

Commit 86dcf3c

Browse files
authored
Correct type attribute's usages (#1354)
* Correct Transport's encoding logging * Correct type declaration for events Normal events shouldn't have the `type` field. Only transactions should have `type: "transaction"`. But in the logs, we should still print normal events' type as `"event"`. For more information about the rules, please see https://github.com/getsentry/sentry-ruby/pull/1336/files#r596851969 * Update changelog
1 parent 5337548 commit 86dcf3c

File tree

8 files changed

+27
-27
lines changed

8 files changed

+27
-27
lines changed

sentry-rails/spec/sentry/send_event_job_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
expect(transport.events.count).to eq(1)
5858
event = transport.events.first
59-
expect(event.type).to eq("event")
59+
expect(event.type).to eq(nil)
6060
end
6161

6262
context "when ApplicationJob is not defined" do

sentry-ruby/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Correct type attribute's usages [#1354](https://github.com/getsentry/sentry-ruby/pull/1354)
6+
7+
38
## 4.3.1
49

510
- Add Sentry.set_context helper [#1337](https://github.com/getsentry/sentry-ruby/pull/1337)

sentry-ruby/lib/sentry/client.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def event_from_transaction(transaction)
7272
def send_event(event, hint = nil)
7373
event_type = event.is_a?(Event) ? event.type : event["type"]
7474

75-
if event_type == "event" && configuration.before_send
75+
if event_type != TransactionEvent::TYPE && configuration.before_send
7676
event = configuration.before_send.call(event, hint)
7777

7878
if event.nil?
@@ -85,8 +85,9 @@ def send_event(event, hint = nil)
8585

8686
event
8787
rescue => e
88-
logger.error(LOGGER_PROGNAME) { "#{event_type.capitalize} sending failed: #{e.message}" }
89-
logger.error(LOGGER_PROGNAME) { "Unreported #{event_type.capitalize}: #{Event.get_log_message(event.to_hash)}" }
88+
loggable_event_type = (event_type || "event").capitalize
89+
logger.error(LOGGER_PROGNAME) { "#{loggable_event_type} sending failed: #{e.message}" }
90+
logger.error(LOGGER_PROGNAME) { "Unreported #{loggable_event_type}: #{Event.get_log_message(event.to_hash)}" }
9091
raise
9192
end
9293

@@ -110,8 +111,8 @@ def dispatch_async_event(async_block, event, hint)
110111
async_block.call(event_hash)
111112
end
112113
rescue => e
113-
event_type = event_hash["type"]
114-
logger.error(LOGGER_PROGNAME) { "Async #{event_type} sending failed: #{e.message}" }
114+
loggable_event_type = event_hash["type"] || "event"
115+
logger.error(LOGGER_PROGNAME) { "Async #{loggable_event_type} sending failed: #{e.message}" }
115116
send_event(event, hint)
116117
end
117118

sentry-ruby/lib/sentry/event.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ def rack_env=(env)
100100
end
101101

102102
def type
103-
"event"
104103
end
105104

106105
def to_hash

sentry-ruby/lib/sentry/transaction_event.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Sentry
44
class TransactionEvent < Event
5+
TYPE = "transaction"
6+
57
ATTRIBUTES = %i(
68
event_id level timestamp start_timestamp
79
release environment server_name modules
@@ -17,7 +19,7 @@ def start_timestamp=(time)
1719
end
1820

1921
def type
20-
"transaction"
22+
TYPE
2123
end
2224

2325
def to_hash

sentry-ruby/lib/sentry/transport.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def send_event(event)
2424
return
2525
end
2626

27-
encoded_data = prepare_encoded_event(event)
27+
encoded_data = encode(event)
2828

2929
return nil unless encoded_data
3030

@@ -45,29 +45,22 @@ def generate_auth_header
4545
'Sentry ' + fields.map { |key, value| "#{key}=#{value}" }.join(', ')
4646
end
4747

48-
def encode(event_hash)
49-
event_id = event_hash[:event_id] || event_hash['event_id']
50-
event_type = event_hash[:type] || event_hash['type']
48+
def encode(event)
49+
# Convert to hash
50+
event_hash = event.to_hash
51+
52+
event_id = event_hash[:event_id] || event_hash["event_id"]
53+
item_type = event_hash[:type] || event_hash["type"] || "event"
5154

5255
envelope = <<~ENVELOPE
5356
{"event_id":"#{event_id}","dsn":"#{configuration.dsn.to_s}","sdk":#{Sentry.sdk_meta.to_json},"sent_at":"#{Sentry.utc_now.iso8601}"}
54-
{"type":"#{event_type}","content_type":"application/json"}
57+
{"type":"#{item_type}","content_type":"application/json"}
5558
#{JSON.generate(event_hash)}
5659
ENVELOPE
5760

58-
envelope
59-
end
60-
61-
private
62-
63-
def prepare_encoded_event(event)
64-
# Convert to hash
65-
event_hash = event.to_hash
61+
configuration.logger.info(LOGGER_PROGNAME) { "Sending envelope [#{item_type}] #{event_id} to Sentry" }
6662

67-
event_id = event_hash[:event_id] || event_hash["event_id"]
68-
event_type = event_hash[:type] || event_hash["type"]
69-
configuration.logger.info(LOGGER_PROGNAME) { "Sending #{event_type} #{event_id} to Sentry" }
70-
encode(event_hash)
63+
envelope
7164
end
7265
end
7366
end

sentry-ruby/spec/sentry/rake_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
message = `cd spec/support && bundle exec rake raise_exception 2>&1`
1111
end.join
1212

13-
expect(message).to match(/Sending event [abcdef0-9]+ to Sentry/)
13+
expect(message).to match(/Sending envelope \[event\] [abcdef0-9]+ to Sentry/)
1414
end
1515
end

sentry-ruby/spec/sentry/transport_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
expect(subject.send_event(event)).to eq(event)
116116

117117
expect(io.string).to match(
118-
/INFO -- sentry: Sending event #{event.event_id} to Sentry/
118+
/INFO -- sentry: Sending envelope \[event\] #{event.event_id} to Sentry/
119119
)
120120
end
121121
end

0 commit comments

Comments
 (0)