Class: DatadogMetrics

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

Instance Method Summary collapse

Constructor Details

#initialize(apikey, url) ⇒ DatadogMetrics

Returns a new instance of DatadogMetrics.



9
10
11
12
# File 'lib/fluent/plugin/datadog_metrics_sender.rb', line 9

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

Instance Method Details

#add_custom_data(ddtags, record) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fluent/plugin/datadog_metrics_sender.rb', line 14

def add_custom_data(ddtags, record)
  if ddtags.length() > 0
    record_series = record['series']
    data_array = []
    record_series.each do |interim_data|
      interim_data['tags'] ||= []
      interim_data['tags'].push(*ddtags)
      data_array << interim_data
    end
    record['series'] = data_array
    return record
  end
  return record
end

#send_metrics(ddtags, record, http_proxy, verify_ssl, request_timeout, gzip_compression) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fluent/plugin/datadog_metrics_sender.rb', line 29

def send_metrics(ddtags, record, http_proxy, verify_ssl, request_timeout, gzip_compression)
  # Get the current local timestamp
  puts "#{Utility.get_time} Additional tags to be added to metrics are:", ddtags
  metrics_data = add_custom_data(ddtags, record)
  puts "#{Utility.get_time} Sending received metrics data"

  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 = {
    'DD-API-KEY': @apikey, 
    content_type: :json
  }

  payload = metrics_data.to_json
  if gzip_compression
    payload = Utility.compress_payload(metrics_data)
    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 DataDog"
      return response.body
    else
      puts "#{Utility.get_time} Cannot send metrics to DataDog url: %s. Received response code: %d, Response body: %s"  % [@url, response.code, response.body]
    end
  end
end