# frozen_string_literal: true require 'rest-client' require_relative 'utility' class MetricsHelper @@obs_endpoint_exists = false def initialize(metric_prefix, jpd_url, username, apikey, token, common_jpd, verify_ssl, request_timeout) @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 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 puts "#{Utility.get_time} Executing #{@metric_prefix} metrics collection from: #{url}" if !@token.nil? && @token != '' execute_rest_call(url, @username, nil, @token, true, @verify_ssl, @request_timeout) elsif !@apikey.nil? && @apikey != '' execute_rest_call(url, @username, @apikey, nil, false, @verify_ssl, @request_timeout) end end def get_additional_metrics puts "#{Utility.get_time} Aadditional metrics collection started" if (@metric_prefix == 'jfrog.artifactory' || @common_jpd == false) && !@token.nil? && @token != '' url = "#{@jpd_url}/observability/api/v1/metrics" puts "#{Utility.get_time} Collecting additional metrics from: #{url}" check_endpoint(url, @token, @verify_ssl, @request_timeout) if @@obs_endpoint_exists == nil? || !@@obs_endpoint_exists execute_rest_call(url, @username, nil, @token, true, @verify_ssl, @request_timeout) if @@obs_endpoint_exists end end def check_endpoint(url, token, verify_ssl, request_timeout) puts "#{Utility.get_time} Checking connectivity to endpoint: #{url}" response = RestClient::Request.new( method: :get, url: url, headers: { Authorization: "Bearer #{token}"}, verify_ssl: verify_ssl, timeout: request_timeout ).execute do |response, request, result| if response.code == 200 @@obs_endpoint_exists = true puts "#{Utility.get_time} #{url} exists: #{@@obs_endpoint_exists}. Storing the result for next executions" else @@obs_endpoint_exists = false puts "#{Utility.get_time} Cannot verify endpoint. Skipping metrics collection from #{url}. Received response code: #{response.code}, Response body:\n#{response.body}" end end end def execute_rest_call(url, user, password, token, use_token, verify_ssl, request_timeout) request = if use_token == true RestClient::Request.new( method: :get, url: url, headers: { Authorization: "Bearer #{token}" }, verify_ssl: verify_ssl, timeout: request_timeout ) else 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| case response.code when 200 puts "#{Utility.get_time} #{@metric_prefix} metrics were successfully collected from url: #{url}" return response.body else puts "#{Utility.get_time} 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 end