Module: Appsignal::Helpers::Instrumentation
- Included in:
- Appsignal
- Defined in:
- lib/appsignal/helpers/instrumentation.rb
Instance Method Summary collapse
-
#add_breadcrumb(category, action, message = "", metadata = {}, time = Time.now.utc) ⇒ void
Add breadcrumbs to the transaction.
-
#add_custom_data(data) ⇒ void
(also: #set_custom_data)
Add custom data to the current transaction.
-
#add_headers(headers = nil) { ... } ⇒ void
(also: #set_headers)
Add request headers to the current transaction.
-
#add_params(params = nil) { ... } ⇒ void
(also: #set_params)
Add parameters to the current transaction.
-
#add_session_data(session_data = nil) { ... } ⇒ void
(also: #set_session_data)
Add session data to the current transaction.
-
#add_tags(tags = {}) ⇒ void
(also: #tag_request, #tag_job, #set_tags)
Add tags to the current transaction.
-
#ignore_instrumentation_events { ... } ⇒ Object
Convenience method for ignoring instrumentation events in a block of code.
-
#instrument(name, title = nil, body = nil, body_format = Appsignal::EventFormatter::DEFAULT) { ... } ⇒ Object
Instrument helper for AppSignal.
-
#instrument_sql(name, title = nil, body = nil) { ... } ⇒ Object
Instrumentation helper for SQL queries.
-
#monitor(action:, namespace: nil) { ... } ⇒ Object
Monitor a block of code with AppSignal.
-
#monitor_and_stop(action:, namespace: nil, &block) ⇒ Object
Instrument a block of code and stop AppSignal.
-
#report_error(exception) {|transaction| ... } ⇒ void
(also: #report_exception)
Report an error to AppSignal.
-
#send_error(error) {|transaction| ... } ⇒ void
(also: #send_exception)
Send an error to AppSignal regardless of the context.
-
#set_action(action) ⇒ void
Set a custom action name for the current transaction.
-
#set_empty_params! ⇒ void
Mark the parameters sample data to be set as an empty value.
-
#set_error(exception) {|transaction| ... } ⇒ void
(also: #set_exception, #add_exception)
Set an error on the current transaction.
-
#set_namespace(namespace) ⇒ void
Set a custom namespace for the current transaction.
Instance Method Details
#add_breadcrumb(category, action, message = "", metadata = {}, time = Time.now.utc) ⇒ void
This method returns an undefined value.
Add breadcrumbs to the transaction.
Breadcrumbs can be used to trace what path a user has taken before encounterin an error.
Only the last 20 added breadcrumbs will be saved.
702 703 704 705 706 707 708 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 702 def (category, action, = "", = {}, time = Time.now.utc) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.(category, action, , , time) end |
#add_custom_data(data) ⇒ void Also known as: set_custom_data
This method returns an undefined value.
Add custom data to the current transaction.
Add extra information about the request or background that cannot be expressed in tags, like nested data structures.
If the root data type changes between calls of this method, the last method call is stored.
462 463 464 465 466 467 468 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 462 def add_custom_data(data) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.add_custom_data(data) end |
#add_headers(headers = nil) { ... } ⇒ void Also known as: set_headers
This method returns an undefined value.
Add request headers to the current transaction.
Request headers are automatically added by most of our integrations. It should not be necessary to call this method unless you want to also report different request headers.
To filter request headers, see our request header filtering guide.
When both the ‘request_headers` argument and a block is given to this method, the block is leading and the argument will not be used.
654 655 656 657 658 659 660 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 654 def add_headers(headers = nil, &block) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.add_headers(headers, &block) end |
#add_params(params = nil) { ... } ⇒ void Also known as: set_params
This method returns an undefined value.
Add parameters to the current transaction.
Parameters are automatically added by most of our integrations. It should not be necessary to call this method unless you want to report different parameters.
To filter parameters, see our parameter filtering guide.
When both the ‘params` argument and a block is given to this method, the block is leading and the argument will not be used.
548 549 550 551 552 553 554 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 548 def add_params(params = nil, &block) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.add_params(params, &block) end |
#add_session_data(session_data = nil) { ... } ⇒ void Also known as: set_session_data
This method returns an undefined value.
Add session data to the current transaction.
Session data is automatically added by most of our integrations. It should not be necessary to call this method unless you want to report different session data.
To filter session data, see our session data filtering guide.
When both the ‘session_data` argument and a block is given to this method, the bock is leading and the argument will not be used.
613 614 615 616 617 618 619 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 613 def add_session_data(session_data = nil, &block) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.add_session_data(session_data, &block) end |
#add_tags(tags = {}) ⇒ void Also known as: tag_request, tag_job,
This method returns an undefined value.
Add tags to the current transaction.
Tags are extra bits of information that are added to transaction and appear on sample details pages on AppSignal.com.
When this method is called multiple times, it will merge the tags.
506 507 508 509 510 511 512 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 506 def ( = {}) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.() end |
#ignore_instrumentation_events { ... } ⇒ Object
Convenience method for ignoring instrumentation events in a block of code.
-
This helper ignores events, like those created ‘Appsignal.instrument`, within this block. This includes custom instrumentation and events recorded by AppSignal integrations for requests, database queries, view rendering, etc.
-
The time spent in the block is still reported on the transaction.
-
Errors and metrics are reported from within this block.
827 828 829 830 831 832 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 827 def ignore_instrumentation_events Appsignal::Transaction.current&.pause! yield ensure Appsignal::Transaction.current&.resume! end |
#instrument(name, title = nil, body = nil, body_format = Appsignal::EventFormatter::DEFAULT) { ... } ⇒ Object
Instrument helper for AppSignal.
For more help, read our custom instrumentation guide, listed under “See also”.
748 749 750 751 752 753 754 755 756 757 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 748 def instrument( name, title = nil, body = nil, body_format = Appsignal::EventFormatter::DEFAULT, &block ) Appsignal::Transaction.current .instrument(name, title, body, body_format, &block) end |
#instrument_sql(name, title = nil, body = nil) { ... } ⇒ Object
Instrumentation helper for SQL queries.
This helper filters out values from SQL queries so you don’t have to.
789 790 791 792 793 794 795 796 797 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 789 def instrument_sql(name, title = nil, body = nil, &block) instrument( name, title, body, Appsignal::EventFormatter::SQL_BODY_FORMAT, &block ) end |
#monitor(action:, namespace: nil) { ... } ⇒ Object
Monitor a block of code with AppSignal.
This is a helper to create an AppSignal transaction, track any errors that may occur and complete the transaction.
This helper is recommended to be used in Ruby scripts and parts of an app not already instrumented by AppSignal’s automatic instrumentations.
Use this helper in combination with our #instrument helper to track instrumentation events.
If AppSignal is not active (Appsignal.active?) it will still execute the block, but not create a transaction for it.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 112 def monitor(action:, namespace: nil) return yield unless active? has_parent_transaction = Appsignal::Transaction.current? if has_parent_transaction callers = caller Appsignal::Utils::StdoutAndLoggerMessage.warning \ "A transaction is active around this 'Appsignal.monitor' call. " \ "Calling `Appsignal.monitor` in another `Appsignal.monitor` block has no effect. " \ "The namespace and action are not updated for the active transaction." \ "Did you mean to use `Appsignal.instrument`? " \ "Update the 'Appsignal.monitor' call in: #{callers.first}" return yield if block_given? return end transaction = if has_parent_transaction Appsignal::Transaction.current else Appsignal::Transaction.create(namespace || Appsignal::Transaction::HTTP_REQUEST) end begin yield if block_given? rescue Exception => error # rubocop:disable Lint/RescueException transaction.set_error(error) raise error ensure transaction.set_action_if_nil(action.to_s) if action && action != :set_later Appsignal::Transaction.complete_current! end end |
#monitor_and_stop(action:, namespace: nil, &block) ⇒ Object
Instrument a block of code and stop AppSignal.
Useful for cases such as one-off scripts where there is no long running process active and the data needs to be sent after the process exists.
Acts the same way as #monitor. See that method for more documentation.
156 157 158 159 160 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 156 def monitor_and_stop(action:, namespace: nil, &block) monitor(:namespace => namespace, :action => action, &block) ensure Appsignal.stop("monitor_and_stop") end |
#report_error(exception) {|transaction| ... } ⇒ void Also known as: report_exception
This method returns an undefined value.
Report an error to AppSignal.
If a transaction is currently active, it will report the error on the current transaction. If no transaction is active, it will report the error on a new transaction.
If a transaction is active and the transaction already has an error reported on it, it will report multiple errors, up to a maximum of 10 errors.
If a block is given to this method, the metadata set in this block will only be applied to the transaction created for the given error. The block will be called when the transaction is completed, which can be much later than when #report_error is called.
Note: If AppSignal is not active, no error is reported.
Note: If the given exception argument is not an Exception subclass, it will not be reported.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 330 def report_error(exception, &block) unless exception.is_a?(Exception) internal_logger.error "Appsignal.report_error: Cannot add error. " \ "The given value is not an exception: #{exception.inspect}" return end return unless active? has_parent_transaction = Appsignal::Transaction.current? transaction = if has_parent_transaction Appsignal::Transaction.current else Appsignal::Transaction.new(Appsignal::Transaction::HTTP_REQUEST) end transaction.add_error(exception, &block) transaction.complete unless has_parent_transaction end |
#send_error(error) {|transaction| ... } ⇒ void Also known as: send_exception
This method returns an undefined value.
Send an error to AppSignal regardless of the context.
**We recommend using the #report_error helper instead.**
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.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 202 def send_error(error, &block) return unless active? unless error.is_a?(Exception) internal_logger.error "Appsignal.send_error: Cannot send error. " \ "The given value is not an exception: #{error.inspect}" return end transaction = Appsignal::Transaction.new(Appsignal::Transaction::HTTP_REQUEST) transaction.set_error(error, &block) transaction.complete end |
#set_action(action) ⇒ void
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.
373 374 375 376 377 378 379 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 373 def set_action(action) return if !active? || !Appsignal::Transaction.current? || action.nil? Appsignal::Transaction.current.set_action(action) end |
#set_empty_params! ⇒ void
This method returns an undefined value.
Mark the parameters sample data to be set as an empty value.
Use this helper to unset request parameters / background job arguments and not report any for this transaction.
If parameters would normally be added by AppSignal instrumentations of libraries, these parameters will not be added to the Transaction.
Calling #add_params after this helper will add new parameters to the transaction.
573 574 575 576 577 578 579 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 573 def set_empty_params! return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.set_empty_params! end |
#set_error(exception) {|transaction| ... } ⇒ void Also known as: set_exception, add_exception
This method returns an undefined value.
Set an error on the current transaction.
**We recommend using the #report_error helper instead.**
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.
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 267 def set_error(exception) unless exception.is_a?(Exception) internal_logger.error "Appsignal.set_error: Cannot set error. " \ "The given value is not an exception: #{exception.inspect}" return end return if !active? || !Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.set_error(exception) yield transaction if block_given? end |
#set_namespace(namespace) ⇒ void
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.
415 416 417 418 419 420 421 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 415 def set_namespace(namespace) return if !active? || !Appsignal::Transaction.current? || namespace.nil? Appsignal::Transaction.current.set_namespace(namespace) end |