Skip to content

Commit

Permalink
Document Scope's public APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Jan 1, 2022
1 parent f489537 commit 3f75736
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
9 changes: 9 additions & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ def register_integration(name, version)
# @!method send_event
# @see Client#send_event
def_delegators :get_current_client, :configuration, :send_event

# @!method set_tags
# @see Scope#set_tags
# @!method set_extras
# @see Scope#set_extras
# @!method set_user
# @see Scope#set_user
# @!method set_context
# @see Scope#set_context
def_delegators :get_current_scope, :set_tags, :set_extras, :set_user, :set_context

##### Main APIs #####
Expand Down
77 changes: 76 additions & 1 deletion sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ class Scope

attr_reader(*ATTRIBUTES)

# @param 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

# Resets the scope's attributes to defaults.
# @return [void]
def clear
set_default_value
end

# Applies stored attributes and event processors to the given event.
# @param event [Event]
# @param hint [Hash] the hint data that'll be passed to event processors.
# @return [Event]
def apply_to_event(event, hint = nil)
event.tags = tags.merge(event.tags)
event.user = user.merge(event.user)
Expand All @@ -45,14 +52,20 @@ def apply_to_event(event, hint = nil)
event
end

# Adds the breadcrumb to the scope's breadcrumbs buffer.
# @param breadcrumb [Breadcrumb]
# @return [void]
def add_breadcrumb(breadcrumb)
breadcrumbs.record(breadcrumb)
end

# Clears the scope's breadcrumbs buffer
# @return [void]
def clear_breadcrumbs
set_new_breadcrumb_buffer
end

# @return [Scope]
def dup
copy = super
copy.breadcrumbs = breadcrumbs.dup
Expand All @@ -66,6 +79,9 @@ def dup
copy
end

# Updates the scope's data from a given scope.
# @param scope [Scope]
# @return [void]
def update_from_scope(scope)
self.breadcrumbs = scope.breadcrumbs
self.contexts = scope.contexts
Expand All @@ -77,6 +93,14 @@ def update_from_scope(scope)
self.span = scope.span
end

# Updates the scope's data from the given options.
# @param contexts [Hash]
# @param extras [Hash]
# @param tags [Hash]
# @param user [Hash]
# @param level [String, Symbol]
# @param fingerprint [Array]
# @return [void]
def update_from_options(
contexts: nil,
extra: nil,
Expand All @@ -93,77 +117,127 @@ def update_from_options(
self.fingerprint = fingerprint if fingerprint
end

# Sets the scope's rack_env attribute.
# @param env [Hash]
# @return [Hash]
def set_rack_env(env)
env = env || {}
@rack_env = env
end

# Sets the scope's span attribute.
# @param span [Span]
# @return [Span]
def set_span(span)
check_argument_type!(span, Span)
@span = span
end

# Sets the scope's user attribute.
# @param user [Hash]
# @return [Hash]
def set_user(user_hash)
check_argument_type!(user_hash, Hash)
@user = user_hash
end

# Updates the scope's extras attribute by merging with the old value.
# @param extras [Hash]
# @return [Hash]
def set_extras(extras_hash)
check_argument_type!(extras_hash, Hash)
@extra.merge!(extras_hash)
end

# Adds a new key-value pair to current extras.
# @param key [String, Symbol]
# @param value [Object]
# @return [Hash]
def set_extra(key, value)
set_extras(key => value)
end

# Updates the scope's tags attribute by merging with the old value.
# @param tags [Hash]
# @return [Hash]
def set_tags(tags_hash)
check_argument_type!(tags_hash, Hash)
@tags.merge!(tags_hash)
end

# Adds a new key-value pair to current tags.
# @param key [String, Symbol]
# @param value [Object]
# @return [Hash]
def set_tag(key, value)
set_tags(key => value)
end

# Updates the scope's contexts attribute by merging with the old value.
# @param contexts [Hash]
# @return [Hash]
def set_contexts(contexts_hash)
check_argument_type!(contexts_hash, Hash)
@contexts.merge!(contexts_hash) do |key, old, new|
new.merge(old)
end
end

# Adds a new key-value pair to current contexts.
# @param key [String, Symbol]
# @param value [Object]
# @return [Hash]
def set_context(key, value)
check_argument_type!(value, Hash)
set_contexts(key => value)
end

# Sets the scope's level attribute.
# @param level [String, Symbol]
# @return [void]
def set_level(level)
@level = level
end

# Appends a new transaction name to the scope.
# The "transaction" here does not refer to `Transaction` objects.
# @param transaction_name [String]
# @return [void]
def set_transaction_name(transaction_name)
@transaction_names << transaction_name
end

# Returns current transaction name.
# The "transaction" here does not refer to `Transaction` objects.
# @return [String, nil]
def transaction_name
@transaction_names.last
end

# Returns the associated Transaction object.
# @return [Transaction, nil]
def get_transaction
span.transaction if span
end

# Returns the associated Span object.
# @return [Span, nil]
def get_span
span
end

# Sets the scope's fingerprint attribute.
# @param fingerprint [Array]
# @return [Array]
def set_fingerprint(fingerprint)
check_argument_type!(fingerprint, Array)

@fingerprint = fingerprint
end

# Adds a new event processor [Proc] to the scope.
# @param block [Proc]
# @return [void]
def add_event_processor(&block)
@event_processors << block
end
Expand Down Expand Up @@ -193,8 +267,8 @@ def set_new_breadcrumb_buffer
@breadcrumbs = BreadcrumbBuffer.new(@max_breadcrumbs)
end


class << self
# @return [Hash]
def os_context
@os_context ||=
begin
Expand All @@ -208,6 +282,7 @@ def os_context
end
end

# @return [Hash]
def runtime_context
@runtime_context ||= {
name: RbConfig::CONFIG["ruby_install_name"],
Expand Down

0 comments on commit 3f75736

Please sign in to comment.