# frozen_string_literal: true # # Copyright 2021- 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 'concurrent' require 'rest-client' require 'fluent/plugin/input' require_relative 'metrics_parser' require_relative 'metrics_helper' module Fluent module Plugin class JfrogMetricsInput < Fluent::Plugin::Input Fluent::Plugin.register_input('jfrog_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 :tag, :string, default: '' config_param :jpd_url, :string, default: '' config_param :username, :string, default: '' config_param :apikey, :string, default: '' config_param :interval, :time, default: 10 config_param :metric_prefix, :string, default: '' # `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 tag for metrics data.' if @tag == '' raise Fluent::ConfigError, 'Must define the jpd_url to scrape metrics' if @jpd_url == '' raise Fluent::ConfigError, 'Must define the username for authentication' if @username == '' raise Fluent::ConfigError, 'Must define the apikey to use for authentication.' if @apikey == '' raise Fluent::ConfigError, 'Must define the interval to use for gathering the metrics.' if @interval == '' raise Fluent::ConfigError, 'Must define the metric_prefix to use for getting the metrics.' if @metric_prefix == '' end # `start` is called when starting and after `configure` is successfully completed. def start super @running = true @thread = Thread.new(&method(:run)) end def shutdown @running = false @thread.join super end def run puts('Preparing metrics collection, creating timer task') timer_task = Concurrent::TimerTask.new(execution_interval: @interval, timeout_interval: 30, run_now: true) do puts('Timer task execution') do_execute end timer_task.execute end def do_execute puts('Executing metrics collection') metrics_helper = MetricsHelper.new(@metric_prefix, @jpd_url, @username, @apikey) platform_metrics = metrics_helper.get_metrics parser = MetricsParser.new(@metric_prefix, router, @tag) parser.emit_parsed_metrics(platform_metrics) end end end end