Class: Sentry::Transport
- Inherits:
-
Object
- Object
- Sentry::Transport
- Includes:
- LoggingHelper
- Defined in:
- lib/sentry/transport.rb,
lib/sentry/transport/configuration.rb more...
Direct Known Subclasses
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- PROTOCOL_VERSION =
"7"
- USER_AGENT =
"sentry-ruby/#{Sentry::VERSION}"
- CLIENT_REPORT_INTERVAL =
30
- CLIENT_REPORT_REASONS =
[ :ratelimit_backoff, :queue_overflow, :cache_overflow, # NA :network_error, :sample_rate, :before_send, :event_processor, :insufficient_data, :backpressure ]
Instance Attribute Summary collapse
-
#discarded_events ⇒ Object
readonly
Returns the value of attribute discarded_events.
-
#last_client_report_sent ⇒ Object
readonly
Returns the value of attribute last_client_report_sent.
-
#logger ⇒ Object
readonly
deprecated
Deprecated.
Use Sentry.logger to retrieve the current logger instead.
-
#rate_limits ⇒ Object
readonly
Returns the value of attribute rate_limits.
Instance Method Summary collapse
- #any_rate_limited? ⇒ Boolean
- #envelope_from_event(event) ⇒ Object
- #flush ⇒ Object
-
#initialize(configuration) ⇒ Transport
constructor
A new instance of Transport.
- #is_rate_limited?(data_category) ⇒ Boolean
- #record_lost_event(reason, data_category, num: 1) ⇒ Object
- #send_data(data, options = {}) ⇒ Object
- #send_envelope(envelope) ⇒ Object
- #send_event(event) ⇒ Object
- #serialize_envelope(envelope) ⇒ Object
Constructor Details
permalink #initialize(configuration) ⇒ Transport
Returns a new instance of Transport.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sentry/transport.rb', line 32 def initialize(configuration) @logger = configuration.logger @transport_configuration = configuration.transport @dsn = configuration.dsn @rate_limits = {} @send_client_reports = configuration.send_client_reports if @send_client_reports @discarded_events = Hash.new(0) @last_client_report_sent = Time.now end end |
Instance Attribute Details
permalink #discarded_events ⇒ Object (readonly)
Returns the value of attribute discarded_events.
27 28 29 |
# File 'lib/sentry/transport.rb', line 27 def discarded_events @discarded_events end |
permalink #last_client_report_sent ⇒ Object (readonly)
Returns the value of attribute last_client_report_sent.
27 28 29 |
# File 'lib/sentry/transport.rb', line 27 def last_client_report_sent @last_client_report_sent end |
permalink #logger ⇒ Object (readonly)
Deprecated.
Use Sentry.logger to retrieve the current logger instead.
30 31 32 |
# File 'lib/sentry/transport.rb', line 30 def logger @logger end |
permalink #rate_limits ⇒ Object (readonly)
Returns the value of attribute rate_limits.
27 28 29 |
# File 'lib/sentry/transport.rb', line 27 def rate_limits @rate_limits end |
Instance Method Details
permalink #any_rate_limited? ⇒ Boolean
113 114 115 |
# File 'lib/sentry/transport.rb', line 113 def any_rate_limited? @rate_limits.values.any? { |t| t && t > Time.now } end |
permalink #envelope_from_event(event) ⇒ Object
[View source]
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sentry/transport.rb', line 117 def envelope_from_event(event) # Convert to hash event_payload = event.to_hash event_id = event_payload[:event_id] || event_payload["event_id"] item_type = event_payload[:type] || event_payload["type"] envelope_headers = { event_id: event_id, dsn: @dsn.to_s, sdk: Sentry., sent_at: Sentry.utc_now.iso8601 } if event.is_a?(Event) && event.dynamic_sampling_context envelope_headers[:trace] = event.dynamic_sampling_context end envelope = Envelope.new(envelope_headers) envelope.add_item( { type: item_type, content_type: "application/json" }, event_payload ) if event.is_a?(TransactionEvent) && event.profile envelope.add_item( { type: "profile", content_type: "application/json" }, event.profile ) end if event.is_a?(Event) && event..any? event..each do || envelope.add_item(.to_envelope_headers, .payload) end end client_report_headers, client_report_payload = fetch_pending_client_report envelope.add_item(client_report_headers, client_report_payload) if client_report_headers envelope end |
permalink #flush ⇒ Object
[View source]
167 168 169 170 171 172 173 174 |
# File 'lib/sentry/transport.rb', line 167 def flush client_report_headers, client_report_payload = fetch_pending_client_report(force: true) return unless client_report_headers envelope = Envelope.new envelope.add_item(client_report_headers, client_report_payload) send_envelope(envelope) end |
permalink #is_rate_limited?(data_category) ⇒ Boolean
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sentry/transport.rb', line 91 def is_rate_limited?(data_category) # check category-specific limit category_delay = @rate_limits[data_category] # check universal limit if not category limit universal_delay = @rate_limits[nil] delay = if category_delay && universal_delay if category_delay > universal_delay category_delay else universal_delay end elsif category_delay category_delay else universal_delay end !!delay && delay > Time.now end |
permalink #record_lost_event(reason, data_category, num: 1) ⇒ Object
[View source]
160 161 162 163 164 165 |
# File 'lib/sentry/transport.rb', line 160 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]] += num end |
permalink #send_data(data, options = {}) ⇒ Object
45 46 47 |
# File 'lib/sentry/transport.rb', line 45 def send_data(data, = {}) raise NotImplementedError end |
permalink #send_envelope(envelope) ⇒ Object
[View source]
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sentry/transport.rb', line 56 def send_envelope(envelope) reject_rate_limited_items(envelope) return if envelope.items.empty? data, serialized_items = serialize_envelope(envelope) if data log_debug("[Transport] Sending envelope with items [#{serialized_items.map(&:type).join(', ')}] #{envelope.event_id} to Sentry") send_data(data) end end |
permalink #send_event(event) ⇒ Object
[View source]
49 50 51 52 53 54 |
# File 'lib/sentry/transport.rb', line 49 def send_event(event) envelope = envelope_from_event(event) send_envelope(envelope) event end |
permalink #serialize_envelope(envelope) ⇒ Object
[View source]
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sentry/transport.rb', line 69 def serialize_envelope(envelope) serialized_items = [] serialized_results = [] envelope.items.each do |item| result, oversized = item.serialize if oversized log_debug("Envelope item [#{item.type}] is still oversized after size reduction: {#{item.size_breakdown}}") next end serialized_results << result serialized_items << item end data = [JSON.generate(envelope.headers), *serialized_results].join("\n") unless serialized_results.empty? [data, serialized_items] end |