newrelic_rpm 6.1.0.352 → 6.6.0.358

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +34 -96
  4. data/CHANGELOG.md +139 -0
  5. data/lib/new_relic/agent/agent.rb +51 -134
  6. data/lib/new_relic/agent/commands/agent_command_router.rb +2 -21
  7. data/lib/new_relic/agent/configuration/default_source.rb +53 -41
  8. data/lib/new_relic/agent/configuration/environment_source.rb +4 -2
  9. data/lib/new_relic/agent/configuration/event_harvest_config.rb +39 -0
  10. data/lib/new_relic/agent/configuration/high_security_source.rb +1 -0
  11. data/lib/new_relic/agent/configuration/manager.rb +13 -1
  12. data/lib/new_relic/agent/configuration/server_source.rb +34 -1
  13. data/lib/new_relic/agent/connect/request_builder.rb +69 -0
  14. data/lib/new_relic/agent/connect/response_handler.rb +61 -0
  15. data/lib/new_relic/agent/error_collector.rb +2 -2
  16. data/lib/new_relic/agent/error_event_aggregator.rb +2 -1
  17. data/lib/new_relic/agent/error_trace_aggregator.rb +1 -0
  18. data/lib/new_relic/agent/event_aggregator.rb +26 -32
  19. data/lib/new_relic/agent/hostname.rb +1 -1
  20. data/lib/new_relic/agent/inbound_request_monitor.rb +2 -2
  21. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +24 -42
  22. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +46 -74
  23. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +70 -53
  24. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +168 -0
  25. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +41 -48
  26. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +4 -4
  27. data/lib/new_relic/agent/instrumentation/grape.rb +2 -3
  28. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +76 -0
  29. data/lib/new_relic/agent/instrumentation/{rails5 → rails_notifications}/action_cable.rb +5 -6
  30. data/lib/new_relic/agent/instrumentation/{rails5 → rails_notifications}/action_controller.rb +3 -3
  31. data/lib/new_relic/agent/instrumentation/{rails4 → rails_notifications}/action_view.rb +3 -3
  32. data/lib/new_relic/agent/javascript_instrumentor.rb +1 -1
  33. data/lib/new_relic/agent/new_relic_service.rb +0 -4
  34. data/lib/new_relic/agent/new_relic_service/json_marshaller.rb +0 -1
  35. data/lib/new_relic/agent/parameter_filtering.rb +18 -5
  36. data/lib/new_relic/agent/priority_sampled_buffer.rb +2 -0
  37. data/lib/new_relic/agent/span_event_aggregator.rb +2 -5
  38. data/lib/new_relic/agent/threading/backtrace_service.rb +3 -3
  39. data/lib/new_relic/agent/threading/thread_profile.rb +9 -23
  40. data/lib/new_relic/agent/transaction.rb +0 -2
  41. data/lib/new_relic/agent/transaction/trace.rb +3 -8
  42. data/lib/new_relic/agent/transaction/trace_builder.rb +0 -1
  43. data/lib/new_relic/agent/transaction_event_recorder.rb +3 -3
  44. data/lib/new_relic/agent/transaction_sampler.rb +1 -5
  45. data/lib/new_relic/agent/transaction_time_aggregator.rb +19 -4
  46. data/lib/new_relic/control/class_methods.rb +7 -1
  47. data/lib/new_relic/control/frameworks/{rails5.rb → rails_notifications.rb} +1 -1
  48. data/lib/new_relic/rack/browser_monitoring.rb +10 -8
  49. data/lib/new_relic/version.rb +1 -1
  50. data/lib/tasks/config.rake +1 -2
  51. data/newrelic_rpm.gemspec +2 -9
  52. data/test/agent_helper.rb +18 -5
  53. metadata +18 -51
  54. data/lib/new_relic/agent/commands/xray_session.rb +0 -55
  55. data/lib/new_relic/agent/commands/xray_session_collection.rb +0 -161
  56. data/lib/new_relic/agent/instrumentation/active_record_4.rb +0 -42
  57. data/lib/new_relic/agent/instrumentation/active_record_5.rb +0 -41
  58. data/lib/new_relic/agent/instrumentation/evented_subscriber.rb +0 -104
  59. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +0 -32
  60. data/lib/new_relic/agent/instrumentation/rails5/action_view.rb +0 -27
  61. data/lib/new_relic/agent/transaction/xray_sample_buffer.rb +0 -64
  62. data/lib/new_relic/control/frameworks/rails6.rb +0 -14
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+
6
+ module NewRelic
7
+ module Agent
8
+ module Connect
9
+
10
+ class ResponseHandler
11
+
12
+ def initialize(agent, config)
13
+ @agent = agent
14
+ @config = config
15
+ end
16
+
17
+ # Takes a hash of configuration data returned from the
18
+ # server and uses it to set local variables and to
19
+ # initialize various parts of the agent that are configured
20
+ # separately.
21
+ #
22
+ # Can accommodate most arbitrary data - anything extra is
23
+ # ignored unless we say to do something with it here.
24
+ def configure_agent(config_data)
25
+ return if config_data == nil
26
+
27
+ @agent.agent_id = config_data['agent_run_id']
28
+
29
+ security_policies = config_data.delete('security_policies')
30
+
31
+ add_server_side_config(config_data)
32
+ add_security_policy_config(security_policies) if security_policies
33
+
34
+ @agent.transaction_rules = RulesEngine.create_transaction_rules(config_data)
35
+ @agent.stats_engine.metric_rules = RulesEngine.create_metric_rules(config_data)
36
+
37
+ # If you're adding something else here to respond to the server-side config,
38
+ # use Agent.instance.events.subscribe(:initial_configuration_complete) callback instead!
39
+ end
40
+
41
+ def add_server_side_config(config_data)
42
+ if config_data['agent_config']
43
+ ::NewRelic::Agent.logger.debug "Using config from server"
44
+ end
45
+
46
+ ::NewRelic::Agent.logger.debug "Server provided config: #{config_data.inspect}"
47
+ server_config = NewRelic::Agent::Configuration::ServerSource.new(config_data, @config)
48
+ @config.replace_or_add_config(server_config)
49
+ end
50
+
51
+ def add_security_policy_config(security_policies)
52
+ ::NewRelic::Agent.logger.info 'Installing security policies'
53
+ security_policy_source = NewRelic::Agent::Configuration::SecurityPolicySource.new(security_policies)
54
+ @config.replace_or_add_config(security_policy_source)
55
+ # drop data collected before applying security policies
56
+ @agent.drop_buffered_data
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -18,9 +18,9 @@ module NewRelic
18
18
  attr_reader :error_trace_aggregator, :error_event_aggregator
