# # Copyright 2022- MahithaB # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require "fluent/plugin/output" require_relative 'newrelic_metrics_sender' require_relative 'datadog_metrics_sender' module Fluent module Plugin class JfrogSendMetricsOutput < Fluent::Plugin::Output Fluent::Plugin.register_output("jfrog_send_metrics", self) helpers :timer # `config_param` defines a parameter. # You can refer to a parameter like an instance variable e.g. @port. # `:default` means that the parameter is optional. config_param :target_platform, :string, default: '' config_param :apikey, :string, default: '' config_param :url, :string, default: 'https://metric-api.newrelic.com/metric/v1' config_param :ddtags, :array, default: [] config_param :http_proxy, :string, :default => nil config_param :verify_ssl, :bool, default: true config_param :request_timeout, :time, default: 20 config_param :gzip_compression, :bool, default: false # `configure` is called before `start`. # 'conf' is a `Hash` that includes the configuration parameters. # If the configuration is invalid, raise `Fluent::ConfigError`. def configure(conf) super raise Fluent::ConfigError, 'Must define the target_platform to be one of the following (DATADOG, NEWRELIC).' if @target_platform == '' || !(['DATADOG', 'NEWRELIC'].include?(@target_platform)) raise Fluent::ConfigError, 'Must define the apikey to use for authentication.' if @apikey == '' end def process(tag, es) logger = log es.each do |time, record| begin logger.info("Sending metrics to target platform: #{@target_platform} started") if @target_platform == 'NEWRELIC' vendor = NewRelicMetrics.new(@apikey, @url) vendor.send_metrics(record, @http_proxy, @verify_ssl, @request_timeout, @gzip_compression, logger) elsif @target_platform == 'DATADOG' vendor = DatadogMetrics.new(@apikey, @url) vendor.send_metrics(@ddtags, record, @http_proxy, @verify_ssl, @request_timeout, @gzip_compression, logger) end logger.info("Sending metrics to target platform: #{@target_platform} finished") rescue RestClient::Exceptions::OpenTimeout logger.info("#{Utility.get_time} The request timed out while trying to open a connection. The configured request timeout is: #{@request_timeout}") rescue RestClient::Exceptions::ReadTimeout logger.info("#{Utility.get_time} The request timed out while waiting for a response. The configured request timeout is: #{@request_timeout}") rescue RestClient::Exceptions::RequestTimeout logger.info("#{Utility.get_time} The request timed out. The configured request timeout is: #{@request_timeout}") rescue RestClient::ExceptionWithResponse => e logger.info("#{Utility.get_time} HTTP request failed: #{e.response}") rescue Net::HTTPClientException => e logger.info("#{Utility.get_time} An HTTP client error occurred when sending metrics to #{@target_platform}: #{e.message}") rescue StandardError => e logger.info("#{Utility.get_time} An error occurred when sending metrics to #{@target_platform}: #{e.message}") end end end end end end