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
59
60
# 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")
    elsif !Agent.config[:verify_certificate]
      ::NewRelic::Agent.logger.warn("Agent is configured to use SSL but to skip certificate validation 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(command_id, error = nil) ⇒ Object



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

def agent_command_results(command_id, error=nil)
  results = {}
  results["error"] = error unless error.nil?

  invoke_remote(:agent_command_results, @agent_id, { command_id.to_s => results })
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.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/new_relic/agent/new_relic_service.rb', line 97

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



225
226
227
# File 'lib/new_relic/agent/new_relic_service.rb', line 225

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.



152
153
154
155
156
157
158
159
160
# File 'lib/new_relic/agent/new_relic_service.rb', line 152

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



62
63
64
65
66
67
68
69
# File 'lib/new_relic/agent/new_relic_service.rb', line 62

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.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/new_relic/agent/new_relic_service.rb', line 193

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
      if Agent.config[:verify_certificate]
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        http.ca_file = cert_file_path
      else
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
    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



122
123
124
# File 'lib/new_relic/agent/new_relic_service.rb', line 122

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



86
87
88
89
90
91
92
# File 'lib/new_relic/agent/new_relic_service.rb', line 86

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



138
139
140
# File 'lib/new_relic/agent/new_relic_service.rb', line 138

def get_agent_commands
  invoke_remote(:get_agent_commands, @agent_id)
end

#get_redirect_hostObject



71
72
73
# File 'lib/new_relic/agent/new_relic_service.rb', line 71

def get_redirect_host
  invoke_remote(:get_redirect_host)
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.



188
189
190
# File 'lib/new_relic/agent/new_relic_service.rb', line 188

def http_connection
  @shared_tcp_connection || create_http_connection
end

#metric_data(last_harvest_time, now, unsent_timeslice_data) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/new_relic/agent/new_relic_service.rb', line 114

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



134
135
136
# File 'lib/new_relic/agent/new_relic_service.rb', line 134

def profile_data(profile)
  invoke_remote(:profile_data, @agent_id, profile) || ''
end

#reset_metric_id_cacheObject



79
80
81
# File 'lib/new_relic/agent/new_relic_service.rb', line 79

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)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/new_relic/agent/new_relic_service.rb', line 165

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



75
76
77
# File 'lib/new_relic/agent/new_relic_service.rb', line 75

def shutdown(time)
  invoke_remote(:shutdown, @agent_id, time.to_i) if @agent_id
end

#sql_trace_data(sql_traces) ⇒ Object



130
131
132
# File 'lib/new_relic/agent/new_relic_service.rb', line 130

def sql_trace_data(sql_traces)
  invoke_remote(:sql_trace_data, sql_traces)
end

#transaction_sample_data(traces) ⇒ Object



126
127
128
# File 'lib/new_relic/agent/new_relic_service.rb', line 126

def transaction_sample_data(traces)
  invoke_remote(:transaction_sample_data, @agent_id, traces)
end