Module: Appsignal::Helpers::Instrumentation
- Includes:
- Utils::DeprecationMessage
- Included in:
- Appsignal
- Defined in:
- lib/appsignal/helpers/instrumentation.rb
Overview
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
-
#add_breadcrumb(category, action, message = "", metadata = {}, time = Time.now.utc) ⇒ void
Add breadcrumbs to the transaction.
-
#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.
-
#listen_for_error(tags = nil, namespace = Appsignal::Transaction::HTTP_REQUEST) { ... } ⇒ Object
(also: #listen_for_exception)
Listen for an error to occur and send it to AppSignal.
-
#monitor_single_transaction(name, env = {}, &block) ⇒ Object
Monitor a transaction, stop AppSignal and wait for this single transaction to be flushed.
-
#monitor_transaction(name, env = {}) { ... } ⇒ Object
Creates an AppSignal transaction for the given block.
-
#send_error(error, tags = nil, namespace = nil) {|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_error(exception, tags = nil, namespace = nil) {|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.
-
#tag_request(tags = {}) ⇒ void
(also: #tag_job)
Set tags on the current transaction.
-
#without_instrumentation { ... } ⇒ Object
Convenience method for skipping instrumentations around a block of code.
Methods included from Utils::DeprecationMessage
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.
489 490 491 492 493 494 495 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 489 def (category, action, = "", = {}, time = Time.now.utc) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.(category, action, , , time) 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".
536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 536 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
Instrumentation helper for SQL queries.
This helper filters out values from SQL queries so you don't have to.
580 581 582 583 584 585 586 587 588 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 580 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
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 Appsignal.active? is true
.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 136 def listen_for_error( = nil, namespace = Appsignal::Transaction::HTTP_REQUEST ) yield rescue Exception => error # rubocop:disable Lint/RescueException send_error(error) do |transaction| transaction.() if transaction.set_namespace(namespace) if namespace end raise error end |
#monitor_single_transaction(name, env = {}, &block) ⇒ Object
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.
103 104 105 106 107 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 103 def monitor_single_transaction(name, env = {}, &block) monitor_transaction(name, env, &block) ensure stop("monitor_single_transaction") end |
#monitor_transaction(name, env = {}) { ... } ⇒ Object
Creates an AppSignal transaction for the given block.
If AppSignal is not Appsignal.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.
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 90 91 92 93 94 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 60 def monitor_transaction(name, env = {}, &block) # Always verify input, even when Appsignal is not active. # This makes it more likely invalid arguments get flagged in test/dev # environments. if name.start_with?("perform_job") namespace = Appsignal::Transaction::BACKGROUND_JOB request = Appsignal::Transaction::GenericRequest.new(env) elsif name.start_with?("process_action") namespace = Appsignal::Transaction::HTTP_REQUEST request = ::Rack::Request.new(env) else internal_logger.error "Unrecognized name '#{name}': names must " \ "start with either 'perform_job' (for jobs and tasks) or " \ "'process_action' (for HTTP requests)" return yield end return yield unless active? transaction = Appsignal::Transaction.create( SecureRandom.uuid, namespace, request ) begin Appsignal.instrument(name, &block) 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 = nil) {|transaction| ... } ⇒ void Also known as: send_exception
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.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 203 def send_error( error, = nil, namespace = nil ) if call_location = caller(1..1).first \ "The tags argument for `Appsignal.send_error` is deprecated. " \ "Please use the block method to set tags instead.\n\n" \ " Appsignal.send_error(error) do |transaction|\n" \ " transaction.set_tags(#{})\n" \ " end\n\n" \ "Appsignal.send_error called on location: #{call_location}" end if namespace call_location = caller(1..1).first \ "The namespace argument for `Appsignal.send_error` is deprecated. " \ "Please use the block method to set the namespace instead.\n\n" \ " Appsignal.send_error(error) do |transaction|\n" \ " transaction.set_namespace(#{namespace.inspect})\n" \ " end\n\n" \ "Appsignal.send_error called on location: #{call_location}" end 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( SecureRandom.uuid, namespace || Appsignal::Transaction::HTTP_REQUEST, Appsignal::Transaction::GenericRequest.new({}) ) transaction.() if transaction.set_error(error) yield transaction if block_given? 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.
359 360 361 362 363 364 365 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 359 def set_action(action) return if !active? || !Appsignal::Transaction.current? || action.nil? Appsignal::Transaction.current.set_action(action) end |
#set_error(exception, tags = nil, namespace = nil) {|transaction| ... } ⇒ void Also known as: set_exception, add_exception
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.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 300 def set_error(exception, = nil, namespace = nil) if call_location = caller(1..1).first \ "The tags argument for `Appsignal.set_error` is deprecated. " \ "Please use the block method to set tags instead.\n\n" \ " Appsignal.set_error(error) do |transaction|\n" \ " transaction.set_tags(#{})\n" \ " end\n\n" \ "Appsignal.set_error called on location: #{call_location}" end if namespace call_location = caller(1..1).first \ "The namespace argument for `Appsignal.set_error` is deprecated. " \ "Please use the block method to set the namespace instead.\n\n" \ " Appsignal.set_error(error) do |transaction|\n" \ " transaction.set_namespace(#{namespace.inspect})\n" \ " end\n\n" \ "Appsignal.set_error called on location: #{call_location}" end 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) transaction.() if transaction.set_namespace(namespace) if namespace 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.
399 400 401 402 403 404 405 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 399 def set_namespace(namespace) return if !active? || !Appsignal::Transaction.current? || namespace.nil? Appsignal::Transaction.current.set_namespace(namespace) end |
#tag_request(tags = {}) ⇒ void Also known as: tag_job
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.
440 441 442 443 444 445 446 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 440 def tag_request( = {}) return unless active? return unless Appsignal::Transaction.current? transaction = Appsignal::Transaction.current transaction.() end |
#without_instrumentation { ... } ⇒ Object
Convenience method for skipping instrumentations around a block of code.
600 601 602 603 604 605 |
# File 'lib/appsignal/helpers/instrumentation.rb', line 600 def without_instrumentation Appsignal::Transaction.current&.pause! yield ensure Appsignal::Transaction.current&.resume! end |