Class: Appsignal::Transaction

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

Constant Summary collapse

HTTP_REQUEST =

Returns:

  • (String)
"http_request"
BACKGROUND_JOB =

Returns:

  • (String)
"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.

@param namespace — Namespace of the to be created transaction.

Parameters:

  • namespace (String)

Returns:



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 .current?

Returns:



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

Returns:

  • (Boolean)


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

@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

Parameters:



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

Parameters:

  • given_headers (::Hash[String, Object], nil) (defaults to: nil)


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

Parameters:

  • given_params (?(::Hash[String, Object] | ::Array[Object]), nil) (defaults to: nil)


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

Parameters:

  • given_session_data (::Hash[String, Object], nil) (defaults to: nil)


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

@param given_tags — Collection of tags.

@see Helpers::Instrumentation#add_tags

@see https://docs.appsignal.com/ruby/instrumentation/tagging.html — Tagging guide

Parameters:

  • given_tags (::Hash[String, Object]) (defaults to: {})


404
405
406
# File 'lib/appsignal/transaction.rb', line 404

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.

@param action — the action name to set.

@see Appsignal::Helpers::Instrumentation#set_action

Parameters:

  • action (String)


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

Parameters:

  • namespace (String)


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.

Parameters:

  • start (Integer)


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