Class: Gitlab::Triage::APIObservability::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/triage/api_observability.rb

Constant Summary collapse

PAGINATION_PARAMS =
%w[page per_page].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.



13
14
15
16
17
18
# File 'lib/gitlab/triage/api_observability.rb', line 13

def initialize
  @calls = Hash.new(0)
  @resources_fetched = 0
  @rate_limit_waits = 0
  @start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



11
12
13
# File 'lib/gitlab/triage/api_observability.rb', line 11

def calls
  @calls
end

#resources_fetchedObject (readonly)

Returns the value of attribute resources_fetched.



11
12
13
# File 'lib/gitlab/triage/api_observability.rb', line 11

def resources_fetched
  @resources_fetched
end

Instance Method Details

#normalize_endpoint(url) ⇒ Object (private)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gitlab/triage/api_observability.rb', line 55

def normalize_endpoint(url)
  uri = URI.parse(url)
  path = uri.path
            .sub(%r{/api/v\d+}, '')
            .sub(%r{/projects/[^/]+}, '/projects/:id')
            .sub(%r{/groups/[^/]+}, '/groups/:id')
            .gsub(%r{/\d+(/|\z)}, '/:id\1')

  query_keys = (URI.decode_www_form(uri.query || '').map(&:first) - PAGINATION_PARAMS).sort

  path += "?#{query_keys.join('&')}" if query_keys.any?
  path
rescue URI::InvalidURIError
  url
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/triage/api_observability.rb', line 33

def print_summary
  elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start_time

  puts "\n"
  puts '=' * 70
  puts '  API Call Summary'
  puts '=' * 70
  puts "  Total HTTP requests:  #{total}"
  puts "  Resources fetched:    #{@resources_fetched}"
  puts "  Rate limit waits:     #{@rate_limit_waits}"
  puts "  Elapsed time:         #{elapsed.round(1)}s"
  puts '-' * 70

  @calls.sort_by { |_, count| -count }.each do |endpoint, count|
    puts "  #{count.to_s.rjust(5)}x  #{endpoint}"
  end

  puts '=' * 70
end

#record(method, url, resource_count: 0) ⇒ Object



20
21
22
23
# File 'lib/gitlab/triage/api_observability.rb', line 20

def record(method, url, resource_count: 0)
  @calls["#{method} #{normalize_endpoint(url)}"] += 1
  @resources_fetched += resource_count
end

#record_rate_limit_waitObject



25
26
27
# File 'lib/gitlab/triage/api_observability.rb', line 25

def record_rate_limit_wait
  @rate_limit_waits += 1
end

#totalObject



29
30
31
# File 'lib/gitlab/triage/api_observability.rb', line 29

def total
  @calls.values.sum
end