Module: Sentry

Extended by:
Forwardable
Defined in:
lib/sentry/backtrace.rb,
lib/sentry/dsn.rb,
lib/sentry/hub.rb,
lib/sentry-ruby.rb,
lib/sentry/span.rb,
lib/sentry/event.rb,
lib/sentry/scope.rb,
lib/sentry/client.rb,
lib/sentry/logger.rb,
lib/sentry/version.rb,
lib/sentry/interface.rb,
lib/sentry/linecache.rb,
lib/sentry/transport.rb,
lib/sentry/breadcrumb.rb,
lib/sentry/exceptions.rb,
lib/sentry/integrable.rb,
lib/sentry/transaction.rb,
lib/sentry/configuration.rb,
lib/sentry/utils/real_ip.rb,
lib/sentry/utils/request_id.rb,
lib/sentry/background_worker.rb,
lib/sentry/breadcrumb_buffer.rb,
lib/sentry/rack/deprecations.rb,
lib/sentry/transaction_event.rb,
lib/sentry/interfaces/request.rb,
lib/sentry/interfaces/threads.rb,
lib/sentry/interfaces/exception.rb,
lib/sentry/interfaces/stacktrace.rb,
lib/sentry/rack/capture_exceptions.rb,
lib/sentry/transport/configuration.rb,
lib/sentry/breadcrumb/sentry_logger.rb,
lib/sentry/transport/http_transport.rb,
lib/sentry/transport/dummy_transport.rb,
lib/sentry/interfaces/single_exception.rb,
lib/sentry/utils/exception_cause_chain.rb,
lib/sentry/interfaces/stacktrace_builder.rb,
lib/sentry/benchmarks/benchmark_transport.rb,
lib/sentry/utils/argument_checking_helper.rb

Overview

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.

Defined Under Namespace

Modules: ArgumentCheckingHelper, Integrable, Rack, Utils Classes: BackgroundWorker, Backtrace, BenchmarkTransport, Breadcrumb, BreadcrumbBuffer, Client, Configuration, DSN, DummyTransport, Error, Event, ExceptionInterface, ExternalError, HTTPTransport, Hub, Interface, LineCache, Logger, RequestInterface, Scope, SingleExceptionInterface, Span, StacktraceBuilder, StacktraceInterface, ThreadsInterface, Transaction, TransactionEvent, Transport

Constant Summary collapse

META =
{ "name" => "sentry.ruby", "version" => Sentry::VERSION }.freeze
LOGGER_PROGNAME =
"sentry".freeze
THREAD_LOCAL =
:sentry_hub
VERSION =
"4.3.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.background_workerObject

Returns the value of attribute background_worker.



63
64
65
# File 'lib/sentry-ruby.rb', line 63

def background_worker
  @background_worker
end

Class Method Details

.add_breadcrumb(breadcrumb) ⇒ Object

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



82
83
84
# File 'lib/sentry-ruby.rb', line 82

def add_breadcrumb(breadcrumb)
  get_current_hub&.add_breadcrumb(breadcrumb)
end

.capture_event(event) ⇒ Object

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



161
162
163
# File 'lib/sentry-ruby.rb', line 161

def capture_event(event)
  get_current_hub&.capture_event(event)
end

.capture_exception(exception, **options, &block) ⇒ Object

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



151
152
153
# File 'lib/sentry-ruby.rb', line 151

def capture_exception(exception, **options, &block)
  get_current_hub&.capture_exception(exception, **options, &block)
end

.capture_message(message, **options, &block) ⇒ Object

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



156
157
158
# File 'lib/sentry-ruby.rb', line 156

def capture_message(message, **options, &block)
  get_current_hub&.capture_message(message, **options, &block)
end

.clone_hub_to_current_threadObject

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



109
110
111
# File 'lib/sentry-ruby.rb', line 109

def clone_hub_to_current_thread
  Thread.current[THREAD_LOCAL] = get_main_hub.clone
end

.configure_scope(&block) ⇒ Object

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” } “‘



123
124
125
# File 'lib/sentry-ruby.rb', line 123

def configure_scope(&block)
  get_current_hub&.configure_scope(&block)
end

.get_current_clientObject

Returns the current active client.



99
100
101
# File 'lib/sentry-ruby.rb', line 99

def get_current_client
  get_current_hub&.current_client
end

.get_current_hubObject

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.



89
90
91
92
93
94
95
96
# File 'lib/sentry-ruby.rb', line 89

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_LOCAL] || clone_hub_to_current_thread
end

.get_current_scopeObject

Returns the current active scope.



104
105
106
# File 'lib/sentry-ruby.rb', line 104

def get_current_scope
  get_current_hub&.current_scope
end

.get_main_hubObject

Returns the main thread’s active hub.



77
78
79
# File 'lib/sentry-ruby.rb', line 77

def get_main_hub
  @main_hub
end

.init {|config| ... } ⇒ Object

Yields:

  • (config)


65
66
67
68
69
70
71
72
73
74
# File 'lib/sentry-ruby.rb', line 65

def init(&block)
  config = Configuration.new
  yield(config) if block_given?
  client = Client.new(config)
  scope = Scope.new(max_breadcrumbs: config.max_breadcrumbs)
  hub = Hub.new(client, scope)
  Thread.current[THREAD_LOCAL] = hub
  @main_hub = hub
  @background_worker = Sentry::BackgroundWorker.new(config)
end

.initialized?Boolean



182
183
184
# File 'lib/sentry-ruby.rb', line 182

def initialized?
  !!@main_hub
end

.integrationsObject

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



46
47
48
# File 'lib/sentry-ruby.rb', line 46

def integrations
  @integrations ||= {}
end

.last_event_idObject

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



171
172
173
# File 'lib/sentry-ruby.rb', line 171

def last_event_id
  get_current_hub&.last_event_id
end

.loggerObject



186
187
188
# File 'lib/sentry-ruby.rb', line 186

def logger
  configuration.logger
end

.register_integration(name, version) ⇒ Object

Registers the SDK integration with its name and version.



51
52
53
54
# File 'lib/sentry-ruby.rb', line 51

def register_integration(name, version)
  meta = { name: "sentry.ruby.#{name}", version: version }.freeze
  integrations[name.to_s] = meta
end

.sdk_metaObject



36
37
38
# File 'lib/sentry-ruby.rb', line 36

def self.sdk_meta
  META
end

.start_transaction(**options) ⇒ Object

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



166
167
168
# File 'lib/sentry-ruby.rb', line 166

def start_transaction(**options)
  get_current_hub&.start_transaction(**options)
end

.sys_command(command) ⇒ Object



175
176
177
178
179
180
# File 'lib/sentry-ruby.rb', line 175

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_nowObject



40
41
42
# File 'lib/sentry-ruby.rb', line 40

def self.utc_now
  Time.now.utc
end

.with_scope(&block) ⇒ Object

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” } “‘



146
147
148
# File 'lib/sentry-ruby.rb', line 146

def with_scope(&block)
  get_current_hub&.with_scope(&block)
end