Class: Sentry::Scope

Inherits:
Object
  • Object
show all
Includes:
ArgumentCheckingHelper
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
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_breadcrumbs: nil) ⇒ Scope

Returns a new instance of Scope.

Parameters:

  • max_breadcrumbs (Integer) (defaults to: nil)

    the maximum number of breadcrumbs to be stored in the scope.



33
34
35
36
# File 'lib/sentry/scope.rb', line 33

def initialize(max_breadcrumbs: nil)
  @max_breadcrumbs = 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.

Parameters:

  • block (Proc)


407
408
409
# File 'lib/sentry/scope.rb', line 407

def add_global_event_processor(&block)
  global_event_processors << block
end

.global_event_processorsArray<Proc>

Returns the global event processors array.

Returns:

  • (Array<Proc>)


397
398
399
# File 'lib/sentry/scope.rb', line 397

def global_event_processors
  @global_event_processors ||= []
end

.os_contextHash

Returns:

  • (Hash)


373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/sentry/scope.rb', line 373

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_contextHash

Returns:

  • (Hash)


388
389
390
391
392
393
# File 'lib/sentry/scope.rb', line 388

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.



337
338
339
340
# File 'lib/sentry/scope.rb', line 337

def add_attachment(**opts)
  attachments << (attachment = Attachment.new(**opts))
  attachment
end

#add_breadcrumb(breadcrumb) ⇒ void

This method returns an undefined value.

Adds the breadcrumb to the scope’s breadcrumbs buffer.

Parameters:



113
114
115
# File 'lib/sentry/scope.rb', line 113

def add_breadcrumb(breadcrumb)
  breadcrumbs.record(breadcrumb)
end

#add_event_processor(&block) ⇒ void

This method returns an undefined value.

Adds a new event processor [Proc] to the scope.

Parameters:

  • block (Proc)


325
326
327
# File 'lib/sentry/scope.rb', line 325

def add_event_processor(&block)
  @event_processors << block
end

#apply_to_event(event, hint = nil) ⇒ Event

Applies stored attributes and event processors to the given event.

Parameters:

  • event (Event)
  • hint (Hash) (defaults to: nil)

    the hint data that’ll be passed to event processors.

Returns:



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
# File 'lib/sentry/scope.rb', line 48

def apply_to_event(event, hint = nil)
  unless event.is_a?(CheckInEvent)
    event.tags = tags.merge(event.tags)
    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.breadcrumbs = breadcrumbs
    event.rack_env = rack_env if rack_env
    event.attachments = attachments
  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 = processor_block.call(event, hint)
    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.

Parameters:

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sentry/scope.rb', line 86

