Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite documents with yard #1635

Merged
merged 5 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sentry-ruby/.yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--exclude lib/sentry/utils/
--exclude lib/sentry/core_ext
2 changes: 2 additions & 0 deletions sentry-ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ gem "benchmark-ips"
gem "benchmark_driver"
gem "benchmark-ipsa"
gem "benchmark-memory"

gem "yard", "~> 0.9.27"
104 changes: 76 additions & 28 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Sentry
THREAD_LOCAL = :sentry_hub

class << self
# @!visibility private
def exception_locals_tp
@exception_locals_tp ||= TracePoint.new(:raise) do |tp|
exception = tp.raised_exception
Expand All @@ -55,46 +56,60 @@ def exception_locals_tp
end
end

# @!attribute [rw] background_worker
# @return [BackgroundWorker]
attr_accessor :background_worker

##### Patch Registration #####
#

# @!visibility private
def register_patch(&block)
registered_patches << block
end

# @!visibility private
def apply_patches(config)
registered_patches.each do |patch|
patch.call(config)
end
end

# @!visibility private
def registered_patches
@registered_patches ||= []
end

##### Integrations #####
#

# Returns a hash that contains all the integrations that have been registered to the main SDK.
#
# @return [Hash{String=>Hash}]
def integrations
@integrations ||= {}
end

# Registers the SDK integration with its name and version.
#
# @param name [String] name of the integration
# @param version [String] version of the integration
def register_integration(name, version)
meta = { name: "sentry.ruby.#{name}", version: version }.freeze
integrations[name.to_s] = meta
end

##### Method Delegation #####
#

extend Forwardable

def_delegators :get_current_client, :configuration, :send_event
def_delegators :get_current_scope, :set_tags, :set_extras, :set_user, :set_context

##### Main APIs #####

# Initializes the SDK with given configuration.
#
# @yieldparam config [Configuration]
# @return [void]
def init(&block)
config = Configuration.new
yield(config) if block_given?
Expand All @@ -116,31 +131,45 @@ def init(&block)
end
end

# Returns true if the SDK is initialized.
#
# @return [Boolean]
def initialized?
!!@main_hub
end

# Returns an uri for security policy reporting that's generated from the given DSN
# (To learn more about security policy reporting: https://docs.sentry.io/product/security-policy-reporting/)
#
# It returns nil if
# - The SDK is not initialized yet.
# - The DSN is not provided or is invalid.
#
# 1. The SDK is not initialized yet.
# 2. The DSN is not provided or is invalid.
# @return [String, nil]
def csp_report_uri
return unless initialized?
configuration.csp_report_uri
end

# Returns the main thread's active hub.
#
# @return [Hub]
def get_main_hub
@main_hub
end

# Takes an instance of Sentry::Breadcrumb and stores it to the current active scope.
#
# @return [Breadcrumb, nil]
def add_breadcrumb(breadcrumb, **options)
get_current_hub&.add_breadcrumb(breadcrumb, **options)
end

# Returns the current active hub.
# If the current thread doesn't have an active hub, it will clone the main thread's active hub,
# stores it in the current thread, and then returns it.
#
# @return [Hub]
def get_current_hub
# we need to assign a hub to the current thread if it doesn't have one yet
#
Expand All @@ -151,104 +180,123 @@ def get_current_hub
end

# Returns the current active client.
# @return [Client, nil]
def get_current_client
get_current_hub&.current_client
end

# Returns the current active scope.
#
# @return [Scope, nil]
def get_current_scope
get_current_hub&.current_scope
end

# Clones the main thread's active hub and stores it to the current thread.
#
# @return [void]
def clone_hub_to_current_thread
Thread.current.thread_variable_set(THREAD_LOCAL, get_main_hub.clone)
end

# Takes a block and yields the current active scope.
#
# ```ruby
# Sentry.configure_scope do |scope|
# scope.set_tags(foo: "bar")
# end
# @example
# Sentry.configure_scope do |scope|
# scope.set_tags(foo: "bar")
# end
#
# Sentry.capture_message("test message") # this event will have tags { foo: "bar" }
# ```
# Sentry.capture_message("test message") # this event will have tags { foo: "bar" }
#
# @yieldparam scope [Scope]
# @return [void]
def configure_scope(&block)
get_current_hub&.configure_scope(&block)
end

# Takes a block and yields a temporary scope.
# The temporary scope will inherit all the attributes from the current active scope and replace it to be the active
# scope inside the block. For example:
# scope inside the block.
#
# ```ruby
# Sentry.configure_scope do |scope|
# scope.set_tags(foo: "bar")
# end
# @example
# Sentry.configure_scope do |scope|
# scope.set_tags(foo: "bar")
# end
#
# Sentry.capture_message("test message") # this event will have tags { foo: "bar" }
# Sentry.capture_message("test message") # this event will have tags { foo: "bar" }
#
# Sentry.with_scope do |temp_scope|
# temp_scope.set_tags(foo: "baz")
# Sentry.capture_message("test message 2") # this event will have tags { foo: "baz" }
# end
# Sentry.with_scope do |temp_scope|
# temp_scope.set_tags(foo: "baz")
# Sentry.capture_message("test message 2") # this event will have tags { foo: "baz" }
# end
#
# Sentry.capture_message("test message 3") # this event will have tags { foo: "bar" }
# ```
# Sentry.capture_message("test message 3") # this event will have tags { foo: "bar" }
#
# @yieldparam scope [Scope]
# @return [void]
def with_scope(&block)
get_current_hub&.with_scope(&block)
end

# Takes an exception and reports it to Sentry via the currently active hub.
#
# @yieldparam scope [Scope]
# @return [Event, nil]
def capture_exception(exception, **options, &block)
get_current_hub&.capture_exception(exception, **options, &block)
end

# Takes a message string and reports it to Sentry via the currently active hub.
#
# @yieldparam scope [Scope]
# @return [Event, nil]
def capture_message(message, **options, &block)
get_current_hub&.capture_message(message, **options, &block)
end

# Takes an instance of Sentry::Event and dispatches it to the currently active hub.
#
# @return [Event, nil]
def capture_event(event)
get_current_hub&.capture_event(event)
end

# Takes or initializes a new Sentry::Transaction and makes a sampling decision for it.
#
# @return [Transaction, nil]
def start_transaction(**options)
get_current_hub&.start_transaction(**options)
end

# Returns the id of the lastly reported Sentry::Event.
#
# @return [String, nil]
def last_event_id
get_current_hub&.last_event_id
end


##### Helpers #####
#

# @!visibility private
def sys_command(command)
result = `#{command} 2>&1` rescue nil
return if result.nil? || result.empty? || ($CHILD_STATUS && $CHILD_STATUS.exitstatus != 0)

result.strip
end

def initialized?
!!@main_hub
end

# @!visibility private
def logger
configuration.logger
end

# @!visibility private
def sdk_meta
META
end

# @!visibility private
def utc_now
Time.now.utc
end
Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry/backtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Sentry
# Front end to parsing the backtrace for each notice
# @api private
class Backtrace
# Handles backtrace parsing line by line
class Line
Expand Down
Loading