Class: MetricsHelper
- Inherits:
-
Object
- Object
- MetricsHelper
- Defined in:
- lib/fluent/plugin/metrics_helper.rb
Constant Summary collapse
- @@obs_endpoint_exists =
false
Instance Method Summary collapse
- #check_endpoint(url, token, verify_ssl, request_timeout) ⇒ Object
- #execute_rest_call(url, user, password, token, use_token, verify_ssl, request_timeout) ⇒ Object
- #get_additional_metrics ⇒ Object
- #get_metrics ⇒ Object
-
#initialize(logger, metric_prefix, jpd_url, username, apikey, token, common_jpd, verify_ssl, request_timeout) ⇒ MetricsHelper
constructor
A new instance of MetricsHelper.
Constructor Details
#initialize(logger, metric_prefix, jpd_url, username, apikey, token, common_jpd, verify_ssl, request_timeout) ⇒ MetricsHelper
Returns a new instance of MetricsHelper.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/fluent/plugin/metrics_helper.rb', line 9 def initialize(logger, metric_prefix, jpd_url, username, apikey, token, common_jpd, verify_ssl, request_timeout) @logger = logger @metric_prefix = metric_prefix @jpd_url = jpd_url @username = username @apikey = apikey @token = token @common_jpd = common_jpd @verify_ssl = verify_ssl @request_timeout = request_timeout end |
Instance Method Details
#check_endpoint(url, token, verify_ssl, request_timeout) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fluent/plugin/metrics_helper.rb', line 56 def check_endpoint(url, token, verify_ssl, request_timeout) @logger.debug("Checking connectivity to endpoint: #{url} started") # Configure proxy with NO_PROXY support ProxyHelper.configure_rest_client_proxy(url, nil, @logger) request = RestClient::Request.new( method: :get, url: url, headers: { Authorization: "Bearer #{token}"}, verify_ssl: verify_ssl, timeout: request_timeout ) request.execute do |response, request, result| if response.code == 200 @@obs_endpoint_exists = true @logger.info("#{url} exists: #{@@obs_endpoint_exists}. Storing the result for next executions") else @@obs_endpoint_exists = false @logger.info("Cannot verify endpoint. Skipping metrics collection from #{url}. Received response code: #{response.code}, Response body:\n#{response.body}") end end @logger.debug("Checking connectivity to endpoint: #{url} finished") end |
#execute_rest_call(url, user, password, token, use_token, verify_ssl, request_timeout) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fluent/plugin/metrics_helper.rb', line 81 def execute_rest_call(url, user, password, token, use_token, verify_ssl, request_timeout) @logger.debug("Rest call to fetch metrics started") # Configure proxy with NO_PROXY support ProxyHelper.configure_rest_client_proxy(url, nil, @logger) request = if use_token == true @logger.debug("Using token for authentication") RestClient::Request.new( method: :get, url: url, headers: { Authorization: "Bearer #{token}" }, verify_ssl: verify_ssl, timeout: request_timeout ) else @logger.debug("Using apiKey for authentication") RestClient::Request.new( method: :get, url: url, user: user, password: password, verify_ssl: verify_ssl, timeout: request_timeout ) end request.execute do |response, request, result| @logger.debug("Recieved response body: #{response.body} when fetching metrics") case response.code when 200 @logger.info("#{@metric_prefix} metrics were successfully collected from url: #{url}") return response.body else @logger.info("Cannot fetch #{@metric_prefix} metrics from url: #{url}. Received response code: #{response.code}, Response body:\n#{response.body}") raise "Unexpected response code: #{response.code} when calling #{url}" end end end |
#get_additional_metrics ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fluent/plugin/metrics_helper.rb', line 44 def get_additional_metrics @logger.info("Aadditional metrics collection started") if (@metric_prefix == 'jfrog.artifactory' || @common_jpd == false) && !@token.nil? && @token != '' url = "#{@jpd_url}/observability/api/v1/metrics" @logger.info("Collecting additional metrics from: #{url}") check_endpoint(url, @token, @verify_ssl, @request_timeout) if @@obs_endpoint_exists == nil? || !@@obs_endpoint_exists additional_metrics = execute_rest_call(url, @username, nil, @token, true, @verify_ssl, @request_timeout) if @@obs_endpoint_exists end @logger.info("Aadditional metrics collection finished") return additional_metrics end |
#get_metrics ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fluent/plugin/metrics_helper.rb', line 21 def get_metrics @logger.debug("Get metrics started") 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 @logger.info("Executing #{@metric_prefix} metrics collection from: #{url}") metrics = nil if !@token.nil? && @token != '' metrics = execute_rest_call(url, @username, nil, @token, true, @verify_ssl, @request_timeout) elsif !@apikey.nil? && @apikey != '' metrics = execute_rest_call(url, @username, @apikey, nil, false, @verify_ssl, @request_timeout) end @logger.debug("Get metrics finished") return metrics end |