def apply_to_telemetry(telemetry)
  # TODO-neel when new scope set_attribute api is added: add them here
  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.attributes["sentry.sdk.name"] ||= Sentry.sdk_meta["name"]
  telemetry.attributes["sentry.sdk.version"] ||= Sentry.sdk_meta["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

  if configuration.send_default_pii && !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

#clearvoid

This method returns an undefined value.

Resets the scope’s attributes to defaults.



40
41
42
# File 'lib/sentry/scope.rb', line 40

def clear
  set_default_value
end

#clear_breadcrumbsvoid

This method returns an undefined value.

Clears the scope’s breadcrumbs buffer



119
120
121
# File 'lib/sentry/scope.rb', line 119

def clear_breadcrumbs
  set_new_breadcrumb_buffer
end

#dupScope

Returns:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sentry/scope.rb', line 124

def dup
  copy = super
  copy.breadcrumbs = breadcrumbs.dup
  copy.contexts = contexts.deep_dup
  copy.extra = extra.deep_dup
  copy.tags = tags.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.attachments = attachments.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.

Parameters:

  • env (Hash, nil) (defaults to: nil)


332
333
334
# File 'lib/sentry/scope.rb', line 332

def generate_propagation_context(env = nil)
  @propagation_context = PropagationContext.new(self, env)
end

#get_spanSpan?

Returns the associated Span object.

Returns:



295
296
297
# File 'lib/sentry/scope.rb', line 295

def get_span
  span
end

#get_trace_contextHash

Returns the trace context for this scope. Prioritizes external propagation context (from OTel) over local propagation context.

Returns:

  • (Hash)


302
303
304
305
306
307
308
309
310
311
# File 'lib/sentry/scope.rb', line 302

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_transactionTransaction?

Returns the associated Transaction object.

Returns:



289
290
291
# File 'lib/sentry/scope.rb', line 289

def get_transaction
  span.transaction if span
end

#set_context(key, value) ⇒ Hash

Adds a new key-value pair to current contexts.

Parameters:

  • key (String, Symbol)
  • value (Object)

Returns:

  • (Hash)


253
254
255
256
# File 'lib/sentry/scope.rb', line 253

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.

Parameters:

  • contexts (Hash)

Returns:

  • (Hash)


241
242
243
244
245
246
247
248
249
250
# File 'lib/sentry/scope.rb', line 241

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.

Parameters:

  • key (String, Symbol)
  • value (Object)

Returns:

  • (Hash)


220
221
222
# File 'lib/sentry/scope.rb', line 220

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.

Parameters:

  • extras (Hash)

Returns:

  • (Hash)


211
212
213
214
# File 'lib/sentry/scope.rb', line 211

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.

Parameters:

  • fingerprint (Array)

Returns:

  • (Array)


316
317
318
319
320
# File 'lib/sentry/scope.rb', line 316

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.

Parameters:

  • level (String, Symbol)


261
262
263
# File 'lib/sentry/scope.rb', line 261

def set_level(level)
  @level = level
end

#set_rack_env(env) ⇒ Hash

Sets the scope’s rack_env attribute.

Parameters:

  • env (Hash)

Returns:

  • (Hash)


191
192
193
194
# File 'lib/sentry/scope.rb', line 191

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.

Parameters:



277
278
279
# File 'lib/sentry/scope.rb', line 277

def set_session(session)
  @session = session
end

#set_span(span) ⇒ Span

Sets the scope’s span attribute.

Parameters:

Returns:



199
200
201
202
# File 'lib/sentry/scope.rb', line 199

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.

Parameters:

  • key (String, Symbol)
  • value (Object)

Returns:

  • (Hash)


234
235
236
# File 'lib/sentry/scope.rb', line 234

def set_tag(key, value)
  set_tags(key => value)
end

#set_tags(tags_hash) ⇒ Hash

Updates the scope’s tags attribute by merging with the old value.

Parameters:

  • tags (Hash)

Returns:

  • (Hash)


225
226
227
228
# File 'lib/sentry/scope.rb', line 225

def set_tags(tags_hash)
  check_argument_type!(tags_hash, Hash)
  @tags.merge!(tags_hash)
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.

Parameters:

  • transaction_name (String)


269
270
271
272
# File 'lib/sentry/scope.rb', line 269

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.

Parameters:

  • user (Hash)

Returns:

  • (Hash)


205
206
207
208
# File 'lib/sentry/scope.rb', line 205

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.

Returns:

  • (Boolean)


283
284
285
# File 'lib/sentry/scope.rb', line 283

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.

Parameters:

  • contexts (Hash) (defaults to: nil)
  • extras (Hash)
  • tags (Hash) (defaults to: nil)
  • user (Hash) (defaults to: nil)
  • level (String, Symbol) (defaults to: nil)
  • fingerprint (Array) (defaults to: nil)
  • attachments (Array<Attachment>) (defaults to: nil)

Returns:

  • (Array)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/sentry/scope.rb', line 167

def update_from_options(
  contexts: nil,
  extra: nil,
  tags: nil,
  user: nil,
  level: nil,
  fingerprint: nil,
  attachments: nil,
  **options
)
  self.contexts.merge!(contexts) if contexts
  self.extra.merge!(extra) if extra
  self.tags.merge!(tags) if tags
  self.user = user if user
  self.level = level if level
  self.fingerprint = fingerprint if fingerprint

  # Returns unsupported option keys so we can notify users.
  options.keys
end

#update_from_scope(scope) ⇒ void

This method returns an undefined value.

Updates the scope’s data from a given scope.

Parameters:



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/sentry/scope.rb', line 144

def update_from_scope(scope)
  self.breadcrumbs = scope.breadcrumbs
  self.contexts = scope.contexts
  self.extra = scope.extra
  self.tags = scope.tags
  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.attachments = scope.attachments
end