class Sentry::Scope

def add_attachment(**opts)

Add a new attachment to the scope.
def add_attachment(**opts)
  attachments << (attachment = Attachment.new(**opts))
  attachment
end

def add_breadcrumb(breadcrumb)

Returns:
  • (void) -

Parameters:
  • breadcrumb (Breadcrumb) --
def add_breadcrumb(breadcrumb)
  breadcrumbs.record(breadcrumb)
end

def add_event_processor(&block)

Returns:
  • (void) -

Parameters:
  • block (Proc) --
def add_event_processor(&block)
  @event_processors << block
end

def add_global_event_processor(&block)

Returns:
  • (void) -

Parameters:
  • block (Proc) --
def add_global_event_processor(&block)
  global_event_processors << block
end

def apply_to_event(event, hint = nil)

Returns:
  • (Event) -

Parameters:
  • hint (Hash) -- the hint data that'll be passed to event processors.
  • event (Event) --
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

def apply_to_telemetry(telemetry)

Returns:
  • (MetricEvent, LogEvent) - the telemetry event with scope context applied

Parameters:
  • telemetry (MetricEvent, LogEvent) -- the telemetry event to apply scope context to
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
  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

def clear

Returns:
  • (void) -
def clear
  set_default_value
end

def clear_breadcrumbs

Returns:
  • (void) -
def clear_breadcrumbs
  set_new_breadcrumb_buffer
end

def dup

Returns:
  • (Scope) -
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

def generate_propagation_context(env = nil)

Returns:
  • (void) -

Parameters:
  • env (Hash, nil) --
def generate_propagation_context(env = nil)
  @propagation_context = PropagationContext.new(self, env)
end

def get_span

Returns:
  • (Span, nil) -
def get_span
  span
end

def get_trace_context

Returns:
  • (Hash) -
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

def get_transaction

Returns:
  • (Transaction, nil) -
def get_transaction
  span.transaction if span
end

def global_event_processors

Returns:
  • (Array) -
def global_event_processors
  @global_event_processors ||= []
end

def initialize(max_breadcrumbs: nil)

Parameters:
  • max_breadcrumbs (Integer) -- the maximum number of breadcrumbs to be stored in the scope.
def initialize(max_breadcrumbs: nil)
  @max_breadcrumbs = max_breadcrumbs
  set_default_value
end

def os_context

Returns:
  • (Hash) -
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

def runtime_context

Returns:
  • (Hash) -
def runtime_context
  @runtime_context ||= {
    name: RbConfig::CONFIG["ruby_install_name"],
    version: RUBY_DESCRIPTION || Sentry.sys_command("ruby -v")
  }
end

def set_context(key, value)

@!macro set_context
def set_context(key, value)
  check_argument_type!(value, Hash)
  set_contexts(key => value)
end

def set_contexts(contexts_hash)

Returns:
  • (Hash) -

Parameters:
  • contexts (Hash) --
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

def set_default_value

def set_default_value
  @contexts = { os: self.class.os_context, runtime: self.class.runtime_context }
  @extra = {}
  @tags = {}
  @user = {}
  @level = :error
  @fingerprint = []
  @transaction_name = nil
  @transaction_source = nil
  @event_processors = []
  @rack_env = {}
  @span = nil
  @session = nil
  @attachments = []
  generate_propagation_context
  set_new_breadcrumb_buffer
end

def set_extra(key, value)

Returns:
  • (Hash) -

Parameters:
  • value (Object) --
  • key (String, Symbol) --
def set_extra(key, value)
  set_extras(key => value)
end

def set_extras(extras_hash)

@!macro set_extras
def set_extras(extras_hash)
  check_argument_type!(extras_hash, Hash)
  @extra.merge!(extras_hash)
end

def set_fingerprint(fingerprint)

Returns:
  • (Array) -

Parameters:
  • fingerprint (Array) --
def set_fingerprint(fingerprint)
  check_argument_type!(fingerprint, Array)
  @fingerprint = fingerprint
end

def set_level(level)

Returns:
  • (void) -

Parameters:
  • level (String, Symbol) --
def set_level(level)
  @level = level
end

def set_new_breadcrumb_buffer

def set_new_breadcrumb_buffer
  @breadcrumbs = BreadcrumbBuffer.new(@max_breadcrumbs)
end

def set_rack_env(env)

Returns:
  • (Hash) -

Parameters:
  • env (Hash) --
def set_rack_env(env)
  env = env || {}
  @rack_env = env
end

def set_session(session)

Returns:
  • (void) -

Parameters:
  • session (Session, nil) --
def set_session(session)
  @session = session
end

def set_span(span)

Returns:
  • (Span) -

Parameters:
  • span (Span) --
def set_span(span)
  check_argument_type!(span, Span)
  @span = span
end

def set_tag(key, value)

Returns:
  • (Hash) -

Parameters:
  • value (Object) --
  • key (String, Symbol) --
def set_tag(key, value)
  set_tags(key => value)
end

def set_tags(tags_hash)

@!macro set_tags
def set_tags(tags_hash)
  check_argument_type!(tags_hash, Hash)
  @tags.merge!(tags_hash)
end

def set_transaction_name(transaction_name, source: :custom)

Returns:
  • (void) -

Parameters:
  • transaction_name (String) --
def set_transaction_name(transaction_name, source: :custom)
  @transaction_name = transaction_name
  @transaction_source = source
end

def set_user(user_hash)

@!macro set_user
def set_user(user_hash)
  check_argument_type!(user_hash, Hash)
  @user = user_hash
end

def transaction_source_low_quality?

Returns:
  • (Boolean) -
def transaction_source_low_quality?
  transaction_source == :url
end

def update_from_options(

Returns:
  • (Array) -

Parameters:
  • attachments (Array) --
  • fingerprint (Array) --
  • level (String, Symbol) --
  • user (Hash) --
  • tags (Hash) --
  • extras (Hash) --
  • contexts (Hash) --
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

def update_from_scope(scope)

Returns:
  • (void) -

Parameters:
  • scope (Scope) --
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