Sha256: 903783c5f38ff7bacbb7ad4e708823d71fcd9add309178f082c8a6c79f801b60

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true
require 'json'
require 'rest-client'
require 'stringio'
require_relative 'utility'

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

  def send_metrics(metrics_data, http_proxy, verify_ssl, request_timeout, gzip_compression, logger)
    logger.info("Sending received metrics data")
    metrics_payload = []
    metrics_payload.push(JSON.parse(metrics_data.to_json))

    if http_proxy
      RestClient.proxy = URI.parse(http_proxy)
      logger.info("Using http_proxy param to set proxy for request. Proxy url: #{RestClient.proxy}")
    elsif ENV['HTTP_PROXY']
      RestClient.proxy = ENV['HTTP_PROXY']
      logger.info("Using 'HTTP_PROXY' environment variable to set proxy for request. Proxy url: #{RestClient.proxy}")
    elsif ENV['http_proxy']
      RestClient.proxy = ENV['http_proxy']
      logger.info("Using 'http_proxy' environment variable to set proxy for request. Proxy url: #{RestClient.proxy}")
    elsif ENV['https_proxy']
      RestClient.proxy = ENV['https_proxy']
      logger.info("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, logger)
      headers[:'Content-Encoding'] = 'gzip'
    end

    request = RestClient::Request.new(
        method: :post,
        url: @url,
        payload:  payload,
        headers: headers,
        verify_ssl: verify_ssl,
        timeout: request_timeout
    )

    request.execute do |response, request, result|
      case response.code
      when 202
        logger.info("Metrics were successfully sent to NewRelic")
        return response.body
      else
        logger.info("Cannot send metrics to NewRelic url: %s. Received response code: %d, Response body: %s"  % [@url, response.code, response.body])
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fluent-plugin-jfrog-send-metrics-0.1.11 lib/fluent/plugin/newrelic_metrics_sender.rb
fluent-plugin-jfrog-send-metrics-0.1.10.3 lib/fluent/plugin/newrelic_metrics_sender.rb
fluent-plugin-jfrog-send-metrics-0.1.10.2 lib/fluent/plugin/newrelic_metrics_sender.rb
fluent-plugin-jfrog-send-metrics-0.1.10.1 lib/fluent/plugin/newrelic_metrics_sender.rb