Class: Sentry::Client
- Inherits:
-
Object
- Object
- Sentry::Client
- Includes:
- CallbackHelper, LoggingHelper
- Defined in:
- lib/sentry/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Configuration
readonly
The Configuration object that's used for configuring the client and its transport.
-
#spotlight_transport ⇒ SpotlightTransport?
readonly
The Transport object that'll send events for the client.
-
#transport ⇒ Transport
readonly
The Transport object that'll send events for the client.
Instance Method Summary collapse
-
#buffer_log_event(event, scope) ⇒ LogEvent
Buffer a log event to be sent later with other logs in a single envelope.
-
#buffer_metric_event(event, scope) ⇒ MetricEvent
Buffer a metric event to be sent later with other metrics in a single envelope.
-
#capture_envelope(envelope) ⇒ void
Capture an envelope directly.
-
#capture_event(event, scope, hint = {}) ⇒ Event?
Applies the given scope's data to the event and sends it to Sentry.
-
#event_from_check_in(slug, status, hint = {}, duration: nil, monitor_config: nil, check_in_id: nil) ⇒ Event
Initializes a CheckInEvent object with the given options.
-
#event_from_exception(exception, hint = {}) ⇒ Event?
Initializes an Event object with the given exception.
-
#event_from_log(message, level:, **options) ⇒ LogEvent
Initializes a LogEvent object with the given message and options.
-
#event_from_message(message, hint = {}, backtrace: nil) ⇒ Event
Initializes an Event object with the given message.
-
#event_from_transaction(transaction) ⇒ TransactionEvent
Initializes an Event object with the given Transaction object.
-
#flush ⇒ void
Flush pending events to Sentry.
-
#initialize(configuration) ⇒ Client
constructor
A new instance of Client.
-
#send_envelope(envelope) ⇒ void
Send an envelope directly to Sentry.
-
#send_event(event, hint = nil) ⇒ Event
Sends the event to Sentry.
Constructor Details
#initialize(configuration) ⇒ Client
Returns a new instance of Client.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sentry/client.rb', line 34 def initialize(configuration) @configuration = configuration @sdk_logger = configuration.sdk_logger if transport_class = configuration.transport.transport_class @transport = transport_class.new(configuration) else @transport = case configuration.dsn&.scheme when "http", "https" HTTPTransport.new(configuration) else DummyTransport.new(configuration) end end @spotlight_transport = SpotlightTransport.new(configuration) if configuration.spotlight @log_event_buffer = LogEventBuffer.new(configuration, self) @metric_event_buffer = MetricEventBuffer.new(configuration, self) end |
Instance Attribute Details
#configuration ⇒ Configuration (readonly)
The Configuration object that's used for configuring the client and its transport.
31 32 33 |
# File 'lib/sentry/client.rb', line 31 def configuration @configuration end |
#spotlight_transport ⇒ SpotlightTransport? (readonly)
The Transport object that'll send events for the client.
22 23 24 |
# File 'lib/sentry/client.rb', line 22 def spotlight_transport @spotlight_transport end |
#transport ⇒ Transport (readonly)
The Transport object that'll send events for the client.
18 19 20 |
# File 'lib/sentry/client.rb', line 18 def transport @transport end |
Instance Method Details
#buffer_log_event(event, scope) ⇒ LogEvent
Buffer a log event to be sent later with other logs in a single envelope
105 106 107 108 109 |
# File 'lib/sentry/client.rb', line 105 def buffer_log_event(event, scope) return unless event.is_a?(LogEvent) @log_event_buffer.add_item(scope.apply_to_telemetry(event)) event end |
#buffer_metric_event(event, scope) ⇒ MetricEvent
Buffer a metric event to be sent later with other metrics in a single envelope
114 115 116 117 118 119 |
# File 'lib/sentry/client.rb', line 114 def buffer_metric_event(event, scope) return unless event.is_a?(MetricEvent) event = scope.apply_to_telemetry(event) @metric_event_buffer.add_item(event) event end |
#capture_envelope(envelope) ⇒ void
This method returns an undefined value.
Capture an envelope directly.
124 125 126 |
# File 'lib/sentry/client.rb', line 124 def capture_envelope(envelope) Sentry.background_worker.perform { send_envelope(envelope) } end |
#capture_event(event, scope, hint = {}) ⇒ Event?
Applies the given scope's data to the event and sends it to Sentry.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sentry/client.rb', line 62 def capture_event(event, scope, hint = {}) return unless configuration.sending_allowed? if event.is_a?(ErrorEvent) && !configuration.sample_allowed? transport.record_lost_event(:sample_rate, "error") return end 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 configuration.background_worker_threads != 0 && hint.fetch(:background, true) 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 event = send_event(event, hint) end event rescue => e log_error("Event capturing failed", e, debug: configuration.debug) nil end |
#event_from_check_in(slug, status, hint = {}, duration: nil, monitor_config: nil, check_in_id: nil) ⇒ Event
Initializes a CheckInEvent object with the given options.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/sentry/client.rb', line 181 def event_from_check_in( slug, status, hint = {}, duration: nil, monitor_config: nil, check_in_id: nil ) return unless configuration.sending_allowed? CheckInEvent.new( configuration: configuration, integration_meta: Sentry.integrations[hint[:integration]], slug: slug, status: status, duration: duration, monitor_config: monitor_config, check_in_id: check_in_id ) end |
#event_from_exception(exception, hint = {}) ⇒ Event?
Initializes an Event object with the given exception. Returns nil if the exception's class is excluded from reporting.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sentry/client.rb', line 141 def event_from_exception(exception, hint = {}) return unless @configuration.sending_allowed? ignore_exclusions = hint.delete(:ignore_exclusions) { false } return if !ignore_exclusions && !@configuration.exception_class_allowed?(exception) = Sentry.integrations[hint[:integration]] mechanism = hint.delete(:mechanism) { Mechanism.new } ErrorEvent.new(configuration: configuration, integration_meta: ).tap do |event| event.add_exception_interface(exception, mechanism: mechanism) event.add_threads_interface(crashed: true) event.level = :error end end |
#event_from_log(message, level:, **options) ⇒ LogEvent
Initializes a LogEvent object with the given message and options
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/sentry/client.rb', line 210 def event_from_log(, level:, **) return unless configuration.sending_allowed? attributes = .reject { |k, _| k == :level || k == :severity || k == :origin } origin = [:origin] body = Utils::EncodingHelper.safe_utf_8_string() sanitized_attributes = attributes.transform_values do |value| if value.is_a?(String) Utils::EncodingHelper.safe_utf_8_string(value) else value end end LogEvent.new(level: level, body: body, attributes: sanitized_attributes, origin: origin) end |
#event_from_message(message, hint = {}, backtrace: nil) ⇒ Event
Initializes an Event object with the given message.
161 162 163 164 165 166 167 168 169 |
# File 'lib/sentry/client.rb', line 161 def (, hint = {}, backtrace: nil) return unless @configuration.sending_allowed? = Sentry.integrations[hint[:integration]] event = ErrorEvent.new(configuration: configuration, integration_meta: , message: ) event.add_threads_interface(backtrace: backtrace || caller) event.level = :error event end |
#event_from_transaction(transaction) ⇒ TransactionEvent
Initializes an Event object with the given Transaction object.
231 232 233 |
# File 'lib/sentry/client.rb', line 231 def event_from_transaction(transaction) TransactionEvent.new(configuration: configuration, transaction: transaction) end |
#flush ⇒ void
This method returns an undefined value.
Flush pending events to Sentry.
130 131 132 133 134 135 |
# File 'lib/sentry/client.rb', line 130 def flush transport.flush if configuration.sending_to_dsn_allowed? spotlight_transport.flush if spotlight_transport @log_event_buffer.flush @metric_event_buffer.flush end |
#send_envelope(envelope) ⇒ void
This method returns an undefined value.
Send an envelope directly to Sentry.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/sentry/client.rb', line 310 def send_envelope(envelope) transport.send_envelope(envelope) if configuration.sending_to_dsn_allowed? spotlight_transport.send_envelope(envelope) if spotlight_transport rescue => e log_error("Envelope sending failed", e, debug: configuration.debug) envelope.items.each do |item| transport.record_lost_event( :network_error, item.data_category, num: item.item_count, num_bytes: item.lost_event_byte_size ) end raise end |
#send_event(event, hint = nil) ⇒ Event
Sends the event to Sentry.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/sentry/client.rb', line 236 def send_event(event, hint = nil) data_category = Envelope::Item.data_category(event.type) spans_before = event.is_a?(TransactionEvent) ? event.spans.size : 0 if event.is_a?(ErrorEvent) && configuration.before_send event = safe_dispatch_callback( "before_send", configuration.before_send, [event, hint] ) if !event.is_a?(ErrorEvent) # Avoid serializing the event object in this case because we aren't sure what it is and what it contains log_debug(<<~MSG) Discarded event because before_send didn't return a Sentry::ErrorEvent object but an instance of #{event.class} MSG transport.record_lost_event(:before_send, data_category) return end end if event.is_a?(TransactionEvent) && configuration.before_send_transaction event = safe_dispatch_callback( "before_send_transaction", configuration.before_send_transaction, [event, hint] ) if !event.is_a?(TransactionEvent) # Avoid serializing the event object in this case because we aren't sure what it is and what it contains log_debug(<<~MSG) Discarded event because before_send_transaction didn't return a Sentry::TransactionEvent object but an instance of #{event.class} MSG transport.record_lost_event(:before_send, "transaction") transport.record_lost_event(:before_send, "span", num: spans_before + 1) return end 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 if event.is_a?(CheckInEvent) && configuration.before_send_check_in event = safe_dispatch_callback( "before_send_check_in", configuration.before_send_check_in, [event, hint] ) if !event.is_a?(CheckInEvent) # Avoid serializing the event object in this case because we aren't sure what it is and what it contains log_debug(<<~MSG) Discarded event because before_send_check_in didn't return a Sentry::CheckInEvent object but an instance of #{event.class} MSG transport.record_lost_event(:before_send, data_category) return end end transport.send_event(event) if configuration.sending_to_dsn_allowed? spotlight_transport.send_event(event) if spotlight_transport event 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 |