Class: Appsignal::Transaction
- Defined in:
- lib/appsignal/transaction.rb,
sig/appsignal.rbs
Constant Summary collapse
- HTTP_REQUEST =
"http_request"- BACKGROUND_JOB =
"background_job"
Class Method Summary collapse
-
.complete_current! ⇒ void
Complete the currently active transaction and unset it as the active transaction.
-
.create(namespace) ⇒ Transaction
Create a new transaction and set it as the currently active transaction.
-
.current ⇒ Appsignal::Transaction, Appsignal::Transaction::NilTransaction
Returns currently active transaction or a NilTransaction if none is active.
-
.current? ⇒ Boolean
Returns if any transaction is currently active or not.
Instance Method Summary collapse
-
#add_breadcrumb(category, action, message = "", metadata = {}, time = Time.now.utc) ⇒ Object
Add breadcrumbs to the transaction.
-
#add_custom_data(data) ⇒ void
(also: #set_custom_data)
Add custom data to the transaction.
-
#add_headers(given_headers = nil, &block) ⇒ void
(also: #set_headers)
Add headers to the transaction.
-
#add_params(given_params = nil, &block) ⇒ void
(also: #set_params)
Add parameters to the transaction.
-
#add_session_data(given_session_data = nil, &block) ⇒ void
(also: #set_session_data)
Add session data to the transaction.
-
#add_tags(given_tags = {}) ⇒ void
(also: #set_tags)
Add tags to the transaction.
-
#set_action(action) ⇒ void
Set an action name for the transaction.
-
#set_namespace(namespace) ⇒ void
Set the namespace for this transaction.
-
#set_queue_start(start) ⇒ void
Set queue start time for transaction.
Class Method Details
.complete_current! ⇒ void
This method returns an undefined value.
Complete the currently active transaction and unset it as the active transaction.
130 131 132 133 134 135 136 137 138 |
# File 'lib/appsignal/transaction.rb', line 130 def complete_current! current.complete rescue => e Appsignal.internal_logger.error( "Failed to complete transaction ##{current.transaction_id}. #{e.}" ) ensure clear_current_transaction! end |
.create(namespace) ⇒ Transaction
Create a new transaction and set it as the currently active transaction.
@param namespace — Namespace of the to be created transaction.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/appsignal/transaction.rb', line 32 def create(namespace) # Reset the transaction if it was already completed but not cleared if Thread.current[:appsignal_transaction]&.completed? Thread.current[:appsignal_transaction] = nil end if Thread.current[:appsignal_transaction].nil? # If not, start a new transaction set_current_transaction(Appsignal::Transaction.new(namespace)) else transaction = current # Otherwise, log the issue about trying to start another transaction Appsignal.internal_logger.warn( "Trying to start new transaction, but a transaction " \ "with id '#{transaction.transaction_id}' is already running. " \ "Using transaction '#{transaction.transaction_id}'." ) # And return the current transaction instead transaction end end |
.current ⇒ Appsignal::Transaction, Appsignal::Transaction::NilTransaction
Returns currently active transaction or a NilTransaction if none is active.
@see .current?
113 114 115 |
# File 'lib/appsignal/transaction.rb', line 113 def current Thread.current[:appsignal_transaction] || NilTransaction.new end |
.current? ⇒ Boolean
Returns if any transaction is currently active or not. A NilTransaction is not considered an active transaction.
@see .current
122 123 124 |
# File 'lib/appsignal/transaction.rb', line 122 def current? current && !current.nil_transaction? end |
Instance Method Details
#add_breadcrumb(category, action, message = "", metadata = {}, time = Time.now.utc) ⇒ Object
Add breadcrumbs to the transaction.
@param category — category of breadcrumb e.g. "UI", "Network", "Navigation", "Console".
@param action — name of breadcrumb e.g "The user clicked a button", "HTTP 500 from http://blablabla.com"
@param message — optional message in string format
@param metadata — key/value metadata in <string, string> format
@param time — time of breadcrumb, should respond to .to_i defaults to Time.now.utc
@see Appsignal.add_breadcrumb
@see https://docs.appsignal.com/ruby/instrumentation/breadcrumbs.html — Breadcrumb reference
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/appsignal/transaction.rb', line 520 def (category, action, = "", = {}, time = Time.now.utc) unless .is_a? Hash Appsignal.internal_logger.error "add_breadcrumb: Cannot add breadcrumb. " \ "The given metadata argument is not a Hash." return end @breadcrumbs.push( :time => time.to_i, :category => category, :action => action, :message => , :metadata => ) @breadcrumbs = @breadcrumbs.last(BREADCRUMB_LIMIT) end |
#add_custom_data(data) ⇒ void Also known as: set_custom_data
This method returns an undefined value.
Add custom data to the transaction.
@param data — Custom data to add to the transaction.
@see Helpers::Instrumentation#add_custom_data
@see https://docs.appsignal.com/guides/custom-data/sample-data.html — Sample data guide
501 502 503 |
# File 'lib/appsignal/transaction.rb', line 501 def add_custom_data(data) @custom_data.add(data) end |
#add_headers(given_headers = nil, &block) ⇒ void Also known as: set_headers
This method returns an undefined value.
Add headers to the transaction.
@param given_headers — A hash containing headers.
@see Helpers::Instrumentation#add_headers
@see https://docs.appsignal.com/guides/custom-data/sample-data.html — Sample data guide
465 466 467 |
# File 'lib/appsignal/transaction.rb', line 465 def add_headers(given_headers = nil, &block) @headers.add(given_headers, &block) end |
#add_params(given_params = nil, &block) ⇒ void Also known as: set_params
This method returns an undefined value.
Add parameters to the transaction.
When this method is called multiple times, it will merge the request parameters.
When both the given_params and a block is given to this method, the
block is leading and the argument will not be used.
@param given_params — The parameters to set on the transaction.
@see Helpers::Instrumentation#add_params
@see https://docs.appsignal.com/guides/custom-data/sample-data.html — Sample data guide
358 359 360 |
# File 'lib/appsignal/transaction.rb', line 358 def add_params(given_params = nil, &block) @params.add(given_params, &block) end |
#add_session_data(given_session_data = nil, &block) ⇒ void Also known as: set_session_data
This method returns an undefined value.
Add session data to the transaction.
When this method is called multiple times, it will merge the session data.
When both the given_session_data and a block is given to this method,
the block is leading and the argument will not be used.
@param given_session_data — A hash containing session data.
@see Helpers::Instrumentation#add_session_data
@see https://docs.appsignal.com/guides/custom-data/sample-data.html — Sample data guide
426 427 428 |
# File 'lib/appsignal/transaction.rb', line 426 def add_session_data(given_session_data = nil, &block) @session_data.add(given_session_data, &block) end |
#add_tags(given_tags = {}) ⇒ void Also known as:
This method returns an undefined value.
Add tags to the transaction.
When this method is called multiple times, it will merge the tags.
@param given_tags — Collection of tags.
@see Helpers::Instrumentation#add_tags
@see https://docs.appsignal.com/ruby/instrumentation/tagging.html — Tagging guide
404 405 406 |
# File 'lib/appsignal/transaction.rb', line 404 def ( = {}) @tags.merge!() end |
#set_action(action) ⇒ void
This method returns an undefined value.
Set an action name for the transaction.
An action name is used to identify the location of a certain sample; error and performance issues.
@param action — the action name to set.
@see Appsignal::Helpers::Instrumentation#set_action
547 548 549 550 551 552 |
# File 'lib/appsignal/transaction.rb', line 547 def set_action(action) return unless action @action = action @ext.set_action(action) end |
#set_namespace(namespace) ⇒ void
This method returns an undefined value.
Set the namespace for this transaction.
Useful to split up parts of an application into certain namespaces. For example: http requests, background jobs and administration panel controllers.
Note: The "http_request" namespace gets transformed on AppSignal.com to "Web" and "background_job" gets transformed to "Background".
@param namespace — namespace name to use for this transaction.
transaction.set_namespace("background")
@see Appsignal::Helpers::Instrumentation#set_namespace
@see https://docs.appsignal.com/guides/namespaces.html — Grouping with namespaces guide
595 596 597 598 599 600 |
# File 'lib/appsignal/transaction.rb', line 595 def set_namespace(namespace) return unless namespace @namespace = namespace @ext.set_namespace(namespace) end |
#set_queue_start(start) ⇒ void
This method returns an undefined value.
Set queue start time for transaction.
@param start — Queue start time in milliseconds.
610 611 612 613 614 615 616 |
# File 'lib/appsignal/transaction.rb', line 610 def set_queue_start(start) return unless start @ext.set_queue_start(start) rescue RangeError Appsignal.internal_logger.warn("Queue start value #{start} is too big") end |