Class: Sentry::Client
- Inherits:
-
Object
- Object
- Sentry::Client
- Includes:
- 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.
-
#logger ⇒ Object
readonly
deprecated
Deprecated.
Use Sentry.logger to retrieve the current logger instead.
-
#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
-
#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_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.
-
#generate_baggage(span) ⇒ String?
deprecated
Deprecated.
Use Sentry.get_baggage instead.
-
#generate_sentry_trace(span) ⇒ String?
deprecated
Deprecated.
use Sentry.get_traceparent instead.
-
#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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sentry/client.rb', line 24 def initialize(configuration) @configuration = configuration @logger = configuration.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 end |
Instance Attribute Details
#configuration ⇒ Configuration (readonly)
The Configuration object that’s used for configuring the client and its transport.
18 19 20 |
# File 'lib/sentry/client.rb', line 18 def configuration @configuration end |
#logger ⇒ Object (readonly)
Use Sentry.logger to retrieve the current logger instead.
21 22 23 |
# File 'lib/sentry/client.rb', line 21 def logger @logger end |
#spotlight_transport ⇒ SpotlightTransport? (readonly)
The Transport object that’ll send events for the client.
15 16 17 |
# File 'lib/sentry/client.rb', line 15 def spotlight_transport @spotlight_transport end |
#transport ⇒ Transport (readonly)
The Transport object that’ll send events for the client.
11 12 13 |
# File 'lib/sentry/client.rb', line 11 def transport @transport end |
Instance Method Details
#capture_envelope(envelope) ⇒ void
This method returns an undefined value.
Capture an envelope directly.
94 95 96 |
# File 'lib/sentry/client.rb', line 94 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.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 |
# File 'lib/sentry/client.rb', line 48 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 event_type = event.is_a?(Event) ? event.type : event["type"] 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 async_block = configuration.async dispatch_async_event(async_block, event, hint) elsif 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 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.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/sentry/client.rb', line 149 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.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sentry/client.rb', line 109 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_message(message, hint = {}, backtrace: nil) ⇒ Event
Initializes an Event object with the given message.
129 130 131 132 133 134 135 136 137 |
# File 'lib/sentry/client.rb', line 129 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.
173 174 175 |
# File 'lib/sentry/client.rb', line 173 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.
100 101 102 103 |
# File 'lib/sentry/client.rb', line 100 def flush transport.flush if configuration.sending_to_dsn_allowed? spotlight_transport.flush if spotlight_transport end |
#generate_baggage(span) ⇒ String?
Use Sentry.get_baggage instead.
Generates a W3C Baggage header for distributed tracing from the given Span. Returns ‘nil` if `config.propagate_traces` is `false`.
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/sentry/client.rb', line 255 def generate_baggage(span) return unless configuration.propagate_traces baggage = span.to_baggage if baggage && !baggage.empty? log_debug("[Tracing] Adding #{BAGGAGE_HEADER_NAME} header to outgoing request: #{baggage}") end baggage end |
#generate_sentry_trace(span) ⇒ String?
use Sentry.get_traceparent instead.
Generates a Sentry trace for distribted tracing from the given Span. Returns ‘nil` if `config.propagate_traces` is `false`.
241 242 243 244 245 246 247 |
# File 'lib/sentry/client.rb', line 241 def generate_sentry_trace(span) return unless configuration.propagate_traces trace = span.to_sentry_trace log_debug("[Tracing] Adding #{SENTRY_TRACE_HEADER_NAME} header to outgoing request: #{trace}") trace end |
#send_envelope(envelope) ⇒ void
This method returns an undefined value.
Send an envelope directly to Sentry.
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/sentry/client.rb', line 222 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.map(&:data_category).each do |data_category| transport.record_lost_event(:network_error, data_category) end raise end |
#send_event(event, hint = nil) ⇒ Event
Sends the event to Sentry.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/sentry/client.rb', line 178 def send_event(event, hint = nil) event_type = event.is_a?(Event) ? event.type : event["type"] data_category = Envelope::Item.data_category(event_type) spans_before = event.is_a?(TransactionEvent) ? event.spans.size : 0 if event_type != TransactionEvent::TYPE && configuration.before_send event = configuration.before_send.call(event, hint) if event.nil? log_debug("Discarded event because before_send returned nil") transport.record_lost_event(:before_send, data_category) return end end if event_type == TransactionEvent::TYPE && configuration.before_send_transaction event = configuration.before_send_transaction.call(event, hint) if event.nil? log_debug("Discarded event because before_send_transaction returned nil") transport.record_lost_event(:before_send, "transaction") transport.record_lost_event(:before_send, "span", num: spans_before + 1) return else 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 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 |