Class: NewRelic::Agent::NewRelicService
- Inherits:
-
Object
- Object
- NewRelic::Agent::NewRelicService
- Defined in:
- lib/new_relic/agent/new_relic_service.rb
Defined Under Namespace
Modules: Encoders Classes: CollectorError, JsonError, JsonMarshaller, Marshaller, PrubyMarshaller
Constant Summary collapse
- PROTOCOL_VERSION =
Specifies the version of the agent’s communication protocol with the NewRelic hosted site.
12
Instance Attribute Summary collapse
-
#agent_id ⇒ Object
1f147a42: v10 (tag 3.5.3.17) cf0d1ff1: v9 (tag 3.5.0) 14105: v8 (tag 2.10.3) (no v7) 10379: v6 (not tagged) 4078: v5 (tag 2.5.4) 2292: v4 (tag 2.3.6) 1754: v3 (tag 2.3.0) 534: v2 (shows up in 2.1.0, our first tag).
-
#collector ⇒ Object
readonly
Returns the value of attribute collector.
-
#marshaller ⇒ Object
readonly
Returns the value of attribute marshaller.
-
#metric_id_cache ⇒ Object
readonly
Returns the value of attribute metric_id_cache.
-
#request_timeout ⇒ Object
1f147a42: v10 (tag 3.5.3.17) cf0d1ff1: v9 (tag 3.5.0) 14105: v8 (tag 2.10.3) (no v7) 10379: v6 (not tagged) 4078: v5 (tag 2.5.4) 2292: v4 (tag 2.3.6) 1754: v3 (tag 2.3.0) 534: v2 (shows up in 2.1.0, our first tag).
Instance Method Summary collapse
- #agent_command_results(results) ⇒ Object
-
#analytic_event_data(data) ⇒ Object
Send fine-grained analytic data to the collector.
-
#build_metric_data_array(stats_hash) ⇒ Object
The collector wants to recieve metric data in a format that’s different from how we store it internally, so this method handles the translation.
-
#cert_file_path ⇒ Object
The path to the certificate file used to verify the SSL connection if verify_peer is enabled.
-
#compress_request_if_needed(data) ⇒ Object
We do not compress if content is smaller than 64kb.
- #connect(settings = {}) ⇒ Object
-
#create_http_connection ⇒ Object
Return the Net::HTTP with proxy configuration given the NewRelic::Control::Server object.
- #error_data(unsent_errors) ⇒ Object
-
#fill_metric_id_cache(pairs_of_specs_and_ids) ⇒ Object
takes an array of arrays of spec and id, adds it into the metric cache so we can save the collector some work by sending integers instead of strings the next time around.
- #get_agent_commands ⇒ Object
- #get_redirect_host ⇒ Object
-
#http_connection ⇒ Object
Return a Net::HTTP connection object to make a call to the collector.
-
#initialize(license_key = nil, collector = control.server) ⇒ NewRelicService
constructor
A new instance of NewRelicService.
- #metric_data(last_harvest_time, now, unsent_timeslice_data) ⇒ Object
- #profile_data(profile) ⇒ Object
- #reset_metric_id_cache ⇒ Object
-
#session(&block) ⇒ Object
One session with the service’s endpoint.
- #shutdown(time) ⇒ Object
- #sql_trace_data(sql_traces) ⇒ Object
- #transaction_sample_data(traces) ⇒ Object
Constructor Details
#initialize(license_key = nil, collector = control.server) ⇒ NewRelicService
Returns a new instance of NewRelicService.
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 55 56 57 58 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 28 def initialize(license_key=nil, collector=control.server) @license_key = license_key || Agent.config[:license_key] @collector = collector @request_timeout = Agent.config[:timeout] @metric_id_cache = {} @audit_logger = ::NewRelic::Agent::AuditLogger.new(Agent.config) Agent.config.register_callback(:'audit_log.enabled') do |enabled| @audit_logger.enabled = enabled end Agent.config.register_callback(:ssl) do |ssl| if !ssl ::NewRelic::Agent.logger.warn("Agent is configured not to use SSL when communicating with New Relic's servers") else ::NewRelic::Agent.logger.debug("Agent is configured to use SSL") end end Agent.config.register_callback(:marshaller) do |marshaller| begin if marshaller == 'json' require 'json' @marshaller = JsonMarshaller.new else @marshaller = PrubyMarshaller.new end rescue LoadError @marshaller = PrubyMarshaller.new end end end |
Instance Attribute Details
#agent_id ⇒ Object
1f147a42: v10 (tag 3.5.3.17) cf0d1ff1: v9 (tag 3.5.0) 14105: v8 (tag 2.10.3) (no v7) 10379: v6 (not tagged) 4078: v5 (tag 2.5.4) 2292: v4 (tag 2.3.6) 1754: v3 (tag 2.3.0) 534: v2 (shows up in 2.1.0, our first tag)
25 26 27 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 25 def agent_id @agent_id end |
#collector ⇒ Object (readonly)
Returns the value of attribute collector.
26 27 28 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 26 def collector @collector end |
#marshaller ⇒ Object (readonly)
Returns the value of attribute marshaller.
26 27 28 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 26 def marshaller @marshaller end |
#metric_id_cache ⇒ Object (readonly)
Returns the value of attribute metric_id_cache.
26 27 28 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 26 def metric_id_cache @metric_id_cache end |
#request_timeout ⇒ Object
1f147a42: v10 (tag 3.5.3.17) cf0d1ff1: v9 (tag 3.5.0) 14105: v8 (tag 2.10.3) (no v7) 10379: v6 (not tagged) 4078: v5 (tag 2.5.4) 2292: v4 (tag 2.3.6) 1754: v3 (tag 2.3.0) 534: v2 (shows up in 2.1.0, our first tag)
25 26 27 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 25 def request_timeout @request_timeout end |
Instance Method Details
#agent_command_results(results) ⇒ Object
140 141 142 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 140 def agent_command_results(results) invoke_remote(:agent_command_results, @agent_id, results) end |
#analytic_event_data(data) ⇒ Object
Send fine-grained analytic data to the collector.
145 146 147 148 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 145 def analytic_event_data(data) data.collect! {|hash| [hash] } invoke_remote(:analytic_event_data, @agent_id, data) end |
#build_metric_data_array(stats_hash) ⇒ Object
The collector wants to recieve metric data in a format that’s different from how we store it internally, so this method handles the translation. It also handles translating metric names to IDs using our metric ID cache.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 95 def build_metric_data_array(stats_hash) metric_data_array = [] stats_hash.each do |metric_spec, stats| # Omit empty stats as an optimization unless stats.is_reset? metric_id = metric_id_cache[metric_spec] metric_data = if metric_id NewRelic::MetricData.new(nil, stats, metric_id) else NewRelic::MetricData.new(metric_spec, stats, nil) end metric_data_array << metric_data end end metric_data_array end |
#cert_file_path ⇒ Object
The path to the certificate file used to verify the SSL connection if verify_peer is enabled
222 223 224 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 222 def cert_file_path File.(File.join(control.newrelic_root, 'cert', 'cacert.pem')) end |
#compress_request_if_needed(data) ⇒ Object
We do not compress if content is smaller than 64kb. There are problems with bugs in Ruby in some versions that expose us to a risk of segfaults if we compress aggressively.
153 154 155 156 157 158 159 160 161 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 153 def compress_request_if_needed(data) encoding = 'identity' if data.size > 64 * 1024 data = Encoders::Compressed.encode(data) encoding = 'deflate' end check_post_size(data) [data, encoding] end |
#connect(settings = {}) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 60 def connect(settings={}) if host = get_redirect_host @collector = NewRelic::Control.instance.server_from_host(host) end response = invoke_remote(:connect, settings) @agent_id = response['agent_run_id'] response end |
#create_http_connection ⇒ Object
Return the Net::HTTP with proxy configuration given the NewRelic::Control::Server object.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 194 def create_http_connection proxy_server = control.proxy_server # Proxy returns regular HTTP if @proxy_host is nil (the default) http_class = Net::HTTP::Proxy(proxy_server.name, proxy_server.port, proxy_server.user, proxy_server.password) http = http_class.new((@collector.ip || @collector.name), @collector.port) if Agent.config[:ssl] begin # Jruby 1.6.8 requires a gem for full ssl support and will throw # an error when use_ssl=(true) is called and jruby-openssl isn't # installed http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.ca_file = cert_file_path rescue StandardError, LoadError msg = "Agent is configured to use SSL, but SSL is not available in the environment. " msg << "Either disable SSL in the agent configuration, or install SSL support." raise UnrecoverableAgentException.new(msg) end end ::NewRelic::Agent.logger.debug("Created net/http handle to #{http.address}:#{http.port}") http end |
#error_data(unsent_errors) ⇒ Object
120 121 122 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 120 def error_data(unsent_errors) invoke_remote(:error_data, @agent_id, unsent_errors) end |
#fill_metric_id_cache(pairs_of_specs_and_ids) ⇒ Object
takes an array of arrays of spec and id, adds it into the metric cache so we can save the collector some work by sending integers instead of strings the next time around
84 85 86 87 88 89 90 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 84 def fill_metric_id_cache(pairs_of_specs_and_ids) Array(pairs_of_specs_and_ids).each do |metric_spec_hash, metric_id| metric_spec = MetricSpec.new(metric_spec_hash['name'], metric_spec_hash['scope']) metric_id_cache[metric_spec] = metric_id end end |
#get_agent_commands ⇒ Object
136 137 138 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 136 def get_agent_commands invoke_remote(:get_agent_commands, @agent_id) end |
#get_redirect_host ⇒ Object
69 70 71 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 69 def get_redirect_host invoke_remote(:get_redirect_host) end |
#http_connection ⇒ Object
Return a Net::HTTP connection object to make a call to the collector. We’ll reuse the same handle for cases where we’re using keep-alive, or otherwise create a new one.
189 190 191 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 189 def http_connection @shared_tcp_connection || create_http_connection end |
#metric_data(last_harvest_time, now, unsent_timeslice_data) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 112 def metric_data(last_harvest_time, now, unsent_timeslice_data) metric_data_array = build_metric_data_array(unsent_timeslice_data) result = invoke_remote(:metric_data, @agent_id, last_harvest_time, now, metric_data_array) fill_metric_id_cache(result) result end |
#profile_data(profile) ⇒ Object
132 133 134 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 132 def profile_data(profile) invoke_remote(:profile_data, @agent_id, profile) || '' end |
#reset_metric_id_cache ⇒ Object
77 78 79 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 77 def reset_metric_id_cache @metric_id_cache = {} end |
#session(&block) ⇒ Object
One session with the service’s endpoint. In this case the session represents 1 tcp connection which may transmit multiple HTTP requests via keep-alive.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 166 def session(&block) raise ArgumentError, "#{self.class}#shared_connection must be passed a block" unless block_given? http = create_http_connection # Immediately open a TCP connection to the server and leave it open for # multiple requests. ::NewRelic::Agent.logger.debug("Opening TCP connection to #{http.address}:#{http.port}") http.start begin @shared_tcp_connection = http block.call ensure @shared_tcp_connection = nil # Close the TCP socket ::NewRelic::Agent.logger.debug("Closing TCP connection to #{http.address}:#{http.port}") http.finish end end |
#shutdown(time) ⇒ Object
73 74 75 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 73 def shutdown(time) invoke_remote(:shutdown, @agent_id, time.to_i) if @agent_id end |
#sql_trace_data(sql_traces) ⇒ Object
128 129 130 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 128 def sql_trace_data(sql_traces) invoke_remote(:sql_trace_data, sql_traces) end |
#transaction_sample_data(traces) ⇒ Object
124 125 126 |
# File 'lib/new_relic/agent/new_relic_service.rb', line 124 def transaction_sample_data(traces) invoke_remote(:transaction_sample_data, @agent_id, traces) end |