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
Public Class Methods
# File lib/sentry-ruby.rb, line 48 def apply_patches(config) registered_patches.each do |patch| patch.call(config) end end
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
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
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
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
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
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
-
The SDK is not initialized yet.
-
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
Returns the current active client.
# File lib/sentry-ruby.rb, line 128 def get_current_client get_current_hub&.current_client 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.
# 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
Returns the current active scope.
# File lib/sentry-ruby.rb, line 133 def get_current_scope get_current_hub&.current_scope end
Returns the main thread's active hub.
# File lib/sentry-ruby.rb, line 106 def get_main_hub @main_hub end
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
# File lib/sentry-ruby.rb, line 214 def initialized? !!@main_hub end
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
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
# File lib/sentry-ruby.rb, line 218 def logger configuration.logger end
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
Patch Registration #####
# File lib/sentry-ruby.rb, line 44 def register_patch(&block) registered_patches << block end
# File lib/sentry-ruby.rb, line 54 def registered_patches @registered_patches ||= [] end
# File lib/sentry-ruby.rb, line 222 def sdk_meta META end
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
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
# File lib/sentry-ruby.rb, line 226 def utc_now Time.now.utc 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:
“`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