lib/fluent/plugin/datadog_metrics_sender.rb in fluent-plugin-jfrog-send-metrics-0.1.7 vs lib/fluent/plugin/datadog_metrics_sender.rb in fluent-plugin-jfrog-send-metrics-0.1.8
- old
+ new
@@ -1,9 +1,11 @@
# frozen_string_literal: true
require 'json'
require 'rest-client'
require 'uri'
+require 'stringio'
+require_relative 'utility'
class DatadogMetrics
def initialize(apikey, url)
@apikey = apikey
@url = url
@@ -22,50 +24,56 @@
return record
end
return record
end
- def send_metrics(ddtags, record, http_proxy, verify_ssl)
- puts "Additional tags to be added to metrics are:", ddtags
+ 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 "Sending received metrics data"
+ puts "#{Utility.get_time} Sending received metrics data"
if http_proxy
RestClient.proxy = URI.parse(http_proxy)
- puts "Using http_proxy param to set proxy for request. Proxy url: #{RestClient.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 "Using 'HTTP_PROXY' environment variable to set proxy for request. Proxy url: #{RestClient.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 "Using 'http_proxy' environment variable to set proxy for request. Proxy url: #{RestClient.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 "Using 'https_proxy' environment variable to set proxy for request. Proxy url: #{RestClient.proxy}"
+ puts "#{Utility.get_time} Using 'https_proxy' environment variable to set proxy for request. Proxy url: #{RestClient.proxy}"
end
- begin
+ 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: metrics_data.to_json,
- headers: {'DD-API-KEY': @apikey, content_type: :json },
- verify_ssl: verify_ssl
+ payload: payload,
+ headers: headers,
+ verify_ssl: verify_ssl,
+ timeout: request_timeout
).execute do |response, request, result|
- case response.code
- when 202
- puts 'Metrics were successfully sent to DataDog'
- return response.body
- else
- puts "Cannot send metrics to DataDog url: %s. Received response code: %d, Response body: %s" % [@url, response.code, response.body]
- end
+ 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
- rescue Net::HTTPClientException => e
- # Handle the HTTP client exception
- puts "An HTTP client error occurred when sending metrics to DataDog: #{e.message}"
- rescue StandardError => e
- # Handle any other exceptions
- puts "An error occurred when sending metrics to DataDog: #{e.message}"
end
end
+
end