Sha256: 16276bde8ab4b7fd969348c35413a7d6bfcec5f96cb732fcd90ffed3d6cf3e8c

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true
require 'json'
require 'rest-client'

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

  def send_metrics(metrics_data, http_proxy)
    puts "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 "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}"
    elsif ENV['http_proxy']
      RestClient.proxy = ENV['http_proxy']
      puts "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}"
    end

    begin
      response = RestClient::Request.new(
          method: :post,
          url: @url,
          payload:  metrics_payload.to_json,
          headers: {params: {'Api-Key' => @apikey}}
      ).execute do |response, request, result|
        case response.code
        when 202
          puts 'Metrics were successfully sent to NewRelic'
          return response.body
        else
          puts "Cannot send metrics to NewRelic. Received response code: %d"  % [response&.code]
        end
      end
    rescue Net::HTTPClientException => e
      # Handle the HTTP client exception
      puts "An HTTP client error occurred when sending metrics to NewRelic: #{e.message}"
    rescue StandardError => e
      # Handle any other exceptions
      puts "An error occurred when sending metrics to NewRelic: #{e.message}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-jfrog-send-metrics-0.1.5 lib/fluent/plugin/newrelic_metrics_sender.rb