Module: Appsignal::Helpers::Instrumentation Private
- Included in:
- Appsignal
- Defined in:
- lib/appsignal/helpers/instrumentation.rb
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Instance Method Summary collapse
-
#instrument(name, title = nil, body = nil, body_format = Appsignal::EventFormatter::DEFAULT) { ... } ⇒ Object
private
Instrument helper for AppSignal.
-
#instrument_sql(name, title = nil, body = nil) { ... } ⇒ Object
private
Instrumentation helper for SQL queries.
-
#listen_for_error(tags = nil, namespace = Appsignal::Transaction::HTTP_REQUEST) { ... } ⇒ Object
(also: #listen_for_exception)
private
Listen for an error to occur and send it to AppSignal.
-
#monitor_single_transaction(name, env = {}, &block) ⇒ Object
private
Monitor a transaction, stop AppSignal and wait for this single transaction to be flushed.
-
#monitor_transaction(name, env = {}) { ... } ⇒ Object
private
Creates an AppSignal transaction for the given block.
-
#send_error(error, tags = nil, namespace = Appsignal::Transaction::HTTP_REQUEST) {|transaction| ... } ⇒ void
(also: #send_exception)
private
Send an error to AppSignal regardless of the context.
-
#set_action(action) ⇒ void
private
Set a custom action name for the current transaction.
-
#set_error(exception, tags = nil, namespace = nil) ⇒ void
(also: #set_exception, #add_exception)
private
Set an error on the current transaction.
-
#set_namespace(namespace) ⇒ void
private
Set a custom namespace for the current transaction.
-
#tag_request(tags = {}) ⇒ void
(also: #tag_job)
private
Set tags on the current transaction.
-
#without_instrumentation { ... } ⇒ Object
private
Convenience method for skipping instrumentations around a block of code.
Instance Method Details
#instrument(name, title = nil, body = nil, body_format = Appsignal::EventFormatter::DEFAULT) { ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Instrument helper for AppSignal.
For more help, read our custom instrumentation guide, listed under "See also".
413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 413 def instrument( name, title = nil, body = nil, body_format = Appsignal::EventFormatter::DEFAULT ) Appsignal::Transaction.current.start_event yield if block_given? ensure Appsignal::Transaction .current .finish_event(name, title, body, body_format) end |
#instrument_sql(name, title = nil, body = nil) { ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Instrumentation helper for SQL queries.
This helper filters out values from SQL queries so you don't have to.
457 458 459 460 461 462 463 464 465 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 457 def instrument_sql(name, title = nil, body = nil, &block) instrument( name, title, body, Appsignal::EventFormatter::SQL_BODY_FORMAT, &block ) end |
#listen_for_error(tags = nil, namespace = Appsignal::Transaction::HTTP_REQUEST) { ... } ⇒ Object Also known as: listen_for_exception
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Listen for an error to occur and send it to AppSignal.
Uses #send_error to directly send the error in a separate transaction. Does not add the error to the current transaction.
Make sure that AppSignal is integrated in your application beforehand.
AppSignal won't record errors unless Config#active? is true
.
131 132 133 134 135 136 137 138 139 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 131 def listen_for_error( = nil, namespace = Appsignal::Transaction::HTTP_REQUEST ) yield rescue Exception => error # rubocop:disable Lint/RescueException send_error(error, , namespace) raise error end |
#monitor_single_transaction(name, env = {}, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Monitor a transaction, stop AppSignal and wait for this single transaction to be flushed.
Useful for cases such as Rake tasks and Resque-like systems where a process is forked and immediately exits after the transaction finishes.
98 99 100 101 102 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 98 def monitor_single_transaction(name, env = {}, &block) monitor_transaction(name, env, &block) ensure stop("monitor_single_transaction") end |
#monitor_transaction(name, env = {}) { ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an AppSignal transaction for the given block.
If AppSignal is not active? it will still execute the block, but not create a transaction for it.
A event is created for this transaction with the name given in the
name
argument. The event name must start with either perform_job
or
process_action
to differentiate between the "web" and "background"
namespace. Custom namespaces are not supported by this helper method.
This helper method also captures any exception that occurs in the given block.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 59 def monitor_transaction(name, env = {}) return yield unless active? if name.start_with?("perform_job".freeze) namespace = Appsignal::Transaction::BACKGROUND_JOB request = Appsignal::Transaction::GenericRequest.new(env) elsif name.start_with?("process_action".freeze) namespace = Appsignal::Transaction::HTTP_REQUEST request = ::Rack::Request.new(env) else logger.error("Unrecognized name '#{name}'") return end transaction = Appsignal::Transaction.create( SecureRandom.uuid, namespace, request ) begin Appsignal.instrument(name) do yield end rescue Exception => error # rubocop:disable Lint/RescueException transaction.set_error(error) raise error ensure transaction.set_http_or_background_action(request.env) transaction.set_http_or_background_queue_start Appsignal::Transaction.complete_current! end end |
#send_error(error, tags = nil, namespace = Appsignal::Transaction::HTTP_REQUEST) {|transaction| ... } ⇒ void Also known as: send_exception
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Send an error to AppSignal regardless of the context.
Records and send the exception to AppSignal.
This instrumentation helper does not require a transaction to be active, it starts a new transaction by itself.
Use #set_error if your want to add an exception to the current transaction.
Note: Does not do anything if AppSignal is not active or when the "error" is not a class extended from Ruby's Exception class.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 192 def send_error( error, = nil, namespace = Appsignal::Transaction::HTTP_REQUEST ) return unless active? unless error.is_a?(Exception) logger.error("Can't send error, given value is not an exception") return end transaction = Appsignal::Transaction.new( SecureRandom.uuid, namespace, Appsignal::Transaction::GenericRequest.new({}) ) transaction.() if transaction.set_error(error) yield transaction if block_given? transaction.complete end |
#set_action(action) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Set a custom action name for the current transaction.
When using an integration such as the Rails or Sinatra AppSignal will try to find the action name from the controller or endpoint for you.
If you want to customize the action name as it appears on AppSignal.com you can use this method. This overrides the action name AppSignal generates in an integration.
287 288 289 290 291 292 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 287 def set_action(action) return if !active? || Appsignal::Transaction.current.nil? || action.nil? Appsignal::Transaction.current.set_action(action) end |
#set_error(exception, tags = nil, namespace = nil) ⇒ void Also known as: set_exception, add_exception
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Set an error on the current transaction.
Note: Does not do anything if AppSignal is not active, no transaction is currently active or when the "error" is not a class extended from Ruby's Exception class.
253 254 255 256 257 258 259 260 261 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 253 def set_error(exception, = nil, namespace = nil) return if !active? || Appsignal::Transaction.current.nil? || exception.nil? transaction = Appsignal::Transaction.current transaction.set_error(exception) transaction.() if transaction.set_namespace(namespace) if namespace end |
#set_namespace(namespace) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Set a custom namespace for the current transaction.
When using an integration such as Rails or Sidekiq AppSignal will try to find a appropriate namespace for the transaction.
A Rails controller will be automatically put in the "http_request" namespace, while a Sidekiq background job is put in the "background_job" namespace.
Note: The "http_request" namespace gets transformed on AppSignal.com to "Web" and "background_job" gets transformed to "Background".
If you want to customize the namespace in which transactions appear you can use this method. This overrides the namespace AppSignal uses by default.
A common request we've seen is to split the administration panel from the main application.
326 327 328 329 330 331 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 326 def set_namespace(namespace) return if !active? || Appsignal::Transaction.current.nil? || namespace.nil? Appsignal::Transaction.current.set_namespace(namespace) end |
#tag_request(tags = {}) ⇒ void Also known as: tag_job
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Set tags on the current transaction.
Tags are extra bits of information that are added to transaction and appear on sample details pages on AppSignal.com.
366 367 368 369 370 371 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 366 def tag_request( = {}) return unless active? transaction = Appsignal::Transaction.current return false unless transaction transaction.() end |
#without_instrumentation { ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convenience method for skipping instrumentations around a block of code.
477 478 479 480 481 482 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 477 def without_instrumentation Appsignal::Transaction.current.pause! if Appsignal::Transaction.current yield ensure Appsignal::Transaction.current.resume! if Appsignal::Transaction.current end |