Class: Sentry::Hub
- Inherits:
-
Object
- Object
- Sentry::Hub
- Includes:
- ArgumentCheckingHelper
- Defined in:
- lib/sentry/hub.rb
Defined Under Namespace
Classes: Layer
Constant Summary collapse
- MUTEX =
Mutex.new
Instance Attribute Summary collapse
-
#current_profiler ⇒ Object
readonly
Returns the value of attribute current_profiler.
-
#last_event_id ⇒ Object
readonly
Returns the value of attribute last_event_id.
Instance Method Summary collapse
- #add_breadcrumb(breadcrumb, hint: {}) ⇒ Object
- #bind_client(client) ⇒ Object
- #capture_check_in(slug, status, **options) ⇒ Object
- #capture_event(event, **options, &block) ⇒ Object
- #capture_exception(exception, **options, &block) ⇒ Object
- #capture_log_event(message, **options) ⇒ Object
- #capture_message(message, **options, &block) ⇒ Object
-
#capture_metric(name:, type:, value:, unit: nil, attributes: nil) ⇒ void
Captures a metric and sends it to Sentry.
-
#clients ⇒ Array<Client>
All clients bound across the hub’s scope stack, base layer first.
- #clone ⇒ Object
- #configuration ⇒ Object
- #configure_scope(&block) ⇒ Object
- #continue_trace(env, **options) ⇒ Object
- #current_client ⇒ Object
- #current_scope ⇒ Object
- #end_session ⇒ Object
- #get_baggage ⇒ Object
- #get_trace_propagation_headers ⇒ Object
- #get_trace_propagation_meta ⇒ Object
- #get_traceparent ⇒ Object
-
#initialize(client, scope) ⇒ Hub
constructor
A new instance of Hub.
- #new_from_top ⇒ Object
- #pop_scope ⇒ Object
-
#profiler_running? ⇒ Boolean
private
This is an internal private method.
- #push_scope ⇒ Object
-
#start_profiler!(transaction) ⇒ Object
private
This is an internal private method.
- #start_session ⇒ Object
- #start_transaction(transaction: nil, custom_sampling_context: {}, instrumenter: :sentry, **options) ⇒ Object
-
#stop_profiler!(transaction) ⇒ Object
private
This is an internal private method.
-
#with_background_worker_disabled(&block) ⇒ Object
this doesn’t do anything to the already initialized background worker but it temporarily disables dispatching events to it.
- #with_child_span(instrumenter: :sentry, **attributes, &block) ⇒ Object
- #with_scope(&block) ⇒ Object
- #with_session_tracking(&block) ⇒ Object
Constructor Details
Instance Attribute Details
#current_profiler ⇒ Object (readonly)
Returns the value of attribute current_profiler.
15 16 17 |
# File 'lib/sentry/hub.rb', line 15 def current_profiler @current_profiler end |
#last_event_id ⇒ Object (readonly)
Returns the value of attribute last_event_id.
13 14 15 |
# File 'lib/sentry/hub.rb', line 13 def last_event_id @last_event_id end |
Instance Method Details
#add_breadcrumb(breadcrumb, hint: {}) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/sentry/hub.rb', line 292 def (, hint: {}) return unless current_client return unless configuration.enabled_in_current_env? if = current_client.configuration. = .call(, hint) end return unless current_scope.() end |
#bind_client(client) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/sentry/hub.rb', line 81 def bind_client(client) layer = current_layer if layer layer.client = client end end |
#capture_check_in(slug, status, **options) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/sentry/hub.rb', line 202 def capture_check_in(slug, status, **) check_argument_type!(slug, ::String) check_argument_includes!(status, Sentry::CheckInEvent::VALID_STATUSES) return unless current_client [:hint] ||= {} [:hint][:slug] = slug event = current_client.event_from_check_in( slug, status, [:hint], duration: .delete(:duration), monitor_config: .delete(:monitor_config), check_in_id: .delete(:check_in_id) ) return unless event capture_event(event, **) event.check_in_id end |
#capture_event(event, **options, &block) ⇒ Object
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 |
# File 'lib/sentry/hub.rb', line 259 def capture_event(event, **, &block) check_argument_type!(event, Sentry::Event) return unless current_client hint = .delete(:hint) || {} scope = current_scope.dup if block block.call(scope) elsif custom_scope = [:scope] scope.update_from_scope(custom_scope) elsif !.empty? unsupported_option_keys = scope.(**) unless unsupported_option_keys.empty? configuration.log_debug <<~MSG Options #{unsupported_option_keys} are not supported and will not be applied to the event. You may want to set them under the `extra` option. MSG end end event = current_client.capture_event(event, scope, hint) if event && configuration.debug configuration.log_debug(event.to_json_compatible) end @last_event_id = event&.event_id if event.is_a?(Sentry::ErrorEvent) event end |
#capture_exception(exception, **options, &block) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/sentry/hub.rb', line 161 def capture_exception(exception, **, &block) if RUBY_PLATFORM == "java" check_argument_type!(exception, ::Exception, ::Java::JavaLang::Throwable) else check_argument_type!(exception, ::Exception) end return if Sentry.exception_captured?(exception) return unless current_client [:hint] ||= {} [:hint][:exception] = exception event = current_client.event_from_exception(exception, [:hint]) return unless event current_scope.session&.update_from_exception(event.exception) capture_event(event, **, &block).tap do # mark the exception as captured so we can use this information to avoid duplicated capturing exception.instance_variable_set(Sentry::CAPTURED_SIGNATURE, true) end end |
#capture_log_event(message, **options) ⇒ Object
226 227 228 229 230 231 232 233 234 |
# File 'lib/sentry/hub.rb', line 226 def capture_log_event(, **) return unless current_client && current_client.configuration.enable_logs event = current_client.event_from_log(, **) return unless event current_client.buffer_log_event(event, current_scope) end |
#capture_message(message, **options, &block) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/sentry/hub.rb', line 187 def (, **, &block) check_argument_type!(, ::String) return unless current_client [:hint] ||= {} [:hint][:message] = backtrace = .delete(:backtrace) event = current_client.(, [:hint], backtrace: backtrace) return unless event capture_event(event, **, &block) end |
#capture_metric(name:, type:, value:, unit: nil, attributes: nil) ⇒ void
This method returns an undefined value.
Captures a metric and sends it to Sentry
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/sentry/hub.rb', line 244 def capture_metric(name:, type:, value:, unit: nil, attributes: nil) return unless current_client&.configuration.enable_metrics metric = MetricEvent.new( name: name, value: value, type: type, unit: unit, attributes: attributes, ) current_client.buffer_metric_event(metric, current_scope) end |
#clients ⇒ Array<Client>
All clients bound across the hub’s scope stack, base layer first.
59 60 61 |
# File 'lib/sentry/hub.rb', line 59 def clients @stack.map(&:client).compact end |
#clone ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/sentry/hub.rb', line 71 def clone layer = current_layer if layer scope = layer.scope&.dup Hub.new(layer.client, scope) end end |
#configuration ⇒ Object
63 64 65 |
# File 'lib/sentry/hub.rb', line 63 def configuration current_client.configuration end |
#configure_scope(&block) ⇒ Object
89 90 91 |
# File 'lib/sentry/hub.rb', line 89 def configure_scope(&block) block.call(current_scope) end |
#continue_trace(env, **options) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/sentry/hub.rb', line 376 def continue_trace(env, **) configure_scope { |s| s.generate_propagation_context(env) } return nil unless configuration.tracing_enabled? propagation_context = current_scope.propagation_context return nil unless propagation_context.incoming_trace Transaction.new( trace_id: propagation_context.trace_id, parent_span_id: propagation_context.parent_span_id, parent_sampled: propagation_context.parent_sampled, baggage: propagation_context.baggage, sample_rand: propagation_context.sample_rand, ** ) end |
#current_client ⇒ Object
53 54 55 |
# File 'lib/sentry/hub.rb', line 53 def current_client current_layer&.client end |
#current_scope ⇒ Object
67 68 69 |
# File 'lib/sentry/hub.rb', line 67 def current_scope current_layer&.scope end |
#end_session ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/sentry/hub.rb', line 321 def end_session return unless current_scope session = current_scope.session current_scope.set_session(nil) return unless session session.close # NOTE: Under some circumstances, session_flusher nilified out of sync # See: https://github.com/getsentry/sentry-ruby/issues/2378 # See: https://github.com/getsentry/sentry-ruby/pull/2396 Sentry.session_flusher&.add_session(session) end |
#get_baggage ⇒ Object
351 352 353 354 355 356 |
# File 'lib/sentry/hub.rb', line 351 def get_baggage return nil unless current_scope current_scope.get_span&.to_baggage || current_scope.propagation_context.get_baggage&.serialize end |
#get_trace_propagation_headers ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/sentry/hub.rb', line 358 def get_trace_propagation_headers headers = {} traceparent = get_traceparent headers[SENTRY_TRACE_HEADER_NAME] = traceparent if traceparent baggage = get_baggage headers[BAGGAGE_HEADER_NAME] = baggage if baggage && !baggage.empty? headers end |
#get_trace_propagation_meta ⇒ Object
370 371 372 373 374 |
# File 'lib/sentry/hub.rb', line 370 def get_trace_propagation_headers.map do |k, v| "<meta name=\"#{k}\" content=\"#{v}\">" end.join("\n") end |
#get_traceparent ⇒ Object
344 345 346 347 348 349 |
# File 'lib/sentry/hub.rb', line 344 def get_traceparent return nil unless current_scope current_scope.get_span&.to_sentry_trace || current_scope.propagation_context.get_traceparent end |
#new_from_top ⇒ Object
49 50 51 |
# File 'lib/sentry/hub.rb', line 49 def new_from_top Hub.new(current_client, current_scope) end |
#pop_scope ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/sentry/hub.rb', line 111 def pop_scope if @stack.size > 1 @stack.pop else # We never want to enter a situation where we have no scope and no client client = current_client @stack = [Layer.new(client, Scope.new)] end end |
#profiler_running? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is an internal private method
43 44 45 46 47 |
# File 'lib/sentry/hub.rb', line 43 def profiler_running? MUTEX.synchronize do !@current_profiler.empty? end end |
#push_scope ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sentry/hub.rb', line 100 def push_scope new_scope = if current_scope current_scope.dup else Scope.new end @stack << Layer.new(current_client, new_scope) end |
#start_profiler!(transaction) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is an internal private method
26 27 28 29 30 31 |
# File 'lib/sentry/hub.rb', line 26 def start_profiler!(transaction) MUTEX.synchronize do transaction.start_profiler! @current_profiler[transaction.__id__] = transaction.profiler end end |
#start_session ⇒ Object
316 317 318 319 |
# File 'lib/sentry/hub.rb', line 316 def start_session return unless current_scope current_scope.set_session(Session.new) end |
#start_transaction(transaction: nil, custom_sampling_context: {}, instrumenter: :sentry, **options) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/sentry/hub.rb', line 121 def start_transaction(transaction: nil, custom_sampling_context: {}, instrumenter: :sentry, **) return unless configuration.tracing_enabled? return unless instrumenter == configuration.instrumenter transaction ||= Transaction.new(**) sampling_context = { transaction_context: transaction.to_h, parent_sampled: transaction.parent_sampled, parent_sample_rate: transaction.parent_sample_rate } sampling_context.merge!(custom_sampling_context) transaction.set_initial_sample_decision(sampling_context: sampling_context) start_profiler!(transaction) transaction end |
#stop_profiler!(transaction) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is an internal private method
35 36 37 38 39 |
# File 'lib/sentry/hub.rb', line 35 def stop_profiler!(transaction) MUTEX.synchronize do @current_profiler.delete(transaction.__id__)&.stop end end |
#with_background_worker_disabled(&block) ⇒ Object
this doesn’t do anything to the already initialized background worker but it temporarily disables dispatching events to it
307 308 309 310 311 312 313 314 |
# File 'lib/sentry/hub.rb', line 307 def with_background_worker_disabled(&block) original_background_worker_threads = configuration.background_worker_threads configuration.background_worker_threads = 0 block.call ensure configuration.background_worker_threads = original_background_worker_threads end |
#with_child_span(instrumenter: :sentry, **attributes, &block) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/sentry/hub.rb', line 141 def with_child_span(instrumenter: :sentry, **attributes, &block) return yield(nil) unless instrumenter == configuration.instrumenter current_span = current_scope.get_span return yield(nil) unless current_span result = nil begin current_span.with_child_span(**attributes) do |child_span| current_scope.set_span(child_span) result = yield(child_span) end ensure current_scope.set_span(current_span) end result end |
#with_scope(&block) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/sentry/hub.rb', line 93 def with_scope(&block) push_scope yield(current_scope) ensure pop_scope end |
#with_session_tracking(&block) ⇒ Object
335 336 337 338 339 340 341 342 |
# File 'lib/sentry/hub.rb', line 335 def with_session_tracking(&block) return yield unless configuration.session_tracking? start_session yield ensure end_session end |