Class: Fluent::Plugin::JfrogMetricsInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_jfrog_metrics.rb

Defined Under Namespace

Classes: TaskObserver

Instance Method Summary collapse

Constructor Details

#initializeJfrogMetricsInput

Returns a new instance of JfrogMetricsInput.



68
69
70
# File 'lib/fluent/plugin/in_jfrog_metrics.rb', line 68

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object

‘configure` is called before `start`. ’conf’ is a ‘Hash` that includes the configuration parameters. If the configuration is invalid, raise `Fluent::ConfigError`.

Raises:

  • (Fluent::ConfigError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fluent/plugin/in_jfrog_metrics.rb', line 51

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 or token for authentication.' if @token == '' &&  @apikey == ''

  raise Fluent::ConfigError, 'Must define the metric_prefix to use for getting the metrics.' if @metric_prefix == ''

  raise Fluent::ConfigError, 'Must define the target_platform to use for getting the metrics.' if @target_platform == ''

  raise Fluent::ConfigError, 'Must define the target_platform to be fone of the following (DATADOG, NEWRELIC, SPLUNK).' if !(['DATADOG', 'NEWRELIC', 'SPLUNK'].include?(@target_platform))
end

#do_executeObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fluent/plugin/in_jfrog_metrics.rb', line 105

def do_execute
  begin
    $logger.info("Metrics collection started")
    metrics_helper = MetricsHelper.new($logger, @metric_prefix, @jpd_url, @username, @apikey, @token, @common_jpd, @verify_ssl, @request_timeout)
    platform_metrics = metrics_helper.get_metrics

    if platform_metrics.nil?
      raise "Error while fetching platform metrics. Metrics response was null"
    end

    additional_metrics = metrics_helper.get_additional_metrics
    if !additional_metrics.nil? && additional_metrics != ''
      platform_metrics += additional_metrics.to_s
    end
    $logger.info("Metrics collection finished")

    if @target_platform == 'SPLUNK'
      parser = SplunkMetricsParser.new(@metric_prefix, router, @tag)
    elsif @target_platform == 'NEWRELIC'
      parser = NewRelicMetricsParser.new(@metric_prefix, router, @tag)
    elsif @target_platform == 'DATADOG'
      parser = DatadogMetricsParser.new(@metric_prefix, router, @tag)
    else
      raise 'Parser Type is not valid. target_platform Should be SPLUNK or NEWRELIC or DATADOG'
    end
    $logger.debug("Emitting collected metrics started")
    parser.emit_parsed_metrics(platform_metrics)
    $logger.debug("Emitting collected metrics finished")

  rescue RestClient::Exceptions::OpenTimeout
    $logger.error("The request timed out while trying to open a connection. The configured request timeout is: #{@request_timeout}")
  rescue RestClient::Exceptions::ReadTimeout
    $logger.error("The request timed out while waiting for a response. The configured request timeout is: #{@request_timeout}")
  rescue RestClient::ExceptionWithResponse => e
    $logger.error("HTTP request failed: #{e.response}")
  rescue StandardError => e
    $logger.error("An unexpected error occurred during metrics collection: #{e.message}")
  else
    $logger.debug("Metrics collection and emission do_execute finished with no errors")
  end
end

#runObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fluent/plugin/in_jfrog_metrics.rb', line 88

def run
  $logger.info("Preparing metrics collection, creating timer task")
  timer_task = Concurrent::TimerTask.new(execution_interval: @execution_interval, run_now: true) do
    begin
      $logger.info("Timer task execution started")
      do_execute
      next "Timer task execution finished successfully"
    rescue => e
      $logger.error("Error occurred when running Timer task: #{e.message}")
      raise e
    end
  end
  timer_task.add_observer(TaskObserver.new)
  timer_task.execute
  sleep 100
end

#shutdownObject



82
83
84
85
86
# File 'lib/fluent/plugin/in_jfrog_metrics.rb', line 82

def shutdown
  @running = false
  @thread.join
  super
end

#startObject

‘start` is called when starting and after `configure` is successfully completed.



73
74
75
76
77
78
79
80
# File 'lib/fluent/plugin/in_jfrog_metrics.rb', line 73

def start
  super
  @running = true
  $logger = log
  @thread = Thread.new do
    run
  end
end