module Sentry

Inspired by Rails' and Airbrake's backtrace parsers.

Based on ActionDispatch::RemoteIp. All security-related precautions from that middleware have been removed, because the Event IP just needs to be accurate, and spoofing an IP here only makes data inaccurate, not insecure. Don't re-use this module if you have to trust the IP address.

Constants

LOGGER_PROGNAME
META
SENTRY_TRACE_HEADER_NAME
THREAD_LOCAL
VERSION

Attributes

background_worker[RW]

Public Class Methods

add_breadcrumb(breadcrumb, **options) click to toggle source

Takes an instance of Sentry::Breadcrumb and stores it to the current active scope.

# File lib/sentry-ruby.rb, line 111
def add_breadcrumb(breadcrumb, **options)
  get_current_hub&.add_breadcrumb(breadcrumb, **options)
end
apply_patches(config) click to toggle source
# File lib/sentry-ruby.rb, line 48
def apply_patches(config)
  registered_patches.each do |patch|
    patch.call(config)
  end
end
capture_event(event) click to toggle source

Takes an instance of Sentry::Event and dispatches it to the currently active hub.

# File lib/sentry-ruby.rb, line 190
def capture_event(event)
  get_current_hub&.capture_event(event)
end
capture_exception(exception, **options, &block) click to toggle source

Takes an exception and reports it to Sentry via the currently active hub.

# File lib/sentry-ruby.rb, line 180
def capture_exception(exception, **options, &block)
  get_current_hub&.capture_exception(exception, **options, &block)
end
capture_message(message, **options, &block) click to toggle source

Takes a message string and reports it to Sentry via the currently active hub.

# File lib/sentry-ruby.rb, line 185
def capture_message(message, **options, &block)
  get_current_hub&.capture_message(message, **options, &block)
end
clone_hub_to_current_thread() click to toggle source

Clones the main thread's active hub and stores it to the current thread.

# File lib/sentry-ruby.rb, line 138
def clone_hub_to_current_thread
  Thread.current.thread_variable_set(THREAD_LOCAL, get_main_hub.clone)
end
configure_scope(&block) click to toggle source

Takes a block and yields the current active scope.

“`ruby Sentry.configure_scope do |scope|

scope.set_tags(foo: "bar")

end

Sentry.capture_message(“test message”) # this event will have tags { foo: “bar” } “`

# File lib/sentry-ruby.rb, line 152
def configure_scope(&block)
  get_current_hub&.configure_scope(&block)
end
csp_report_uri() click to toggle source

Returns an uri for security policy reporting that's generated from the given DSN (To learn more about security policy reporting: docs.sentry.io/product/security-policy-reporting/)

It returns nil if

  1. The SDK is not initialized yet.

  2. The DSN is not provided or is invalid.

# File lib/sentry-ruby.rb, line 100
def csp_report_uri
  return unless initialized?
  configuration.csp_report_uri
end
get_current_client() click to toggle source

Returns the current active client.

# File lib/sentry-ruby.rb, line 128
def get_current_client
  get_current_hub&.current_client
end
get_current_hub() click to toggle source

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.

# File lib/sentry-ruby.rb, line 118
def get_current_hub
  # we need to assign a hub to the current thread if it doesn't have one yet
  #
  # ideally, we should do this proactively whenever a new thread is created
  # but it's impossible for the SDK to keep track every new thread
  # so we need to use this rather passive way to make sure the app doesn't crash
  Thread.current.thread_variable_get(THREAD_LOCAL) || clone_hub_to_current_thread
end
get_current_scope() click to toggle source

Returns the current active scope.

# File lib/sentry-ruby.rb, line 133
def get_current_scope
  get_current_hub&.current_scope
end
get_main_hub() click to toggle source

Returns the main thread's active hub.

# File lib/sentry-ruby.rb, line 106
def get_main_hub
  @main_hub
end
init() { |config| ... } click to toggle source

Main APIs #####

# File lib/sentry-ruby.rb, line 80
def init(&block)
  config = Configuration.new
  yield(config) if block_given?
  config.detect_release
  apply_patches(config)
  client = Client.new(config)
  scope = Scope.new(max_breadcrumbs: config.max_breadcrumbs)
  hub = Hub.new(client, scope)
  Thread.current.thread_variable_set(THREAD_LOCAL, hub)
  @main_hub = hub
  @background_worker = Sentry::BackgroundWorker.new(config)
end
initialized?() click to toggle source
# File lib/sentry-ruby.rb, line 214
def initialized?
  !!@main_hub
end
integrations() click to toggle source
Integrations #####

Returns a hash that contains all the integrations that have been registered to the main SDK.

# File lib/sentry-ruby.rb, line 61
def integrations
  @integrations ||= {}
end
last_event_id() click to toggle source

Returns the id of the lastly reported Sentry::Event.

# File lib/sentry-ruby.rb, line 200
def last_event_id
  get_current_hub&.last_event_id
end
logger() click to toggle source
# File lib/sentry-ruby.rb, line 218
def logger
  configuration.logger
end
register_integration(name, version) click to toggle source

Registers the SDK integration with its name and version.

# File lib/sentry-ruby.rb, line 66
def register_integration(name, version)
  meta = { name: "sentry.ruby.#{name}", version: version }.freeze
  integrations[name.to_s] = meta
end
register_patch(&block) click to toggle source

Patch Registration #####

# File lib/sentry-ruby.rb, line 44
def register_patch(&block)
  registered_patches << block
end
registered_patches() click to toggle source
# File lib/sentry-ruby.rb, line 54
def registered_patches
  @registered_patches ||= []
end
sdk_meta() click to toggle source
# File lib/sentry-ruby.rb, line 222
def sdk_meta
  META
end
start_transaction(**options) click to toggle source

Takes or initializes a new Sentry::Transaction and makes a sampling decision for it.

# File lib/sentry-ruby.rb, line 195
def start_transaction(**options)
  get_current_hub&.start_transaction(**options)
end
sys_command(command) click to toggle source

Helpers #####

# File lib/sentry-ruby.rb, line 207
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
utc_now() click to toggle source
# File lib/sentry-ruby.rb, line 226
def utc_now
  Time.now.utc
end
with_scope(&block) click to toggle source

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:

“`ruby Sentry.configure_scope do |scope|

scope.set_tags(foo: "bar")

end

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.capture_message(“test message 3”) # this event will have tags { foo: “bar” } “`

# File lib/sentry-ruby.rb, line 175
def with_scope(&block)
  get_current_hub&.with_scope(&block)
end