19
19
 
20
20
  # Returns a new error collector
21
- def initialize
21
+ def initialize events
22
22
  @error_trace_aggregator = ErrorTraceAggregator.new(MAX_ERROR_QUEUE_LENGTH)
23
- @error_event_aggregator = ErrorEventAggregator.new
23
+ @error_event_aggregator = ErrorEventAggregator.new events
24
24
 
25
25
  # lookup of exception class names to ignore. Hash for fast access
26
26
  @ignore = {}
@@ -13,7 +13,8 @@ module NewRelic
13
13
 
14
14
  named :ErrorEventAggregator
15
15
  capacity_key :'error_collector.max_event_samples_stored'
16
- enabled_key :'error_collector.capture_events'
16
+ enabled_keys :'error_collector.enabled',
17
+ :'error_collector.capture_events'
17
18
  buffer_class PrioritySampledBuffer
18
19
 
19
20
  def record noticed_error, transaction_payload = nil
@@ -91,6 +91,7 @@ module NewRelic
91
91
 
92
92
  def register_config_callbacks
93
93
  Agent.config.register_callback(:'error_collector.enabled') do |enabled|
94
+ reset! if enabled == false
94
95
  ::NewRelic::Agent.logger.debug "Error traces will #{enabled ? '' : 'not '}be sent to the New Relic service."
