Class: NewRelicMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/newrelic_metrics_sender.rb

Instance Method Summary collapse

Constructor Details

#initialize(apikey, url) ⇒ NewRelicMetrics

Returns a new instance of NewRelicMetrics.



8
9
10
11
# File 'lib/fluent/plugin/newrelic_metrics_sender.rb', line 8

def initialize(apikey, url)
  @apikey = apikey
  @url = url
end

Instance Method Details

#send_metrics(metrics_data, http_proxy, verify_ssl, request_timeout, gzip_compression) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
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
57
# File 'lib/fluent/plugin/newrelic_metrics_sender.rb', line 13

def send_metrics(metrics_data, http_proxy, verify_ssl, request_timeout, gzip_compression)
  puts "#{Utility.get_time} Sending received metrics data"
  metrics_payload = []
  metrics_payload.push(JSON.parse(metrics_data.to_json))

  if http_proxy
    RestClient.proxy = URI.parse(http_proxy)
    puts "#{Utility.get_time} Using http_proxy param to set proxy for request. Proxy url: #{RestClient.proxy}"
  elsif ENV['HTTP_PROXY']
    RestClient.proxy = ENV['HTTP_PROXY']
    puts "#{Utility.get_time} Using 'HTTP_PROXY' environment variable to set proxy for request. Proxy url: #{RestClient.proxy}"
  elsif ENV['http_proxy']
    RestClient.proxy = ENV['http_proxy']
    puts "#{Utility.get_time} Using 'http_proxy' environment variable to set proxy for request. Proxy url: #{RestClient.proxy}"
  elsif ENV['https_proxy']
    RestClient.proxy = ENV['https_proxy']
    puts "#{Utility.get_time} Using 'https_proxy' environment variable to set proxy for request. Proxy url: #{RestClient.proxy}"
  end

  headers = {
    params: {'Api-Key' => @apikey}
  }
  payload = metrics_payload.to_json
  if gzip_compression
    payload = Utility.compress_payload(metrics_payload)
    headers[:'Content-Encoding'] = 'gzip'
  end
  
  response = RestClient::Request.new(
      method: :post,
      url: @url,
      payload:  payload,
      headers: headers,
      verify_ssl: verify_ssl,
      timeout: request_timeout
  ).execute do |response, request, result|
    case response.code
    when 202
      puts "#{Utility.get_time} Metrics were successfully sent to NewRelic"
      return response.body
    else
      puts "#{Utility.get_time} Cannot send metrics to NewRelic url: %s. Received response code: %d, Response body: %s"  % [@url, response.code, response.body]
    end
  end
end