Class: Opener::Daemons::Transaction
- Inherits:
-
Object
- Object
- Opener::Daemons::Transaction
- 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.
:opener_daemons_transaction
Instance Attribute Summary collapse
- #parameters ⇒ Hash readonly
Class Method Summary collapse
-
.current ⇒ Opener::Daemons::Transaction
Returns the current transaction.
-
.reset_current ⇒ Object
Removes the current transaction.
Instance Method Summary collapse
-
#add_parameters(parameters = {}) ⇒ Object
Merges the given parameters with the existing ones.
-
#initialize ⇒ Transaction
constructor
A new instance of Transaction.
Constructor Details
#initialize ⇒ Transaction
Returns a new instance of Transaction.
35 36 37 |
# File 'lib/opener/daemons/transaction.rb', line 35 def initialize @parameters = {} end |
Instance Attribute Details
#parameters ⇒ Hash (readonly)
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
.current ⇒ Opener::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_current ⇒ Object
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.
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 |