95
96
  end
96
97
  end
@@ -8,20 +8,6 @@ module NewRelic
8
8
  module Agent
9
9
  class EventAggregator
10
10
  class << self
11
- # This architecture requires that every aggregator
12
- # implementation provide its own `named`, `capacity_key`, and
13
- # `enabled_...` settings. In particular, we do not allow
14
- # subclasses to share these values from an intermediate
15
- # superclass.
16
-
17
- def inherited(subclass)
18
- # Prevent uninitialized variable warnings; we have to do
19
- # this as soon as the subclass is defined, before its
20
- # implementor calls enabled_key() or enabled_fn().
21
- subclass.instance_variable_set(:@enabled_key, nil)
22
- subclass.instance_variable_set(:@enabled_fn, nil)
23
- end
24
-
25
11
  def named named = nil
26
12
  named ? @named = named.to_s.freeze : @named
27
13
  end
@@ -30,10 +16,25 @@ module NewRelic
30
16
  key ? @capacity_key = key : @capacity_key
31
17
  end
32
18
 
33
- def enabled_key key = nil
34
- key ? @enabled_key = key : @enabled_key
19
+ # An aggregator can specify one or more keys to check to see if it is
20
+ # enabled. Multiple keys will be &&'d and the enabled status of the
21
+ # aggregator will be reset when agent configuration changes.
22
+
23
+ def enabled_keys *keys
24
+ if keys.empty?
25
+ @enabled_keys ||= []
26
+ else
27
+ @enabled_keys = Array(keys)
28
+ @enabled_fn = ->(){ @enabled_keys.all? { |k| Agent.config[k] } }
29
+ end
35
30
  end
36
31
 
32
+ alias_method :enabled_key, :enabled_keys
33
+
34
+ # This can be used instead of `enabled_key(s)` for more fine grained
35
+ # control over whether an aggregator should be enabled. The enabled fn
36
+ # will be reevaluated after configuration changes
37
+
37
38
  def enabled_fn fn = nil
38
39
  fn ? @enabled_fn = fn : @enabled_fn
39
40
  end
@@ -47,13 +48,13 @@ module NewRelic
47
48
  end
48
49
  end
49
50
 
50
- def initialize
51
+ def initialize events
51
52
  @lock = Mutex.new
52
53
  @buffer = self.class.buffer_class.new NewRelic::Agent.config[self.class.capacity_key]
53
- @enabled = false
54
+ @enabled = self.class.enabled_fn ? self.class.enabled_fn.call : false
54
55
  @notified_full = false
55
56
  register_capacity_callback
56
- register_enabled_callback
57
+ register_enabled_callback events
57
58
  after_initialize
58
59
  end
59
60
 
@@ -66,7 +67,6 @@ module NewRelic
66
67
  end
67
68
 
68
69
  def enabled?
69
- return self.class.enabled_fn.call if self.class.enabled_fn
70
70
  @enabled
71
71
  end
72
72
 
@@ -126,18 +126,12 @@ module NewRelic
126
126
  end
127
127
  end
128
128
 
129
- def register_enabled_callback
130
- unless self.class.enabled_key || self.class.enabled_fn
131
- ::NewRelic::Agent.logger.warn "#{self.class.named} needs an enabled_key or an enabled_fn."
132
- end
133
-
134
- return if self.class.enabled_fn
135
-
136
- NewRelic::Agent.config.register_callback(self.class.enabled_key) do |enabled|
137
- # intentionally unsynchronized for liveness
138
- @enabled = enabled
139
- ::NewRelic::Agent.logger.debug "#{self.class.named} will #{enabled ? '' : 'not '}be sent to the New Relic service."
140
- end
129
+ def register_enabled_callback events
130
+ events.subscribe(:server_source_configuration_added) {
131
+ @enabled = self.class.enabled_fn.call
132
+ reset! if @enabled == false
133
+ ::NewRelic::Agent.logger.debug "#{self.class.named} will #{@enabled ? '' : 'not '}be sent to the New Relic service."
134
+ }
141
135
  end
