Class: NewRelic::Agent::NewRelicService

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_idObject

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

#collectorObject (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

#marshallerObject (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_cacheObject (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_timeoutObject

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.



149
150
151
152
# File 'lib/new_relic/agent/new_relic_service.rb', line 149

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_pathObject

The path to the certificate file used to verify the SSL connection if verify_peer is enabled



231
232
233
# File 'lib/new_relic/agent/new_relic_service.rb', line 231

def cert_file_path
  File.expand_path(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.



157
158
159
160
161
162
163
164
165
# File 'lib/new_relic/agent/new_relic_service.rb', line 157

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_connectionObject

Return the Net::HTTP with proxy configuration given the NewRelic::Control::Server object.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/new_relic/agent/new_relic_service.rb', line 203

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_commandsObject



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_hostObject



69
70
71
# File 'lib/new_relic/agent/new_relic_service.rb', line 69

def get_redirect_host
  invoke_remote(:get_redirect_host)
end

#get_xray_metadata(xray_ids) ⇒ Object



144
145
146
# File 'lib/new_relic/agent/new_relic_service.rb', line 144

def (xray_ids)
  invoke_remote(:get_xray_metadata, @agent_id, *xray_ids)
end

#http_connectionObject

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.



198
199
200
# File 'lib/new_relic/agent/new_relic_service.rb', line 198

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_cacheObject



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.

Raises:

  • (ArgumentError)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/new_relic/agent/new_relic_service.rb', line 170

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.
  begin
    t0 = Time.now
    ::NewRelic::Agent.logger.debug("Opening TCP connection to #{http.address}:#{http.port}")
    NewRelic::TimerLib.timeout(@request_timeout) { http.start }
    @shared_tcp_connection = http
    block.call
  rescue Timeout::Error
    elapsed = Time.now - t0
    ::NewRelic::Agent.logger.warn "Timed out opening connection to #{http.address}:#{http.port} after #{elapsed} seconds. If this problem persists, please see http://status.newrelic.com"
    raise
  ensure
    @shared_tcp_connection = nil
    # Close the TCP socket
    ::NewRelic::Agent.logger.debug("Closing TCP connection to #{http.address}:#{http.port}")
    http.finish if http.started?
  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