Module: NewRelic::Agent::StatsEngine::Transactions

Included in:
NewRelic::Agent::StatsEngine
Defined in:
lib/new_relic/agent/stats_engine/transactions.rb

Defined Under Namespace

Modules: Shim

Instance Method Summary collapse

Instance Method Details

#end_transactionObject

Try to clean up gracefully, otherwise we leave things hanging around on thread locals. If it looks like a transaction is still in progress, then maybe this is an inner transaction and is ignored.



109
110
111
112
113
114
115
116
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 109

def end_transaction
  stack = scope_stack
  
  if stack && stack.empty?
    Thread::current[:newrelic_scope_stack] = nil
    Thread::current[:newrelic_scope_name] = nil
  end
end

#peek_scopeObject



79
80
81
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 79

def peek_scope
  scope_stack.last
end

#pop_scope(expected_scope, duration, time = Time.now.to_f) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 58

def pop_scope(expected_scope, duration, time=Time.now.to_f)
  capture_gc_time if collecting_gc?
  stack = scope_stack
  scope = stack.pop
  fail "unbalanced pop from blame stack, got #{scope ? scope.name : 'nil'}, expected #{expected_scope ? expected_scope.name : 'nil'}" if scope != expected_scope
  
  if !stack.empty? 
    if scope.deduct_call_time_from_parent
      stack.last.children_time += duration
    else
      stack.last.children_time += scope.children_time
    end
  end
  
  if @transaction_sampler
    @transaction_sampler.notice_pop_scope(scope.name, time)
    @transaction_sampler.notice_scope_empty(time) if stack.empty? 
  end
  scope
end

#push_scope(metric, time = Time.now.to_f, deduct_call_time_from_parent = true) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 37

def push_scope(metric, time = Time.now.to_f, deduct_call_time_from_parent = true)
  
  stack = scope_stack
  if collecting_gc?
    if stack.empty?
      # reset the gc time so we only include gc time spent during this call
      @last_gc_timestamp = GC.time
      @last_gc_count = GC.collections
    else
      capture_gc_time
    end
  end
  if @transaction_sampler
    @transaction_sampler.notice_first_scope_push(time) if stack.empty? 
    @transaction_sampler.notice_push_scope metric, time
  end
  scope = ScopeStackElement.new(metric, deduct_call_time_from_parent)
  stack.push scope
  scope
end

#remove_transaction_sampler(l) ⇒ Object



33
34
35
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 33

def remove_transaction_sampler(l)
  @transaction_sampler = nil
end

#scope_nameObject



95
96
97
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 95

def scope_name
  Thread::current[:newrelic_scope_name]
end

#scope_name=(transaction) ⇒ Object

set the name of the transaction for the current thread, which will be used to define the scope of all traced methods called on this thread until the scope stack is empty.

currently the transaction name is the name of the controller action that is invoked via the dispatcher, but conceivably we could use other transaction names in the future if the traced application does more than service http request via controller actions



91
92
93
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 91

def scope_name=(transaction)
  Thread::current[:newrelic_scope_name] = transaction
end

#start_transaction(name = nil) ⇒ Object

Start a new transaction, unless one is already in progress



100
101
102
103
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 100

def start_transaction(name = nil)
  Thread::current[:newrelic_scope_stack] ||= []
  self.scope_name = name if name
end

#transaction_sampler=(sampler) ⇒ Object



28
29
30
31
# File 'lib/new_relic/agent/stats_engine/transactions.rb', line 28

def transaction_sampler= sampler
  fail "Can't add a scope listener midflight in a transaction" if scope_stack.any?
  @transaction_sampler = sampler
end