142
136
 
143
137
  def notify_if_full
@@ -13,7 +13,7 @@ module NewRelic
13
13
  dyno_name = "#{matching_prefix}.*" if matching_prefix
14
14
  dyno_name
15
15
  else
16
- Socket.gethostname
16
+ Socket.gethostname.force_encoding(Encoding::UTF_8)
17
17
  end
18
18
  end
19
19
 
@@ -20,8 +20,8 @@ module NewRelic
20
20
  attr_reader :obfuscator
21
21
 
22
22
  def initialize(events)
23
- events.subscribe(:finished_configuring) do
24
- # This requires :encoding_key, so must wait until :finished_configuring
23
+ events.subscribe(:initial_configuration_complete) do
24
+ # This requires :encoding_key, so must wait until :initial_configuration_complete
25
25
  setup_obfuscator
26
26
  on_finished_configuring(events)
27
27
  end
@@ -1,77 +1,59 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
- require 'new_relic/agent/instrumentation/evented_subscriber'
4
+ require 'new_relic/agent/instrumentation/notifications_subscriber'
5
5
 
6
6
  module NewRelic
7
7
  module Agent
8
8
  module Instrumentation
9
- class ActionCableSubscriber < EventedSubscriber
9
+ class ActionCableSubscriber < NotificationsSubscriber
10
10
 
11
11
  PERFORM_ACTION = 'perform_action.action_cable'.freeze
12
12
 
13
- def start name, id, payload #THREAD_LOCAL_ACCESS
13
+ def start(name, id, payload) #THREAD_LOCAL_ACCESS
14
14
  return unless state.is_execution_traced?
15
- event = super
16
- if event.name == PERFORM_ACTION
17
- start_transaction event
15
+
16
+ finishable = if name == PERFORM_ACTION
17
+ Tracer.start_transaction_or_segment(
18
+ name: transaction_name_from_payload(payload),
19
+ category: :action_cable
20
+ )
18
21
  else
19
- start_recording_metrics event
22
+ Tracer.start_segment(name: metric_name_from_payload(name, payload))
20
23
  end
24
+ push_segment(id, finishable)
21
25
  rescue => e
22
26
  log_notification_error e, name, 'start'
23
27
  end
24
28
 
25
- def finish name, id, payload #THREAD_LOCAL_ACCESS
29
+ def finish(name, id, payload) #THREAD_LOCAL_ACCESS
26
30
  return unless state.is_execution_traced?
27
- event = super
31
+
28
32
  notice_error payload if payload.key? :exception
29
- if event.name == PERFORM_ACTION
30
- finish_transaction event
31
- else
32
- stop_recording_metrics event
33
- end
33
+ finishable = pop_segment(id)
34
+ finishable.finish if finishable
34
35
  rescue => e
35
36
  log_notification_error e, name, 'finish'
36
37
  end
37
38
 
38
39
  private
39
40
 
40
- def start_transaction event
41
- event.payload[:finishable] = Tracer.start_transaction_or_segment(
42
- name: transaction_name_from_event(event),
43
- category: :action_cable
44
- )
45
- end
46
-
47
- def finish_transaction event
48
- (finishable = event.payload[:finishable]) && finishable.finish
49
- end
50
-
51
- def start_recording_metrics event
52
- event.payload[:segment] = Tracer.start_segment name: metric_name_from_event(event)
53
- end
54
-
55
- def stop_recording_metrics event
56
- event.payload[:segment].finish if event.payload[:segment]
57
- end
58
-
59
- def transaction_name_from_event event
60
- "Controller/ActionCable/#{event.payload[:channel_class]}/#{event.payload[:action]}"
41
+ def transaction_name_from_payload(payload)
42
+ "Controller/ActionCable/#{payload[:channel_class]}/#{payload[:action]}"
61
43
  end
