class MetricsHelper
Public Class Methods
new(metric_prefix, jpd_url, username, apikey, token, common_jpd)
click to toggle source
# File lib/fluent/plugin/metrics_helper.rb, line 8 def initialize(metric_prefix, jpd_url, username, apikey, token, common_jpd) @metric_prefix = metric_prefix @jpd_url = jpd_url @username = username @apikey = apikey @token = token @common_jpd = common_jpd end
Public Instance Methods
check_endpoint(url, token)
click to toggle source
# File lib/fluent/plugin/metrics_helper.rb, line 44 def check_endpoint(url, token) response = RestClient::Request.new( method: :get, url: url, headers: { Authorization: "Bearer #{token}" } ).execute do |response, request, result| @@obs_endpoint_exists = true if response.code == 200 puts "#{url} exists? -> #{@@obs_endpoint_exists}, storing the result for next executions" end end
execute_rest_call(url, user, password, token, ignore_exception, use_token)
click to toggle source
# File lib/fluent/plugin/metrics_helper.rb, line 55 def execute_rest_call(url, user, password, token, ignore_exception, use_token) request = if use_token == true RestClient::Request.new( method: :get, url: url, headers: { Authorization: "Bearer #{token}" } ) else RestClient::Request.new( method: :get, url: url, user: user, password: password ) end request.execute do |response, request, result| case response.code when 200 return response.body else if ignore_exception == true return '' else raise Fluent::ConfigError, 'Cannot fetch #{@metric_prefix} metrics' end end end end
get_additional_metrics()
click to toggle source
# File lib/fluent/plugin/metrics_helper.rb, line 35 def get_additional_metrics if (@metric_prefix == 'jfrog.artifactory' || @common_jpd == false) && !@token.nil? && @token != '' puts 'Executing additional metrics collection' url = "#{@jpd_url}/observability/api/v1/metrics" check_endpoint(url, @token) if @@obs_endpoint_exists == nil? || !@@obs_endpoint_exists execute_rest_call(url, @username, nil, @token, true, true) if @@obs_endpoint_exists end end
get_metrics()
click to toggle source
# File lib/fluent/plugin/metrics_helper.rb, line 17 def get_metrics url = nil url = case @metric_prefix when 'jfrog.artifactory' "#{@jpd_url}/artifactory/api/v1/metrics" when 'jfrog.xray' "#{@jpd_url}/xray/api/v1/metrics" else "#{@jpd_url}/artifactory/api/v1/metrics" end if !@token.nil? && @token != '' execute_rest_call(url, @username, nil, @token, false, true) elsif !@apikey.nil? && @apikey != '' execute_rest_call(url, @username, @apikey, nil, false, false) end end