Class: NewRelic::Agent::Instrumentation::ActionControllerSubscriber

Inherits:
EventedSubscriber
  • Object
show all
Defined in:
lib/new_relic/agent/instrumentation/action_controller_subscriber.rb

Instance Method Summary collapse

Methods inherited from EventedSubscriber

#event_stack, #log_notification_error, #pop_event, #push_event, subscribe, subscribed?

Constructor Details

#initializeActionControllerSubscriber

Returns a new instance of ActionControllerSubscriber.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 10

def initialize
  super
  NewRelic::Agent.instance.events.subscribe(:before_call) do |env|

    request = begin
                require 'rack'
                ::Rack::Request.new(env)
              rescue => e
                Agent.logger.debug("Error creating Rack::Request object: #{e}")
                nil
              end
    TransactionState.reset(request)
  end
end

Instance Method Details

#filter(params) ⇒ Object



115
116
117
118
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 115

def filter(params)
  filters = Rails.application.config.filter_parameters
  ActionDispatch::Http::ParameterFilter.new(filters).filter(params)
end

#finish(name, id, payload) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 41

def finish(name, id, payload)
  event = pop_event(id)
  event.payload.merge!(payload)

  set_enduser_ignore if event.enduser_ignored?

  if NewRelic::Agent.is_execution_traced? && !event.ignored?
    event.finalize_metric_name!
    record_queue_time(event)
    record_metrics(event)
    record_apdex(event)
    record_instance_busy(event)
    stop_transaction(event)
  else
    Agent.instance.pop_trace_execution_flag
  end
rescue => e
  log_notification_error(e, name, 'finish')
end

#record_apdex(event) ⇒ Object



82
83
84
85
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 82

def record_apdex(event)
  return if event.apdex_ignored?
  Transaction.record_apdex(event.end, event.exception_encountered?)
end

#record_instance_busy(event) ⇒ Object



87
88
89
90
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 87

def record_instance_busy(event)
  BusyCalculator.dispatcher_start(event.time)
  BusyCalculator.dispatcher_finish(event.end)
end

#record_metric_on_parent_transaction(metric, time) ⇒ Object



78
79
80
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 78

def record_metric_on_parent_transaction(metric, time)
  NewRelic::Agent::Transaction.parent.stats_hash.record(metric, time)
end

#record_metrics(event) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 65

def record_metrics(event)
  controller_metric = MetricSpec.new(event.metric_name)
  txn = Transaction.current
  metrics = [ 'HttpDispatcher']
  if txn.has_parent?
    parent_metric = MetricSpec.new(event.metric_name, StatsEngine::MetricStats::SCOPE_PLACEHOLDER)
    record_metric_on_parent_transaction(parent_metric, event.duration)
  end
  metrics << controller_metric.dup

  Agent.instance.stats_engine.record_metrics(metrics, event.duration)
end

#record_queue_time(event) ⇒ Object



92
93
94
95
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 92

def record_queue_time(event)
  return unless event.queue_start
  QueueTime.record_frontend_metrics(event.queue_start, event.time)
end

#set_enduser_ignoreObject



61
62
63
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 61

def set_enduser_ignore
  TransactionState.get.request_ignore_enduser = true
end

#start(name, id, payload) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 25

def start(name, id, payload)
  payload[:request] = TransactionState.get.request
  event = ControllerEvent.new(name, Time.now, nil, id, payload)
  push_event(event)

  if NewRelic::Agent.is_execution_traced? && !event.ignored?
    start_transaction(event)
  else
    # if this transaction is ignored, make sure child
    # transaction are also ignored
    NewRelic::Agent.instance.push_trace_execution_flag(false)
  end
rescue => e
  log_notification_error(e, name, 'start')
end

#start_transaction(event) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 97

def start_transaction(event)
  txn = Transaction.start(:controller,
                          :request => event.payload[:request],
                          :filtered_params => filter(event.payload[:params]))
  txn.apdex_start = (event.queue_start || event.time)
  txn.name = event.metric_name

  event.scope = Agent.instance.stats_engine \
    .push_scope(:action_controller, event.time)
end

#stop_transaction(event) ⇒ Object



108
109
110
111
112
113
# File 'lib/new_relic/agent/instrumentation/action_controller_subscriber.rb', line 108

def stop_transaction(event)
  Agent.instance.stats_engine \
    .pop_scope(event.scope, event.metric_name, event.end)
ensure
  Transaction.stop
end