62
44
 
63
- def metric_name_from_event event
64
- "Ruby/ActionCable/#{event.payload[:channel_class]}/#{action_name_from_event(event)}"
45
+ def metric_name_from_payload(name, payload)
46
+ "Ruby/ActionCable/#{payload[:channel_class]}/#{action_name(name)}"
65
47
  end
66
48
 
67
- DOT_ACTION_CABLE = ".action_cable".freeze
68
- EMPTY_STRING = "".freeze
49
+ DOT_ACTION_CABLE = '.action_cable'.freeze
50
+ EMPTY_STRING = ''.freeze
69
51
 
70
- def action_name_from_event event
71
- event.name.gsub DOT_ACTION_CABLE, EMPTY_STRING
52
+ def action_name(name)
53
+ name.gsub DOT_ACTION_CABLE, EMPTY_STRING
72
54
  end
73
55
 
74
- def notice_error payload
56
+ def notice_error(payload)
75
57
  NewRelic::Agent.notice_error payload[:exception_object]
76
58
  end
77
59
  end
@@ -1,24 +1,24 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
- require 'new_relic/agent/instrumentation/evented_subscriber'
4
+ require 'new_relic/agent/instrumentation/notifications_subscriber'
5
5
  require 'new_relic/agent/instrumentation/ignore_actions'
6
6
  require 'new_relic/agent/parameter_filtering'
7
7
 
8
8
  module NewRelic
9
9
  module Agent
10
10
  module Instrumentation
11
- class ActionControllerSubscriber < EventedSubscriber
11
+ class ActionControllerSubscriber < NotificationsSubscriber
12
12
 
13
13
  def start(name, id, payload) #THREAD_LOCAL_ACCESS
14
14
  # @req is a historically stable but not guaranteed Rails header property
15
15
  request = payload[:headers].instance_variable_get(:@req)
16
16
 
17
- event = ControllerEvent.new(name, Time.now, nil, id, payload, request)
18
- push_event(event)
17
+ controller_class = controller_class(payload)
19
18
 
20
- if state.is_execution_traced? && !event.ignored?
21
- start_transaction(event)
19
+ if state.is_execution_traced? && !should_ignore(payload, controller_class)
20
+ finishable = start_transaction_or_segment(payload, request, controller_class)
21
+ push_segment(id, finishable)
22
22
  else
23
23
  # if this transaction is ignored, make sure child
24
24
  # transaction are also ignored
@@ -30,11 +30,12 @@ module NewRelic
30
30
  end
31
31
 
32
32
  def finish(name, id, payload) #THREAD_LOCAL_ACCESS
33
- event = pop_event(id)
34
- event.payload.merge!(payload)
33
+ finishable = pop_segment(id)
35
34
 
36
- if state.is_execution_traced? && !event.ignored?
37
- stop_transaction(event)
35
+ if state.is_execution_traced? \
36
+ && !should_ignore(payload, controller_class(payload))
37
+
38
+ finishable.finish
38
39
  else
39
40
  Agent.instance.pop_trace_execution_flag
40
41
  end
@@ -42,81 +43,52 @@ module NewRelic
42
43
  log_notification_error(e, name, 'finish')
43
44
  end
44
45
 
