Class: Sentry::Scope
- Inherits:
-
Object
- Object
- Sentry::Scope
- Includes:
- ArgumentCheckingHelper, CallbackHelper
- Defined in:
- lib/sentry/scope.rb
Constant Summary collapse
- ATTRIBUTES =
[ :transaction_name, :transaction_source, :contexts, :extra, :tags, :user, :level, :breadcrumbs, :fingerprint, :event_processors, :rack_env, :span, :session, :attachments, :propagation_context, :attributes, :flags ]
- MAX_FLAGS =
Maximum number of feature flag evaluations kept on a scope.
100
Class Method Summary collapse
-
.add_global_event_processor(&block) ⇒ void
Adds a new global event processor [Proc].
-
.global_event_processors ⇒ Array<Proc>
Returns the global event processors array.
- .os_context ⇒ Hash
- .runtime_context ⇒ Hash
Instance Method Summary collapse
-
#add_attachment(**opts) ⇒ Object
Add a new attachment to the scope.
-
#add_breadcrumb(breadcrumb) ⇒ void
Adds the breadcrumb to the scope's breadcrumbs buffer.
-
#add_event_processor(&block) ⇒ void
Adds a new event processor [Proc] to the scope.
-
#add_feature_flag(name, result) ⇒ void
Records a feature flag evaluation on the scope (and the active span, if any).
-
#apply_to_event(event, hint = nil) ⇒ Event
Applies stored attributes and event processors to the given event.
-
#apply_to_telemetry(telemetry) ⇒ MetricEvent, LogEvent
A leaner version of apply_to_event that applies to lightweight payloads like Logs and Metrics.
-
#clear ⇒ void
Resets the scope's attributes to defaults.
-
#clear_breadcrumbs ⇒ void
Clears the scope's breadcrumbs buffer.
- #dup ⇒ Scope
-
#generate_propagation_context(env = nil) ⇒ void
Generate a new propagation context either from the incoming env headers or from scratch.
-
#get_span ⇒ Span?
Returns the associated Span object.
-
#get_trace_context ⇒ Hash
Returns the trace context for this scope.
-
#get_transaction ⇒ Transaction?
Returns the associated Transaction object.
-
#initialize(max_breadcrumbs: nil) ⇒ Scope
constructor
A new instance of Scope.
-
#remove_attribute(key) ⇒ void
Removes a single attribute from the scope.
-
#set_attribute(key, value, unit: nil) ⇒ Hash
Sets a single attribute on the scope.
-
#set_attributes(attributes_hash) ⇒ Hash
Updates the scope's attributes by merging with the old value.
-
#set_context(key, value) ⇒ Hash
Adds a new key-value pair to current contexts.
-
#set_contexts(contexts_hash) ⇒ Hash
Updates the scope's contexts attribute by merging with the old value.
-
#set_extra(key, value) ⇒ Hash
Adds a new key-value pair to current extras.
-
#set_extras(extras_hash) ⇒ Hash
Updates the scope's extras attribute by merging with the old value.
-
#set_fingerprint(fingerprint) ⇒ Array
Sets the scope's fingerprint attribute.
-
#set_level(level) ⇒ void
Sets the scope's level attribute.
-
#set_rack_env(env) ⇒ Hash
Sets the scope's rack_env attribute.
-
#set_session(session) ⇒ void
Sets the currently active session on the scope.
-
#set_span(span) ⇒ Span
Sets the scope's span attribute.
-
#set_tag(key, value) ⇒ Hash
Adds a new key-value pair to current tags.
-
#set_tags(tags_hash) ⇒ Hash
Updates the scope's tags attribute by merging with the old value.
-
#set_transaction_name(transaction_name, source: :custom) ⇒ void
Appends a new transaction name to the scope.
-
#set_user(user_hash) ⇒ Hash
Sets the scope's user attribute.
-
#transaction_source_low_quality? ⇒ Boolean
These are high cardinality and thus bad.
-
#update_from_options(contexts: nil, extra: nil, tags: nil, user: nil, level: nil, fingerprint: nil, attachments: nil, **options) ⇒ Array
Updates the scope's data from the given options.
-
#update_from_scope(scope) ⇒ void
Updates the scope's data from a given scope.
Constructor Details
#initialize(max_breadcrumbs: nil) ⇒ Scope
Returns a new instance of Scope.
41 42 43 44 |
# File 'lib/sentry/scope.rb', line 41 def initialize(max_breadcrumbs: nil) @max_breadcrumbs = set_default_value end |
Class Method Details
.add_global_event_processor(&block) ⇒ void
This method returns an undefined value.
Adds a new global event processor [Proc]. Sometimes we need a global event processor without needing to configure scope. These run before scope event processors.
489 490 491 |
# File 'lib/sentry/scope.rb', line 489 def add_global_event_processor(&block) global_event_processors << block end |
.global_event_processors ⇒ Array<Proc>
Returns the global event processors array.
479 480 481 |
# File 'lib/sentry/scope.rb', line 479 def global_event_processors @global_event_processors ||= [] end |
.os_context ⇒ Hash
455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/sentry/scope.rb', line 455 def os_context @os_context ||= begin uname = Etc.uname { name: uname[:sysname] || RbConfig::CONFIG["host_os"], version: uname[:version], build: uname[:release], kernel_version: uname[:version], machine: uname[:machine] } end end |
.runtime_context ⇒ Hash
470 471 472 473 474 475 |
# File 'lib/sentry/scope.rb', line 470 def runtime_context @runtime_context ||= { name: RbConfig::CONFIG["ruby_install_name"], version: RUBY_DESCRIPTION || Sentry.sys_command("ruby -v") } end |
Instance Method Details
#add_attachment(**opts) ⇒ Object
Add a new attachment to the scope.
404 405 406 407 |
# File 'lib/sentry/scope.rb', line 404 def (**opts) << ( = Attachment.new(**opts)) end |
#add_breadcrumb(breadcrumb) ⇒ void
This method returns an undefined value.
Adds the breadcrumb to the scope's breadcrumbs buffer.
133 134 135 |
# File 'lib/sentry/scope.rb', line 133 def () .record() end |
#add_event_processor(&block) ⇒ void
This method returns an undefined value.
Adds a new event processor [Proc] to the scope.
392 393 394 |
# File 'lib/sentry/scope.rb', line 392 def add_event_processor(&block) @event_processors << block end |
#add_feature_flag(name, result) ⇒ void
This method returns an undefined value.
Records a feature flag evaluation on the scope (and the active span, if any). Only boolean results are supported; other values are ignored. The scope keeps the most recent MAX_FLAGS distinct flags (oldest evicted first).
315 316 317 318 319 320 321 322 323 |
# File 'lib/sentry/scope.rb', line 315 def add_feature_flag(name, result) return unless result == true || result == false name = name.to_s flags.set(name, result) add_feature_flag_to_span(name, result) nil end |
#apply_to_event(event, hint = nil) ⇒ Event
Applies stored attributes and event processors to the given event.
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 |
# File 'lib/sentry/scope.rb', line 56 def apply_to_event(event, hint = nil) unless event.is_a?(CheckInEvent) event. = .merge(event.) event.user = user.merge(event.user) event.extra = extra.merge(event.extra) event.contexts = contexts.merge(event.contexts) event.transaction = transaction_name if transaction_name event.transaction_info = { source: transaction_source } if transaction_source event.fingerprint = fingerprint event.level = level event. = event.rack_env = rack_env if rack_env event. = apply_flags_to_event(event) end trace_context = get_trace_context dynamic_sampling_context = trace_context.delete(:dynamic_sampling_context) event.contexts[:trace] ||= trace_context event.dynamic_sampling_context ||= dynamic_sampling_context all_event_processors = self.class.global_event_processors + @event_processors unless all_event_processors.empty? all_event_processors.each do |processor_block| event = safe_dispatch_callback("event_processor", processor_block, [event, hint]) return unless event end end event end |
#apply_to_telemetry(telemetry) ⇒ MetricEvent, LogEvent
A leaner version of apply_to_event that applies to lightweight payloads like Logs and Metrics.
Adds trace_id, span_id, user from the scope and default attributes from configuration.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sentry/scope.rb', line 96 def apply_to_telemetry(telemetry) # Compare as strings since String and Symbol keys serialize to the same wire key. existing_keys = telemetry.attributes.keys.map(&:to_s).to_set attributes.each do |key, value| next if existing_keys.include?(key) telemetry.attributes[key] = value end trace_context = get_trace_context telemetry.trace_id = trace_context[:trace_id] telemetry.span_id = trace_context[:span_id] configuration = Sentry.configuration return telemetry unless configuration = telemetry.respond_to?(:integration_meta) ? telemetry. : nil ||= Sentry. telemetry.attributes["sentry.sdk.name"] ||= [:name] || ["name"] telemetry.attributes["sentry.sdk.version"] ||= [:version] || ["version"] telemetry.attributes["sentry.environment"] ||= configuration.environment if configuration.environment telemetry.attributes["sentry.release"] ||= configuration.release if configuration.release telemetry.attributes["server.address"] ||= configuration.server_name if configuration.server_name unless user.empty? telemetry.attributes["user.id"] ||= user[:id] if user[:id] telemetry.attributes["user.name"] ||= user[:username] if user[:username] telemetry.attributes["user.email"] ||= user[:email] if user[:email] end telemetry end |
#clear ⇒ void
This method returns an undefined value.
Resets the scope's attributes to defaults.
48 49 50 |
# File 'lib/sentry/scope.rb', line 48 def clear set_default_value end |
#clear_breadcrumbs ⇒ void
This method returns an undefined value.
Clears the scope's breadcrumbs buffer
139 140 141 |
# File 'lib/sentry/scope.rb', line 139 def end |
#dup ⇒ Scope
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/sentry/scope.rb', line 144 def dup copy = super copy. = .dup copy.contexts = contexts.deep_dup copy.extra = extra.deep_dup copy. = .deep_dup copy.user = user.deep_dup copy.transaction_name = transaction_name.dup copy.transaction_source = transaction_source.dup copy.fingerprint = fingerprint.deep_dup copy.span = span.deep_dup copy.session = session.deep_dup copy.propagation_context = propagation_context.deep_dup copy. = .dup copy.event_processors = event_processors.dup copy.attributes = attributes.deep_dup copy.flags = flags.dup copy end |
#generate_propagation_context(env = nil) ⇒ void
This method returns an undefined value.
Generate a new propagation context either from the incoming env headers or from scratch.
399 400 401 |
# File 'lib/sentry/scope.rb', line 399 def generate_propagation_context(env = nil) @propagation_context = PropagationContext.new(self, env) end |
#get_span ⇒ Span?
Returns the associated Span object.
362 363 364 |
# File 'lib/sentry/scope.rb', line 362 def get_span span end |
#get_trace_context ⇒ Hash
Returns the trace context for this scope. Prioritizes external propagation context (from OTel) over local propagation context.
369 370 371 372 373 374 375 376 377 378 |
# File 'lib/sentry/scope.rb', line 369 def get_trace_context if span span.get_trace_context.merge(dynamic_sampling_context: span.get_dynamic_sampling_context) elsif (external_context = Sentry.get_external_propagation_context) trace_id, span_id = external_context { trace_id: trace_id, span_id: span_id } else propagation_context.get_trace_context.merge(dynamic_sampling_context: propagation_context.get_dynamic_sampling_context) end end |
#get_transaction ⇒ Transaction?
Returns the associated Transaction object.
356 357 358 |
# File 'lib/sentry/scope.rb', line 356 def get_transaction span.transaction if span end |
#remove_attribute(key) ⇒ void
This method returns an undefined value.
Removes a single attribute from the scope. No-op if the attribute is not set.
305 306 307 |
# File 'lib/sentry/scope.rb', line 305 def remove_attribute(key) @attributes.delete(key.to_s) end |
#set_attribute(key, value, unit: nil) ⇒ Hash
Sets a single attribute on the scope.
297 298 299 300 |
# File 'lib/sentry/scope.rb', line 297 def set_attribute(key, value, unit: nil) value = { value: value, unit: unit } unless unit.nil? set_attributes(key => value) end |
#set_attributes(attributes_hash) ⇒ Hash
Updates the scope's attributes by merging with the old value.
286 287 288 289 290 |
# File 'lib/sentry/scope.rb', line 286 def set_attributes(attributes_hash) check_argument_type!(attributes_hash, Hash) attributes_hash.each { |key, value| @attributes[key.to_s] = value } @attributes end |
#set_context(key, value) ⇒ Hash
Adds a new key-value pair to current contexts.
278 279 280 281 |
# File 'lib/sentry/scope.rb', line 278 def set_context(key, value) check_argument_type!(value, Hash) set_contexts(key => value) end |
#set_contexts(contexts_hash) ⇒ Hash
Updates the scope's contexts attribute by merging with the old value.
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/sentry/scope.rb', line 266 def set_contexts(contexts_hash) check_argument_type!(contexts_hash, Hash) contexts_hash.values.each do |val| check_argument_type!(val, Hash) end @contexts.merge!(contexts_hash) do |key, old, new| old.merge(new) end end |
#set_extra(key, value) ⇒ Hash
Adds a new key-value pair to current extras.
245 246 247 |
# File 'lib/sentry/scope.rb', line 245 def set_extra(key, value) set_extras(key => value) end |
#set_extras(extras_hash) ⇒ Hash
Updates the scope's extras attribute by merging with the old value.
236 237 238 239 |
# File 'lib/sentry/scope.rb', line 236 def set_extras(extras_hash) check_argument_type!(extras_hash, Hash) @extra.merge!(extras_hash) end |
#set_fingerprint(fingerprint) ⇒ Array
Sets the scope's fingerprint attribute.
383 384 385 386 387 |
# File 'lib/sentry/scope.rb', line 383 def set_fingerprint(fingerprint) check_argument_type!(fingerprint, Array) @fingerprint = fingerprint end |
#set_level(level) ⇒ void
This method returns an undefined value.
Sets the scope's level attribute.
328 329 330 |
# File 'lib/sentry/scope.rb', line 328 def set_level(level) @level = level end |
#set_rack_env(env) ⇒ Hash
Sets the scope's rack_env attribute.
216 217 218 219 |
# File 'lib/sentry/scope.rb', line 216 def set_rack_env(env) env = env || {} @rack_env = env end |
#set_session(session) ⇒ void
This method returns an undefined value.
Sets the currently active session on the scope.
344 345 346 |
# File 'lib/sentry/scope.rb', line 344 def set_session(session) @session = session end |
#set_span(span) ⇒ Span
Sets the scope's span attribute.
224 225 226 227 |
# File 'lib/sentry/scope.rb', line 224 def set_span(span) check_argument_type!(span, Span) @span = span end |
#set_tag(key, value) ⇒ Hash
Adds a new key-value pair to current tags.
259 260 261 |
# File 'lib/sentry/scope.rb', line 259 def set_tag(key, value) (key => value) end |
#set_tags(tags_hash) ⇒ Hash
Updates the scope's tags attribute by merging with the old value.
250 251 252 253 |
# File 'lib/sentry/scope.rb', line 250 def () check_argument_type!(, Hash) @tags.merge!() end |
#set_transaction_name(transaction_name, source: :custom) ⇒ void
This method returns an undefined value.
Appends a new transaction name to the scope.
The "transaction" here does not refer to Transaction objects.
336 337 338 339 |
# File 'lib/sentry/scope.rb', line 336 def set_transaction_name(transaction_name, source: :custom) @transaction_name = transaction_name @transaction_source = source end |
#set_user(user_hash) ⇒ Hash
Sets the scope's user attribute.
230 231 232 233 |
# File 'lib/sentry/scope.rb', line 230 def set_user(user_hash) check_argument_type!(user_hash, Hash) @user = user_hash end |
#transaction_source_low_quality? ⇒ Boolean
These are high cardinality and thus bad.
350 351 352 |
# File 'lib/sentry/scope.rb', line 350 def transaction_source_low_quality? transaction_source == :url end |
#update_from_options(contexts: nil, extra: nil, tags: nil, user: nil, level: nil, fingerprint: nil, attachments: nil, **options) ⇒ Array
Updates the scope's data from the given options.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sentry/scope.rb', line 192 def ( contexts: nil, extra: nil, tags: nil, user: nil, level: nil, fingerprint: nil, attachments: nil, ** ) self.contexts.merge!(contexts) if contexts self.extra.merge!(extra) if extra self..merge!() if self.user = user if user self.level = level if level self.fingerprint = fingerprint if fingerprint # Returns unsupported option keys so we can notify users. .keys end |
#update_from_scope(scope) ⇒ void
This method returns an undefined value.
Updates the scope's data from a given scope.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/sentry/scope.rb', line 167 def update_from_scope(scope) self. = scope. self.contexts = scope.contexts self.extra = scope.extra self. = scope. self.user = scope.user self.transaction_name = scope.transaction_name self.transaction_source = scope.transaction_source self.fingerprint = scope.fingerprint self.span = scope.span self.propagation_context = scope.propagation_context self. = scope. self.attributes = scope.attributes self.flags = scope.flags.dup end |