Class: Opener::Daemons::Transaction

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

Overview

Class for storing information of a single transaction in a thread.

Constant Summary collapse

THREAD_KEY =

The name of the key to store the current transaction in.

Returns:

  • (Symbol)
:opener_daemons_transaction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.



35
36
37
# File 'lib/opener/daemons/transaction.rb', line 35

def initialize
  @parameters = {}
end

Instance Attribute Details

#parametersHash (readonly)

Returns:

  • (Hash)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opener/daemons/transaction.rb', line 9

class Transaction
  attr_reader :parameters

  ##
  # The name of the key to store the current transaction in.
  #
  # @return [Symbol]
  #
  THREAD_KEY = :opener_daemons_transaction

  ##
  # Returns the current transaction.
  #
  # @return [Opener::Daemons::Transaction]
  #
  def self.current
    return Thread.current[THREAD_KEY] ||= new
  end

  ##
  # Removes the current transaction
  #
  def self.reset_current
    Thread.current[THREAD_KEY] = nil
  end

  def initialize
    @parameters = {}
  end

  ##
  # Merges the given parameters with the existing ones.
  #
  # If New Relic is enabled the parameters are also added to the current
  # New Relic transaction.
  #
  # @param [Hash] parameters
  #
  def add_parameters(parameters = {})
    @parameters = @parameters.merge(parameters)

    if Daemons.newrelic?
      NewRelic::Agent.add_custom_attributes parameters
    end
  end
end

Class Method Details

.currentOpener::Daemons::Transaction

Returns the current transaction.



24
25
26
# File 'lib/opener/daemons/transaction.rb', line 24

def self.current
  return Thread.current[THREAD_KEY] ||= new
end

.reset_currentObject

Removes the current transaction



31
32
33
# File 'lib/opener/daemons/transaction.rb', line 31

def self.reset_current
  Thread.current[THREAD_KEY] = nil
end

Instance Method Details

#add_parameters(parameters = {}) ⇒ Object

Merges the given parameters with the existing ones.

If New Relic is enabled the parameters are also added to the current New Relic transaction.

Parameters:

  • parameters (Hash) (defaults to: {})


47
48
49
50
51
52
53
# File 'lib/opener/daemons/transaction.rb', line 47

def add_parameters(parameters = {})
  @parameters = @parameters.merge(parameters)

  if Daemons.newrelic?
    NewRelic::Agent.add_custom_attributes parameters
  end
end