45
- def start_transaction(event)
46
- event.payload[:finishable] = Tracer.start_transaction_or_segment(
47
- name: event.metric_name,
48
- category: :controller,
49
- options: {
50
- request: event.request,
51
- filtered_params: filter(event.payload[:params]),
52
- apdex_start_time: event.queue_start,
53
- ignore_apdex: event.apdex_ignored?,
54
- ignore_enduser: event.enduser_ignored?
46
+ def start_transaction_or_segment(payload, request, controller_class)
47
+ Tracer.start_transaction_or_segment(
48
+ name: format_metric_name(payload[:action], controller_class),
49
+ category: :controller,
50
+ options: {
51
+ request: request,
52
+ filtered_params: NewRelic::Agent::ParameterFiltering.filter_using_rails(
53
+ payload[:params],
54
+ Rails.application.config.filter_parameters
55
+ ),
56
+ apdex_start_time: queue_start(request),
57
+ ignore_apdex: NewRelic::Agent::Instrumentation::IgnoreActions.is_filtered?(
58
+ ControllerInstrumentation::NR_IGNORE_APDEX_KEY,
59
+ controller_class,
60
+ payload[:action]
61
+ ),
62
+ ignore_enduser: NewRelic::Agent::Instrumentation::IgnoreActions.is_filtered?(
63
+ ControllerInstrumentation::NR_IGNORE_ENDUSER_KEY,
64
+ controller_class,
65
+ payload[:action]
66
+ )
55
67
  }
56
68
  )
57
69
  end
58
70
 
59
- def stop_transaction(event)
60
- (finishable = event.payload[:finishable]) && finishable.finish
61
- end
62
-
63
- def filter(params)
64
- munged_params = NewRelic::Agent::ParameterFiltering.filter_rails_request_parameters(params)
65
- filters = Rails.application.config.filter_parameters
66
- ActionDispatch::Http::ParameterFilter.new(filters).filter(munged_params)
67
- end
68
- end
69
-
70
- class ControllerEvent < Event
71
- attr_accessor :parent
72
- attr_reader :queue_start, :request
73
-
74
- def initialize(name, start, ending, transaction_id, payload, request)
75
- # We have a different initialize parameter list, so be explicit
76
- super(name, start, ending, transaction_id, payload)
77
-
78
- @request = request
79
- @controller_class = payload[:controller].split('::') \
80
- .inject(Object){|m,o| m.const_get(o)}
81
-
82
- if request && request.respond_to?(:env)
83
- @queue_start = QueueTime.parse_frontend_timestamp(request.env, self.time)
84
- end
85
- end
86
-
87
- def metric_name
88
- @metric_name || "Controller/#{metric_path}/#{metric_action}"
89
- end
90
-
91
- def metric_path
92
- @controller_class.controller_path
93
- end
94
-
95
- def metric_action
96
- payload[:action]
97
- end
98
-
99
- def ignored?
100
- _is_filtered?(ControllerInstrumentation::NR_DO_NOT_TRACE_KEY)
101
- end
102
-
103
- def apdex_ignored?
104
- _is_filtered?(ControllerInstrumentation::NR_IGNORE_APDEX_KEY)
71
+ def format_metric_name(metric_action, controller_name)
72
+ controller_class = ::NewRelic::LanguageSupport.constantize(controller_name)
73
+ "Controller/#{controller_class.controller_path}/#{metric_action}"
105
74
  end
106
75
 
107
- def enduser_ignored?
108
- _is_filtered?(ControllerInstrumentation::NR_IGNORE_ENDUSER_KEY)
76
+ def controller_class(payload)
77
+ ::NewRelic::LanguageSupport.constantize(payload[:controller])
109
78
  end
110
79
 
111
- def _is_filtered?(key)
80
+ def should_ignore(payload, controller_class)
112
81
  NewRelic::Agent::Instrumentation::IgnoreActions.is_filtered?(
113
- key,
114
- @controller_class,
115
- metric_action)
82
+ ControllerInstrumentation::NR_DO_NOT_TRACE_KEY,
83
+ controller_class,
84
+ payload[:action]
85
+ )
116
86
  end
117
87
 
118
- def to_s
119
- "#<NewRelic::Agent::Instrumentation::ControllerEvent:#{object_id} name: \"#{name}\" id: #{transaction_id} payload: #{payload}}>"
88
+ def queue_start(request)
89
+ if request && request.respond_to?(:env)
90
+ QueueTime.parse_frontend_timestamp(request.env, Time.now)
91
+ end
120
92
  end
121
93
  end
122
94
  end