Class: MetricsHelper

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

Constant Summary collapse

@@obs_endpoint_exists =
false

Instance Method Summary collapse

Constructor Details

#initialize(metric_prefix, jpd_url, username, apikey, token, common_jpd, verify_ssl) ⇒ MetricsHelper

Returns a new instance of MetricsHelper.



8
9
10
11
12
13
14
15
16
# File 'lib/fluent/plugin/metrics_helper.rb', line 8

def initialize(metric_prefix, jpd_url, username, apikey, token, common_jpd, verify_ssl)
  @metric_prefix = metric_prefix
  @jpd_url = jpd_url
  @username = username
  @apikey = apikey
  @token = token
  @common_jpd = common_jpd
  @verify_ssl = verify_ssl
end

Instance Method Details

#check_endpoint(url, token, verify_ssl) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin/metrics_helper.rb', line 47

def check_endpoint(url, token, verify_ssl)
  response = RestClient::Request.new(
    method: :get,
    url: url,
    headers: { Authorization: "Bearer #{token}"},
    verify_ssl: verify_ssl
  ).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, use_token, verify_ssl) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fluent/plugin/metrics_helper.rb', line 59

def execute_rest_call(url, user, password, token, use_token, verify_ssl)
  request = if use_token == true
              RestClient::Request.new(
                method: :get,
                url: url,
                headers: { Authorization: "Bearer #{token}" },
                verify_ssl: verify_ssl
              )
            else
              RestClient::Request.new(
                method: :get,
                url: url,
                user: user,
                password: password,
                verify_ssl: verify_ssl
              )
            end

  request.execute do |response, request, result|
    case response.code
    when 200
      puts "#{@metric_prefix} metrics were successfully collected from url: #{url}"
      return response.body
    else
      puts "Cannot fetch #{@metric_prefix} metrics from url: #{url}. Received response code: #{response.code}, Response body:\n#{response.body}"
      raise Fluent::ConfigError, 'Cannot fetch #{@metric_prefix} metrics'
    end
  end
end

#get_additional_metricsObject



38
39
40
41
42
43
44
45
# File 'lib/fluent/plugin/metrics_helper.rb', line 38

def get_additional_metrics
  if (@metric_prefix == 'jfrog.artifactory' || @common_jpd == false) && !@token.nil? && @token != ''
    url = "#{@jpd_url}/observability/api/v1/metrics"
    puts "Executing additional metrics collection from: #{url}"
    check_endpoint(url, @token, @verify_ssl) if @@obs_endpoint_exists == nil? || !@@obs_endpoint_exists
    execute_rest_call(url, @username, nil, @token, true, @verify_ssl) if @@obs_endpoint_exists
  end
end

#get_metricsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fluent/plugin/metrics_helper.rb', line 18

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 "Executing #{@metric_prefix} metrics collection from: #{url}"
  if !@token.nil? && @token != ''
    execute_rest_call(url, @username, nil, @token, true, @verify_ssl)
  elsif !@apikey.nil? && @apikey != ''
    execute_rest_call(url, @username, @apikey, nil, false, @verify_ssl)
  end

end