Class: Appsignal::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/transaction.rb

Constant Summary collapse

HTTP_REQUEST =
"http_request"
BACKGROUND_JOB =
"background_job"

Class Method Summary collapse

Instance Method Summary collapse

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.message}"
  )
ensure
  clear_current_transaction!
end

.create(namespace) ⇒ Transaction

Create a new transaction and set it as the currently active 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

.currentAppsignal::Transaction, Appsignal::Transaction::NilTransaction

Returns currently active transaction or a NilTransaction if none is active.

See Also:



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 Also:



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) ⇒ void

This method returns an undefined value.

Add breadcrumbs to the transaction.

See Also:



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/appsignal/transaction.rb', line 473

def add_breadcrumb(category, action, message = "",  = {}, 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 => 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.



454
455
456
# File 'lib/appsignal/transaction.rb', line 454

def add_custom_data(data)
  @custom_data.add(data)
end

#add_headers(given_headers = nil) { ... } ⇒ void Also known as: set_headers

This method returns an undefined value.

Add headers to the transaction.

Yields:

  • This block is called when the transaction is sampled. The block’s return value will become the new headers.

Yield Returns:

See Also:

Since:

  • 4.0.0



418
419
420
# File 'lib/appsignal/transaction.rb', line 418

def add_headers(given_headers = nil, &block)
  @headers.add(given_headers, &block)
end

#add_params(given_params = nil) { ... } ⇒ 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.

Yields:

  • This block is called when the transaction is sampled. The block’s return value will become the new parameters.

Yield Returns:

See Also:

Since:

  • 4.0.0



311
312
313
# File 'lib/appsignal/transaction.rb', line 311

def add_params(given_params = nil, &block)
  @params.add(given_params, &block)
end

#add_session_data(given_session_data = nil) { ... } ⇒ 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.

Yields:

  • This block is called when the transaction is sampled. The block’s return value will become the new session data.

Yield Returns:

See Also:

Since:

  • 4.0.0



379
380
381
# File 'lib/appsignal/transaction.rb', line 379

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: set_tags

This method returns an undefined value.

Add tags to the transaction.

When this method is called multiple times, it will merge the tags.

Options Hash (given_tags):

  • :any (String, Symbol, Integer)

    The name of the tag as a Symbol.

  • "any" (String, Symbol, Integer)

    The name of the tag as a String.

See Also:

Since:

  • 4.0.0



357
358
359
# File 'lib/appsignal/transaction.rb', line 357

def add_tags(given_tags = {})
  @tags.merge!(given_tags)
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.

See Also:

Since:

  • 2.2.0



500
501
502
503
504
505
# File 'lib/appsignal/transaction.rb', line 500

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”.

Examples:

transaction.set_namespace("background")

See Also:

Since:

  • 2.2.0



548
549
550
551
552
553
# File 'lib/appsignal/transaction.rb', line 548

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.

Raises:

  • (RangeError)

    When the queue start time value is too big, this method raises a RangeError.

  • (TypeError)

    Raises a TypeError when the given start argument is not an Integer.



563
564
565
566
567
568
569
# File 'lib/appsignal/transaction.rb', line 563

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