newrelic_rpm 8.13.1 → 8.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.rubocop.yml +130 -1
- data/.rubocop_todo.yml +3 -0
- data/CHANGELOG.md +3284 -3148
- data/CONTRIBUTING.md +1 -2
- data/README.md +2 -1
- data/init.rb +1 -1
- data/lib/new_relic/agent/agent.rb +14 -466
- data/lib/new_relic/agent/agent_helpers/connect.rb +227 -0
- data/lib/new_relic/agent/agent_helpers/harvest.rb +153 -0
- data/lib/new_relic/agent/agent_helpers/shutdown.rb +72 -0
- data/lib/new_relic/agent/agent_helpers/special_startup.rb +75 -0
- data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +167 -0
- data/lib/new_relic/agent/agent_helpers/startup.rb +202 -0
- data/lib/new_relic/agent/agent_helpers/transmit.rb +76 -0
- data/lib/new_relic/agent/configuration/default_source.rb +47 -9
- data/lib/new_relic/agent/datastores.rb +2 -2
- data/lib/new_relic/agent/event_loop.rb +1 -1
- data/lib/new_relic/agent/guid_generator.rb +11 -2
- data/lib/new_relic/agent/heap.rb +1 -1
- data/lib/new_relic/agent/instrumentation/active_job.rb +7 -7
- data/lib/new_relic/agent/instrumentation/active_merchant.rb +2 -2
- data/lib/new_relic/agent/instrumentation/active_record.rb +9 -9
- data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +24 -24
- data/lib/new_relic/agent/instrumentation/active_storage.rb +2 -2
- data/lib/new_relic/agent/instrumentation/active_support.rb +12 -0
- data/lib/new_relic/agent/instrumentation/active_support_logger.rb +3 -3
- data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +2 -2
- data/lib/new_relic/agent/instrumentation/authlogic.rb +2 -2
- data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +3 -3
- data/lib/new_relic/agent/instrumentation/bunny.rb +4 -4
- data/lib/new_relic/agent/instrumentation/concurrent_ruby/chain.rb +36 -0
- data/lib/new_relic/agent/instrumentation/concurrent_ruby/instrumentation.rb +21 -0
- data/lib/new_relic/agent/instrumentation/concurrent_ruby/prepend.rb +27 -0
- data/lib/new_relic/agent/instrumentation/concurrent_ruby.rb +31 -0
- data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +1 -1
- data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +1 -1
- data/lib/new_relic/agent/instrumentation/curb.rb +6 -6
- data/lib/new_relic/agent/instrumentation/custom_events_subscriber.rb +37 -0
- data/lib/new_relic/agent/instrumentation/data_mapper.rb +50 -50
- data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +5 -5
- data/lib/new_relic/agent/instrumentation/elasticsearch.rb +5 -5
- data/lib/new_relic/agent/instrumentation/excon/middleware.rb +1 -1
- data/lib/new_relic/agent/instrumentation/excon.rb +9 -9
- data/lib/new_relic/agent/instrumentation/grape.rb +8 -8
- data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +1 -1
- data/lib/new_relic/agent/instrumentation/grpc_client.rb +3 -3
- data/lib/new_relic/agent/instrumentation/grpc_server.rb +4 -4
- data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +1 -1
- data/lib/new_relic/agent/instrumentation/httpclient.rb +2 -2
- data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +1 -1
- data/lib/new_relic/agent/instrumentation/httprb.rb +3 -3
- data/lib/new_relic/agent/instrumentation/logger.rb +3 -3
- data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +3 -3
- data/lib/new_relic/agent/instrumentation/memcache.rb +16 -16
- data/lib/new_relic/agent/instrumentation/mongo.rb +1 -1
- data/lib/new_relic/agent/instrumentation/net_http.rb +6 -6
- data/lib/new_relic/agent/instrumentation/padrino.rb +3 -3
- data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +5 -5
- data/lib/new_relic/agent/instrumentation/rack.rb +14 -14
- data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +5 -5
- data/lib/new_relic/agent/instrumentation/rails_middleware.rb +3 -3
- data/lib/new_relic/agent/instrumentation/rails_notifications/action_cable.rb +6 -6
- data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +2 -2
- data/lib/new_relic/agent/instrumentation/rails_notifications/action_view.rb +5 -3
- data/lib/new_relic/agent/instrumentation/rails_notifications/custom_events.rb +30 -0
- data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +4 -4
- data/lib/new_relic/agent/instrumentation/rake.rb +7 -7
- data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +29 -8
- data/lib/new_relic/agent/instrumentation/redis.rb +4 -4
- data/lib/new_relic/agent/instrumentation/resque.rb +9 -9
- data/lib/new_relic/agent/instrumentation/sequel.rb +2 -2
- data/lib/new_relic/agent/instrumentation/sidekiq.rb +3 -3
- data/lib/new_relic/agent/instrumentation/sinatra.rb +7 -7
- data/lib/new_relic/agent/instrumentation/sunspot.rb +4 -4
- data/lib/new_relic/agent/instrumentation/thread/instrumentation.rb +5 -1
- data/lib/new_relic/agent/instrumentation/thread.rb +3 -3
- data/lib/new_relic/agent/instrumentation/tilt.rb +3 -3
- data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +2 -2
- data/lib/new_relic/agent/instrumentation/typhoeus.rb +1 -1
- data/lib/new_relic/agent/javascript_instrumentor.rb +2 -2
- data/lib/new_relic/agent/method_tracer_helpers.rb +6 -11
- data/lib/new_relic/agent/range_extensions.rb +2 -2
- data/lib/new_relic/agent/tracer.rb +8 -4
- data/lib/new_relic/agent/transaction/abstract_segment.rb +2 -2
- data/lib/new_relic/agent/transaction/segment.rb +6 -0
- data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +1 -1
- data/lib/new_relic/agent/transaction.rb +3 -3
- data/lib/new_relic/agent.rb +7 -0
- data/lib/new_relic/control/instance_methods.rb +6 -2
- data/lib/new_relic/helper.rb +1 -1
- data/lib/new_relic/recipes/helpers/send_deployment.rb +2 -1
- data/lib/new_relic/traced_thread.rb +5 -1
- data/lib/new_relic/version.rb +2 -2
- data/lib/newrelic_rpm.rb +13 -2
- data/lib/sequel/extensions/newrelic_instrumentation.rb +1 -1
- data/lib/sequel/plugins/newrelic_instrumentation.rb +1 -1
- data/newrelic.yml +23 -5
- data/test/agent_helper.rb +7 -7
- metadata +16 -6
- data/lib/new_relic/agent/agent/shutdown.rb +0 -35
- data/lib/new_relic/agent/agent/special_startup.rb +0 -72
- data/lib/new_relic/agent/agent/start_worker_thread.rb +0 -163
- data/lib/new_relic/agent/agent/startup.rb +0 -197
@@ -0,0 +1,227 @@
|
|
1
|
+
# This file is distributed under New Relic's license terms.
|
2
|
+
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
module NewRelic
|
6
|
+
module Agent
|
7
|
+
module AgentHelpers
|
8
|
+
# This module is an artifact of a refactoring of the connect
|
9
|
+
# method - all of its methods are used in that context, so it
|
10
|
+
# can be refactored at will. It should be fully tested
|
11
|
+
module Connect
|
12
|
+
# number of attempts we've made to contact the server
|
13
|
+
attr_accessor :connect_attempts
|
14
|
+
|
15
|
+
# Disconnect just sets the connect state to disconnected, preventing
|
16
|
+
# further retries.
|
17
|
+
def disconnect
|
18
|
+
@connect_state = :disconnected
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
def connected?
|
23
|
+
@connect_state == :connected
|
24
|
+
end
|
25
|
+
|
26
|
+
def disconnected?
|
27
|
+
@connect_state == :disconnected
|
28
|
+
end
|
29
|
+
|
30
|
+
# Don't connect if we're already connected, or if we tried to connect
|
31
|
+
# and were rejected with prejudice because of a license issue, unless
|
32
|
+
# we're forced to by force_reconnect.
|
33
|
+
def should_connect?(force = false)
|
34
|
+
force || (!connected? && !disconnected?)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Per the spec at
|
38
|
+
# /agents/agent-specs/Collector-Response-Handling.md, retry
|
39
|
+
# connections after a specific backoff sequence to prevent
|
40
|
+
# hammering the server.
|
41
|
+
def connect_retry_period
|
42
|
+
NewRelic::CONNECT_RETRY_PERIODS[connect_attempts] || NewRelic::MAX_RETRY_PERIOD
|
43
|
+
end
|
44
|
+
|
45
|
+
def note_connect_failure
|
46
|
+
self.connect_attempts += 1
|
47
|
+
end
|
48
|
+
|
49
|
+
# When we have a problem connecting to the server, we need
|
50
|
+
# to tell the user what happened, since this is not an error
|
51
|
+
# we can handle gracefully.
|
52
|
+
def log_error(error)
|
53
|
+
::NewRelic::Agent.logger.error("Error establishing connection with New Relic Service at #{control.server}:",
|
54
|
+
error)
|
55
|
+
end
|
56
|
+
|
57
|
+
# When the server sends us an error with the license key, we
|
58
|
+
# want to tell the user that something went wrong, and let
|
59
|
+
# them know where to go to get a valid license key
|
60
|
+
#
|
61
|
+
# After this runs, it disconnects the agent so that it will
|
62
|
+
# no longer try to connect to the server, saving the
|
63
|
+
# application and the server load
|
64
|
+
def handle_license_error(error)
|
65
|
+
::NewRelic::Agent.logger.error( \
|
66
|
+
error.message, \
|
67
|
+
"Visit NewRelic.com to obtain a valid license key, or to upgrade your account."
|
68
|
+
)
|
69
|
+
disconnect
|
70
|
+
end
|
71
|
+
|
72
|
+
def handle_unrecoverable_agent_error(error)
|
73
|
+
::NewRelic::Agent.logger.error(error.message)
|
74
|
+
disconnect
|
75
|
+
shutdown
|
76
|
+
end
|
77
|
+
|
78
|
+
# Checks whether we should send environment info, and if so,
|
79
|
+
# returns the snapshot from the local environment.
|
80
|
+
# Generating the EnvironmentReport has the potential to trigger
|
81
|
+
# require calls in Rails environments, so this method should only
|
82
|
+
# be called synchronously from on the main thread.
|
83
|
+
def environment_for_connect
|
84
|
+
@environment_report ||= Agent.config[:send_environment_info] ? Array(EnvironmentReport.new) : []
|
85
|
+
end
|
86
|
+
|
87
|
+
# Constructs and memoizes an event_harvest_config hash to be used in
|
88
|
+
# the payload sent during connect (and reconnect)
|
89
|
+
def event_harvest_config
|
90
|
+
@event_harvest_config ||= Configuration::EventHarvestConfig.from_config(Agent.config)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Builds the payload to send to the connect service,
|
94
|
+
# connects, then configures the agent using the response from
|
95
|
+
# the connect service
|
96
|
+
def connect_to_server
|
97
|
+
request_builder = ::NewRelic::Agent::Connect::RequestBuilder.new( \
|
98
|
+
@service,
|
99
|
+
Agent.config,
|
100
|
+
event_harvest_config,
|
101
|
+
environment_for_connect
|
102
|
+
)
|
103
|
+
connect_response = @service.connect(request_builder.connect_payload)
|
104
|
+
|
105
|
+
response_handler = ::NewRelic::Agent::Connect::ResponseHandler.new(self, Agent.config)
|
106
|
+
response_handler.configure_agent(connect_response)
|
107
|
+
|
108
|
+
log_connection(connect_response) if connect_response
|
109
|
+
connect_response
|
110
|
+
end
|
111
|
+
|
112
|
+
# Logs when we connect to the server, for debugging purposes
|
113
|
+
# - makes sure we know if an agent has not connected
|
114
|
+
def log_connection(config_data)
|
115
|
+
::NewRelic::Agent.logger.debug("Connected to NewRelic Service at #{@service.collector.name}")
|
116
|
+
::NewRelic::Agent.logger.debug("Agent Run = #{@service.agent_id}.")
|
117
|
+
::NewRelic::Agent.logger.debug("Connection data = #{config_data.inspect}")
|
118
|
+
if config_data['messages'] && config_data['messages'].any?
|
119
|
+
log_collector_messages(config_data['messages'])
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def log_collector_messages(messages)
|
124
|
+
messages.each do |message|
|
125
|
+
::NewRelic::Agent.logger.send(message['level'].downcase, message['message'])
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# apdex_f is always 4 times the apdex_t
|
130
|
+
def apdex_f
|
131
|
+
(Agent.config[:apdex_t] * 4).to_f
|
132
|
+
end
|
133
|
+
|
134
|
+
class WaitOnConnectTimeout < StandardError
|
135
|
+
end
|
136
|
+
|
137
|
+
# Used for testing to let us know we've actually started to wait
|
138
|
+
def waited_on_connect?
|
139
|
+
@waited_on_connect
|
140
|
+
end
|
141
|
+
|
142
|
+
def signal_connected
|
143
|
+
@wait_on_connect_mutex.synchronize do
|
144
|
+
@wait_on_connect_condition.signal
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def wait_on_connect(timeout)
|
149
|
+
return if connected?
|
150
|
+
|
151
|
+
@waited_on_connect = true
|
152
|
+
NewRelic::Agent.logger.debug("Waiting on connect to complete.")
|
153
|
+
|
154
|
+
@wait_on_connect_mutex.synchronize do
|
155
|
+
@wait_on_connect_condition.wait(@wait_on_connect_mutex, timeout)
|
156
|
+
end
|
157
|
+
|
158
|
+
unless connected?
|
159
|
+
raise WaitOnConnectTimeout, "Agent was unable to connect in #{timeout} seconds."
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def connect_options(options)
|
164
|
+
{
|
165
|
+
keep_retrying: Agent.config[:keep_retrying],
|
166
|
+
force_reconnect: Agent.config[:force_reconnect]
|
167
|
+
}.merge(options)
|
168
|
+
end
|
169
|
+
|
170
|
+
# Establish a connection to New Relic servers.
|
171
|
+
#
|
172
|
+
# By default, if a connection has already been established, this method
|
173
|
+
# will be a no-op.
|
174
|
+
#
|
175
|
+
# @param [Hash] options
|
176
|
+
# @option options [Boolean] :keep_retrying (true)
|
177
|
+
# If true, this method will block until a connection is successfully
|
178
|
+
# established, continuing to retry upon failure. If false, this method
|
179
|
+
# will return after either successfully connecting, or after failing
|
180
|
+
# once.
|
181
|
+
#
|
182
|
+
# @option options [Boolean] :force_reconnect (false)
|
183
|
+
# If true, this method will force establishment of a new connection
|
184
|
+
# with New Relic, even if there is already an existing connection.
|
185
|
+
# This is useful primarily when re-establishing a new connection after
|
186
|
+
# forking off from a parent process.
|
187
|
+
#
|
188
|
+
def connect(options = {})
|
189
|
+
opts = connect_options(options)
|
190
|
+
return unless should_connect?(opts[:force_reconnect])
|
191
|
+
|
192
|
+
::NewRelic::Agent.logger.debug("Connecting Process to New Relic: #$0")
|
193
|
+
connect_to_server
|
194
|
+
@connected_pid = $$
|
195
|
+
@connect_state = :connected
|
196
|
+
signal_connected
|
197
|
+
rescue NewRelic::Agent::ForceDisconnectException => e
|
198
|
+
handle_force_disconnect(e)
|
199
|
+
rescue NewRelic::Agent::LicenseException => e
|
200
|
+
handle_license_error(e)
|
201
|
+
rescue NewRelic::Agent::UnrecoverableAgentException => e
|
202
|
+
handle_unrecoverable_agent_error(e)
|
203
|
+
rescue StandardError, Timeout::Error, NewRelic::Agent::ServerConnectionException => e
|
204
|
+
retry if retry_from_error?(e, opts)
|
205
|
+
rescue Exception => e
|
206
|
+
::NewRelic::Agent.logger.error("Exception of unexpected type during Agent#connect():", e)
|
207
|
+
|
208
|
+
raise
|
209
|
+
end
|
210
|
+
|
211
|
+
def retry_from_error?(e, opts)
|
212
|
+
# Allow a killed (aborting) thread to continue exiting during shutdown.
|
213
|
+
# See: https://github.com/newrelic/newrelic-ruby-agent/issues/340
|
214
|
+
raise if Thread.current.status == 'aborting'
|
215
|
+
|
216
|
+
log_error(e)
|
217
|
+
return false unless opts[:keep_retrying]
|
218
|
+
|
219
|
+
note_connect_failure
|
220
|
+
::NewRelic::Agent.logger.info("Will re-attempt in #{connect_retry_period} seconds")
|
221
|
+
sleep(connect_retry_period)
|
222
|
+
true
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
# This file is distributed under New Relic's license terms.
|
2
|
+
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
module NewRelic
|
6
|
+
module Agent
|
7
|
+
module AgentHelpers
|
8
|
+
module Harvest
|
9
|
+
# Harvests data from the given container, sends it to the named endpoint
|
10
|
+
# on the service, and automatically merges back in upon a recoverable
|
11
|
+
# failure.
|
12
|
+
#
|
13
|
+
# The given container should respond to:
|
14
|
+
#
|
15
|
+
# #harvest!
|
16
|
+
# returns a payload that contains enumerable collection of data items and
|
17
|
+
# optional metadata to be sent to the collector.
|
18
|
+
#
|
19
|
+
# #reset!
|
20
|
+
# drop any stored data and reset to a clean state.
|
21
|
+
#
|
22
|
+
# #merge!(payload)
|
23
|
+
# merge the given payload back into the internal buffer of the
|
24
|
+
# container, so that it may be harvested again later.
|
25
|
+
#
|
26
|
+
def harvest_and_send_from_container(container, endpoint)
|
27
|
+
payload = harvest_from_container(container, endpoint)
|
28
|
+
sample_count = harvest_size(container, payload)
|
29
|
+
if sample_count > 0
|
30
|
+
NewRelic::Agent.logger.debug("Sending #{sample_count} items to #{endpoint}")
|
31
|
+
send_data_to_endpoint(endpoint, payload, container)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def harvest_size(container, items)
|
36
|
+
if container.respond_to?(:has_metadata?) && container.has_metadata? && !items.empty?
|
37
|
+
items.last.size
|
38
|
+
else
|
39
|
+
items.size
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def harvest_from_container(container, endpoint)
|
44
|
+
items = []
|
45
|
+
begin
|
46
|
+
items = container.harvest!
|
47
|
+
rescue => e
|
48
|
+
NewRelic::Agent.logger.error("Failed to harvest #{endpoint} data, resetting. Error: ", e)
|
49
|
+
container.reset!
|
50
|
+
end
|
51
|
+
items
|
52
|
+
end
|
53
|
+
|
54
|
+
def harvest_and_send_timeslice_data
|
55
|
+
TransactionTimeAggregator.harvest!
|
56
|
+
harvest_and_send_from_container(@stats_engine, :metric_data)
|
57
|
+
end
|
58
|
+
|
59
|
+
def harvest_and_send_slowest_sql
|
60
|
+
harvest_and_send_from_container(@sql_sampler, :sql_trace_data)
|
61
|
+
end
|
62
|
+
|
63
|
+
# This handles getting the transaction traces and then sending
|
64
|
+
# them across the wire. This includes gathering SQL
|
65
|
+
# explanations, stripping out stack traces, and normalizing
|
66
|
+
# SQL. note that we explain only the sql statements whose
|
67
|
+
# nodes' execution times exceed our threshold (to avoid
|
68
|
+
# unnecessary overhead of running explains on fast queries.)
|
69
|
+
def harvest_and_send_transaction_traces
|
70
|
+
harvest_and_send_from_container(@transaction_sampler, :transaction_sample_data)
|
71
|
+
end
|
72
|
+
|
73
|
+
def harvest_and_send_for_agent_commands
|
74
|
+
harvest_and_send_from_container(@agent_command_router, :profile_data)
|
75
|
+
end
|
76
|
+
|
77
|
+
def harvest_and_send_errors
|
78
|
+
harvest_and_send_from_container(@error_collector.error_trace_aggregator, :error_data)
|
79
|
+
end
|
80
|
+
|
81
|
+
def harvest_and_send_analytic_event_data
|
82
|
+
harvest_and_send_from_container(transaction_event_aggregator, :analytic_event_data)
|
83
|
+
harvest_and_send_from_container(synthetics_event_aggregator, :analytic_event_data)
|
84
|
+
end
|
85
|
+
|
86
|
+
def harvest_and_send_custom_event_data
|
87
|
+
harvest_and_send_from_container(@custom_event_aggregator, :custom_event_data)
|
88
|
+
end
|
89
|
+
|
90
|
+
def harvest_and_send_error_event_data
|
91
|
+
harvest_and_send_from_container(@error_collector.error_event_aggregator, :error_event_data)
|
92
|
+
end
|
93
|
+
|
94
|
+
def harvest_and_send_span_event_data
|
95
|
+
harvest_and_send_from_container(span_event_aggregator, :span_event_data)
|
96
|
+
end
|
97
|
+
|
98
|
+
def harvest_and_send_log_event_data
|
99
|
+
harvest_and_send_from_container(@log_event_aggregator, :log_event_data)
|
100
|
+
end
|
101
|
+
|
102
|
+
def harvest_and_send_data_types
|
103
|
+
harvest_and_send_errors
|
104
|
+
harvest_and_send_error_event_data
|
105
|
+
harvest_and_send_transaction_traces
|
106
|
+
harvest_and_send_slowest_sql
|
107
|
+
harvest_and_send_timeslice_data
|
108
|
+
harvest_and_send_span_event_data
|
109
|
+
harvest_and_send_log_event_data
|
110
|
+
end
|
111
|
+
|
112
|
+
def send_data_to_endpoint(endpoint, payload, container)
|
113
|
+
begin
|
114
|
+
@service.send(endpoint, payload)
|
115
|
+
rescue ForceRestartException, ForceDisconnectException
|
116
|
+
raise
|
117
|
+
rescue SerializationError => e
|
118
|
+
NewRelic::Agent.logger.warn("Failed to serialize data for #{endpoint}, discarding. Error: ", e)
|
119
|
+
rescue UnrecoverableServerException => e
|
120
|
+
NewRelic::Agent.logger.warn("#{endpoint} data was rejected by remote service, discarding. Error: ", e)
|
121
|
+
rescue ServerConnectionException => e
|
122
|
+
log_remote_unavailable(endpoint, e)
|
123
|
+
container.merge!(payload)
|
124
|
+
rescue => e
|
125
|
+
NewRelic::Agent.logger.info("Unable to send #{endpoint} data, will try again later. Error: ", e)
|
126
|
+
container.merge!(payload)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def check_for_and_handle_agent_commands
|
131
|
+
begin
|
132
|
+
@agent_command_router.check_for_and_handle_agent_commands
|
133
|
+
rescue ForceRestartException, ForceDisconnectException
|
134
|
+
raise
|
135
|
+
rescue UnrecoverableServerException => e
|
136
|
+
NewRelic::Agent.logger.warn("get_agent_commands message was rejected by remote service, discarding. " \
|
137
|
+
"Error: ", e)
|
138
|
+
rescue ServerConnectionException => e
|
139
|
+
log_remote_unavailable(:get_agent_commands, e)
|
140
|
+
rescue => e
|
141
|
+
NewRelic::Agent.logger.info("Error during check_for_and_handle_agent_commands, will retry later: ", e)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def log_remote_unavailable(endpoint, e)
|
146
|
+
NewRelic::Agent.logger.debug("Unable to send #{endpoint} data, will try again later. Error: ", e)
|
147
|
+
NewRelic::Agent.record_metric("Supportability/remote_unavailable", 0.0)
|
148
|
+
NewRelic::Agent.record_metric("Supportability/remote_unavailable/#{endpoint.to_s}", 0.0)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# This file is distributed under New Relic's license terms.
|
2
|
+
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
module NewRelic
|
6
|
+
module Agent
|
7
|
+
module AgentHelpers
|
8
|
+
module Shutdown
|
9
|
+
# Attempt a graceful shutdown of the agent, flushing any remaining
|
10
|
+
# data.
|
11
|
+
def shutdown
|
12
|
+
return unless started?
|
13
|
+
|
14
|
+
::NewRelic::Agent.logger.info("Starting Agent shutdown")
|
15
|
+
|
16
|
+
stop_event_loop
|
17
|
+
trap_signals_for_litespeed
|
18
|
+
untraced_graceful_disconnect
|
19
|
+
revert_to_default_configuration
|
20
|
+
|
21
|
+
@started = nil
|
22
|
+
Control.reset
|
23
|
+
end
|
24
|
+
|
25
|
+
def untraced_graceful_disconnect
|
26
|
+
begin
|
27
|
+
NewRelic::Agent.disable_all_tracing do
|
28
|
+
graceful_disconnect
|
29
|
+
end
|
30
|
+
rescue => e
|
31
|
+
::NewRelic::Agent.logger.error(e)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# This method contacts the server to send remaining data and
|
36
|
+
# let the server know that the agent is shutting down - this
|
37
|
+
# allows us to do things like accurately set the end of the
|
38
|
+
# lifetime of the process
|
39
|
+
#
|
40
|
+
# If this process comes from a parent process, it will not
|
41
|
+
# disconnect, so that the parent process can continue to send data
|
42
|
+
def graceful_disconnect
|
43
|
+
if connected?
|
44
|
+
begin
|
45
|
+
@service.request_timeout = 10
|
46
|
+
|
47
|
+
@events.notify(:before_shutdown)
|
48
|
+
transmit_data_types
|
49
|
+
shutdown_service
|
50
|
+
|
51
|
+
::NewRelic::Agent.logger.debug("Graceful disconnect complete")
|
52
|
+
rescue Timeout::Error, StandardError => e
|
53
|
+
::NewRelic::Agent.logger.debug("Error when disconnecting #{e.class.name}: #{e.message}")
|
54
|
+
end
|
55
|
+
else
|
56
|
+
::NewRelic::Agent.logger.debug("Bypassing graceful disconnect - agent not connected")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def shutdown_service
|
61
|
+
if @connected_pid == $$ && !@service.kind_of?(NewRelic::Agent::NewRelicService)
|
62
|
+
::NewRelic::Agent.logger.debug("Sending New Relic service agent run shutdown message")
|
63
|
+
@service.shutdown
|
64
|
+
else
|
65
|
+
::NewRelic::Agent.logger.debug("This agent connected from parent process #{@connected_pid}--not sending " \
|
66
|
+
"shutdown")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# This file is distributed under New Relic's license terms.
|
2
|
+
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
module NewRelic
|
6
|
+
module Agent
|
7
|
+
module AgentHelpers
|
8
|
+
module SpecialStartup
|
9
|
+
# If we're using a dispatcher that forks before serving
|
10
|
+
# requests, we need to wait until the children are forked
|
11
|
+
# before connecting, otherwise the parent process sends useless data
|
12
|
+
def using_forking_dispatcher?
|
13
|
+
# TODO: MAJOR VERSION - remove :rainbows
|
14
|
+
if [:puma, :passenger, :rainbows, :unicorn].include?(Agent.config[:dispatcher])
|
15
|
+
::NewRelic::Agent.logger.info("Deferring startup of agent reporting thread because " \
|
16
|
+
"#{Agent.config[:dispatcher]} may fork.")
|
17
|
+
true
|
18
|
+
else
|
19
|
+
false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Return true if we're using resque and it hasn't had a chance to (potentially)
|
24
|
+
# daemonize itself. This avoids hanging when there's a Thread started
|
25
|
+
# before Resque calls Process.daemon (Jira RUBY-857)
|
26
|
+
def defer_for_resque?
|
27
|
+
NewRelic::Agent.config[:dispatcher] == :resque &&
|
28
|
+
NewRelic::Agent::Instrumentation::Resque::Helper.resque_fork_per_job? &&
|
29
|
+
!PipeChannelManager.listener.started?
|
30
|
+
end
|
31
|
+
|
32
|
+
def in_resque_child_process?
|
33
|
+
defined?(@service) && @service.is_a?(PipeService)
|
34
|
+
end
|
35
|
+
|
36
|
+
def defer_for_delayed_job?
|
37
|
+
NewRelic::Agent.config[:dispatcher] == :delayed_job &&
|
38
|
+
!NewRelic::DelayedJobInjection.worker_name
|
39
|
+
end
|
40
|
+
|
41
|
+
# This matters when the following three criteria are met:
|
42
|
+
#
|
43
|
+
# 1. A Sinatra 'classic' application is being run
|
44
|
+
# 2. The app is being run by executing the main file directly, rather
|
45
|
+
# than via a config.ru file.
|
46
|
+
# 3. newrelic_rpm is required *after* sinatra
|
47
|
+
#
|
48
|
+
# In this case, the entire application runs from an at_exit handler in
|
49
|
+
# Sinatra, and if we were to install ours, it would be executed before
|
50
|
+
# the one in Sinatra, meaning that we'd shutdown the agent too early
|
51
|
+
# and never collect any data.
|
52
|
+
def sinatra_classic_app?
|
53
|
+
(
|
54
|
+
defined?(Sinatra::Application) &&
|
55
|
+
Sinatra::Application.respond_to?(:run) &&
|
56
|
+
Sinatra::Application.run?
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
def should_install_exit_handler?
|
61
|
+
return false unless Agent.config[:send_data_on_exit]
|
62
|
+
|
63
|
+
!sinatra_classic_app? || Agent.config[:force_install_exit_handler]
|
64
|
+
end
|
65
|
+
|
66
|
+
def install_exit_handler
|
67
|
+
return unless should_install_exit_handler?
|
68
|
+
|
69
|
+
NewRelic::Agent.logger.debug("Installing at_exit handler")
|
70
|
+
at_exit { shutdown }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# This file is distributed under New Relic's license terms.
|
2
|
+
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
module NewRelic
|
6
|
+
module Agent
|
7
|
+
module AgentHelpers
|
8
|
+
module StartWorkerThread
|
9
|
+
LOG_ONCE_KEYS_RESET_PERIOD = 60.0
|
10
|
+
|
11
|
+
TRANSACTION_EVENT_DATA = "transaction_event_data".freeze
|
12
|
+
CUSTOM_EVENT_DATA = "custom_event_data".freeze
|
13
|
+
ERROR_EVENT_DATA = "error_event_data".freeze
|
14
|
+
SPAN_EVENT_DATA = "span_event_data".freeze
|
15
|
+
LOG_EVENT_DATA = "log_event_data".freeze
|
16
|
+
|
17
|
+
# Try to launch the worker thread and connect to the server.
|
18
|
+
#
|
19
|
+
# See #connect for a description of connection_options.
|
20
|
+
def start_worker_thread(connection_options = {})
|
21
|
+
if disable = NewRelic::Agent.config[:disable_harvest_thread]
|
22
|
+
NewRelic::Agent.logger.info("Not starting Ruby Agent worker thread because :disable_harvest_thread is " \
|
23
|
+
"#{disable}")
|
24
|
+
return
|
25
|
+
end
|
26
|
+
|
27
|
+
::NewRelic::Agent.logger.debug("Creating Ruby Agent worker thread.")
|
28
|
+
@worker_thread = Threading::AgentThread.create('Worker Loop') do
|
29
|
+
deferred_work!(connection_options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_event_loop
|
34
|
+
EventLoop.new
|
35
|
+
end
|
36
|
+
|
37
|
+
# If the @worker_thread encounters an error during the attempt to connect to the collector
|
38
|
+
# then the connect attempts enter an exponential backoff retry loop. To avoid potential
|
39
|
+
# race conditions with shutting down while also attempting to reconnect, we join the
|
40
|
+
# @worker_thread with a timeout threshold. This allows potentially connecting and flushing
|
41
|
+
# pending data to the server, but without waiting indefinitely for a reconnect to succeed.
|
42
|
+
# The use-case where this typically arises is in cronjob scheduled rake tasks where there's
|
43
|
+
# also some network stability/latency issues happening.
|
44
|
+
def stop_event_loop
|
45
|
+
@event_loop.stop if @event_loop
|
46
|
+
# Wait the end of the event loop thread.
|
47
|
+
if @worker_thread
|
48
|
+
unless @worker_thread.join(3)
|
49
|
+
::NewRelic::Agent.logger.debug("Event loop thread did not stop within 3 seconds")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Certain event types may sometimes need to be on the same interval as metrics,
|
55
|
+
# so we will check config assigned in EventHarvestConfig to determine the interval
|
56
|
+
# on which to report them
|
57
|
+
def interval_for(event_type)
|
58
|
+
interval = Agent.config[:"event_report_period.#{event_type}"]
|
59
|
+
:"#{interval}_second_harvest"
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_and_run_event_loop
|
63
|
+
data_harvest = :"#{Agent.config[:data_report_period]}_second_harvest"
|
64
|
+
event_harvest = :"#{Agent.config[:event_report_period]}_second_harvest"
|
65
|
+
|
66
|
+
@event_loop = create_event_loop
|
67
|
+
@event_loop.on(data_harvest) do
|
68
|
+
transmit_data
|
69
|
+
end
|
70
|
+
|
71
|
+
@event_loop.on(interval_for(TRANSACTION_EVENT_DATA)) do
|
72
|
+
transmit_analytic_event_data
|
73
|
+
end
|
74
|
+
@event_loop.on(interval_for(CUSTOM_EVENT_DATA)) do
|
75
|
+
transmit_custom_event_data
|
76
|
+
end
|
77
|
+
@event_loop.on(interval_for(ERROR_EVENT_DATA)) do
|
78
|
+
transmit_error_event_data
|
79
|
+
end
|
80
|
+
@event_loop.on(interval_for(SPAN_EVENT_DATA)) do
|
81
|
+
transmit_span_event_data
|
82
|
+
end
|
83
|
+
@event_loop.on(interval_for(LOG_EVENT_DATA)) do
|
84
|
+
transmit_log_event_data
|
85
|
+
end
|
86
|
+
|
87
|
+
@event_loop.on(:reset_log_once_keys) do
|
88
|
+
::NewRelic::Agent.logger.clear_already_logged
|
89
|
+
end
|
90
|
+
@event_loop.fire_every(Agent.config[:data_report_period], data_harvest)
|
91
|
+
@event_loop.fire_every(Agent.config[:event_report_period], event_harvest)
|
92
|
+
@event_loop.fire_every(LOG_ONCE_KEYS_RESET_PERIOD, :reset_log_once_keys)
|
93
|
+
|
94
|
+
@event_loop.run
|
95
|
+
end
|
96
|
+
|
97
|
+
# Handles the case where the server tells us to restart -
|
98
|
+
# this clears the data, clears connection attempts, and
|
99
|
+
# waits a while to reconnect.
|
100
|
+
def handle_force_restart(error)
|
101
|
+
::NewRelic::Agent.logger.debug(error.message)
|
102
|
+
drop_buffered_data
|
103
|
+
@service.force_restart if @service
|
104
|
+
@connect_state = :pending
|
105
|
+
sleep(30)
|
106
|
+
end
|
107
|
+
|
108
|
+
# when a disconnect is requested, stop the current thread, which
|
109
|
+
# is the worker thread that gathers data and talks to the
|
110
|
+
# server.
|
111
|
+
def handle_force_disconnect(error)
|
112
|
+
::NewRelic::Agent.logger.warn("Agent received a ForceDisconnectException from the server, disconnecting. " \
|
113
|
+
"(#{error.message})")
|
114
|
+
disconnect
|
115
|
+
end
|
116
|
+
|
117
|
+
# Handles an unknown error in the worker thread by logging
|
118
|
+
# it and disconnecting the agent, since we are now in an
|
119
|
+
# unknown state.
|
120
|
+
def handle_other_error(error)
|
121
|
+
::NewRelic::Agent.logger.error("Unhandled error in worker thread, disconnecting.")
|
122
|
+
# These errors are fatal (that is, they will prevent the agent from
|
123
|
+
# reporting entirely), so we really want backtraces when they happen
|
124
|
+
::NewRelic::Agent.logger.log_exception(:error, error)
|
125
|
+
disconnect
|
126
|
+
end
|
127
|
+
|
128
|
+
# a wrapper method to handle all the errors that can happen
|
129
|
+
# in the connection and worker thread system. This
|
130
|
+
# guarantees a no-throw from the background thread.
|
131
|
+
def catch_errors
|
132
|
+
yield
|
133
|
+
rescue NewRelic::Agent::ForceRestartException => e
|
134
|
+
handle_force_restart(e)
|
135
|
+
retry
|
136
|
+
rescue NewRelic::Agent::ForceDisconnectException => e
|
137
|
+
handle_force_disconnect(e)
|
138
|
+
rescue => e
|
139
|
+
handle_other_error(e)
|
140
|
+
end
|
141
|
+
|
142
|
+
# This is the method that is run in a new thread in order to
|
143
|
+
# background the harvesting and sending of data during the
|
144
|
+
# normal operation of the agent.
|
145
|
+
#
|
146
|
+
# Takes connection options that determine how we should
|
147
|
+
# connect to the server, and loops endlessly - typically we
|
148
|
+
# never return from this method unless we're shutting down
|
149
|
+
# the agent
|
150
|
+
def deferred_work!(connection_options)
|
151
|
+
catch_errors do
|
152
|
+
NewRelic::Agent.disable_all_tracing do
|
153
|
+
connect(connection_options)
|
154
|
+
if connected?
|
155
|
+
create_and_run_event_loop
|
156
|
+
# never reaches here unless there is a problem or
|
157
|
+
# the agent is exiting
|
158
|
+
else
|
159
|
+
::NewRelic::Agent.logger.debug("No connection. Worker thread ending.")
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|