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

Extract envelope construction logic from Transport #1616

Merged
merged 2 commits into from
Nov 17, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 4.8.1

### Refactoring

- Extract envelope construction logic from Transport [#1616](https://github.com/getsentry/sentry-ruby/pull/1616)

## 4.8.0

### Features
Expand Down
23 changes: 23 additions & 0 deletions sentry-ruby/lib/sentry/envelope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Sentry
class Envelope
def initialize(headers)
@headers = headers
@items = []
end

def add_item(headers, payload)
@items << [headers, payload]
end

def to_s
payload = @items.map do |item_headers, item_payload|
<<~ENVELOPE
#{JSON.generate(item_headers)}
#{JSON.generate(item_payload)}
ENVELOPE
end.join("\n")

"#{JSON.generate(@headers)}\n#{payload}"
end
end
end
41 changes: 17 additions & 24 deletions sentry-ruby/lib/sentry/transport.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "json"
require "base64"
require "sentry/envelope"

module Sentry
class Transport
Expand Down Expand Up @@ -103,31 +104,29 @@ def generate_auth_header
def encode(event)
# Convert to hash
event_payload = event.to_hash

event_id = event_payload[:event_id] || event_payload["event_id"]
item_type = get_item_type(event_payload)

envelope_header = {
event_id: event_id,
dsn: @dsn.to_s,
sdk: Sentry.sdk_meta,
sent_at: Sentry.utc_now.iso8601
}

event_header = { type: item_type, content_type: 'application/json' }
envelope = Envelope.new(
{
event_id: event_id,
dsn: @dsn.to_s,
sdk: Sentry.sdk_meta,
sent_at: Sentry.utc_now.iso8601
}
)

envelope = <<~ENVELOPE
#{JSON.generate(envelope_header)}
#{JSON.generate(event_header)}
#{JSON.generate(event_payload)}
ENVELOPE
envelope.add_item(
{ type: item_type, content_type: 'application/json' },
event_payload
)

client_report = fetch_pending_client_report
envelope << client_report if client_report
client_report_headers, client_report_payload = fetch_pending_client_report
envelope.add_item(client_report_headers, client_report_payload) if client_report_headers

log_info("Sending envelope [#{item_type}] #{event_id} to Sentry")

envelope
envelope.to_s
end

def record_lost_event(reason, item_type)
Expand Down Expand Up @@ -159,21 +158,15 @@ def fetch_pending_client_report
end

item_header = { type: 'client_report' }

item_payload = {
timestamp: Sentry.utc_now.iso8601,
discarded_events: discarded_events_hash
}

client_report_item = <<~CLIENT_REPORT_ITEM
#{JSON.generate(item_header)}
#{JSON.generate(item_payload)}
CLIENT_REPORT_ITEM

@discarded_events = Hash.new(0)
@last_client_report_sent = Time.now

client_report_item
[item_header, item_payload]
end
end
end
Expand Down