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.



24
25
26
27
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
# File 'lib/new_relic/agent/new_relic_service.rb', line 24

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)



21
22
23
# File 'lib/new_relic/agent/new_relic_service.rb', line 21

def agent_id
  @agent_id
end

#collectorObject (readonly)

Returns the value of attribute collector.



22
23
24
# File 'lib/new_relic/agent/new_relic_service.rb', line 22

def collector
  @collector
end

#marshallerObject (readonly)

Returns the value of attribute marshaller.



22
23
24
# File 'lib/new_relic/agent/new_relic_service.rb', line 22

def marshaller
  @marshaller
end

#metric_id_cacheObject (readonly)

Returns the value of attribute metric_id_cache.



22
23
24
# File 'lib/new_relic/agent/new_relic_service.rb', line 22

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)



21
22
23
# File 'lib/new_relic/agent/new_relic_service.rb', line 21

def request_timeout
  @request_timeout
end

Instance Method Details

#agent_command_results(command_id, error = nil) ⇒ Object



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

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.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/new_relic/agent/new_relic_service.rb', line 93

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



221
222
223
# File 'lib/new_relic/agent/new_relic_service.rb', line 221

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.



148
149
150
151
152
153
154
155
156
# File 'lib/new_relic/agent/new_relic_service.rb', line 148

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



58
59
60
61
62
63
64
65
# File 'lib/new_relic/agent/new_relic_service.rb', line 58

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.



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

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



118
119
120
# File 'lib/new_relic/agent/new_relic_service.rb', line 118

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



82
83
84
85
86
87
88
# File 'lib/new_relic/agent/new_relic_service.rb', line 82

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



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

def get_agent_commands
  invoke_remote(:get_agent_commands, @agent_id)
end

#get_redirect_hostObject



67
68
69
# File 'lib/new_relic/agent/new_relic_service.rb', line 67

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.



184
185
186
# File 'lib/new_relic/agent/new_relic_service.rb', line 184

def http_connection
  @shared_tcp_connection || create_http_connection
end

#metric_data(last_harvest_time, now, unsent_timeslice_data) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/new_relic/agent/new_relic_service.rb', line 110

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



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

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

#reset_metric_id_cacheObject



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

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)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/new_relic/agent/new_relic_service.rb', line 161

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



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

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

#sql_trace_data(sql_traces) ⇒ Object



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

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

#transaction_sample_data(traces) ⇒ Object



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

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