Class: Karafka::Web::Tracking::Consumers::Sampler::Metrics::Jobs

Inherits:
Base
  • Object
show all
Includes:
Core::Helpers::Time
Defined in:
lib/karafka/web/tracking/consumers/sampler/metrics/jobs.rb

Overview

Collects job queue statistics and worker utilization metrics

Instance Method Summary collapse

Constructor Details

#initialize(windows, started_at) ⇒ Jobs

Returns a new instance of Jobs.

Parameters:

  • windows (Helpers::Ttls::Windows)

    time windows for aggregating metrics

  • started_at (Float)

    process start time



15
16
17
18
19
# File 'lib/karafka/web/tracking/consumers/sampler/metrics/jobs.rb', line 15

def initialize(windows, started_at)
  super()
  @windows = windows
  @started_at = started_at
end

Instance Method Details

#jobs_queue_statisticsHash

Returns job queue statistics.

Returns:

  • (Hash)

    job queue statistics



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/karafka/web/tracking/consumers/sampler/metrics/jobs.rb', line 42

def jobs_queue_statistics
  # We return empty stats in case jobs queue is not yet initialized
  base = Karafka::Server.jobs_queue&.statistics || { busy: 0, enqueued: 0 }
  stats = base.slice(:busy, :enqueued, :waiting)
  stats[:waiting] ||= 0
  # busy - represents number of jobs that are being executed currently
  # enqueued - jobs that are in the queue but not being picked up yet
  # waiting - jobs that are not scheduled on the queue but will be
  # be enqueued in case of advanced schedulers
  stats
end

#utilizationNumeric

Returns % utilization of all the threads. 100% means all the threads are utilized all the time within the given time window. 0% means, nothing is happening most if not all the time.

Returns:

  • (Numeric)

    % utilization of all the threads. 100% means all the threads are utilized all the time within the given time window. 0% means, nothing is happening most if not all the time.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/karafka/web/tracking/consumers/sampler/metrics/jobs.rb', line 24

def utilization
  totals = windows.m1[:processed_total_time]

  return 0 if totals.empty?

  timefactor = [float_now - started_at, 60].min

  workers_count = workers

  # If downscaled completely (edge case) we return 0 because no way to compute
  return 0 if workers_count.zero?

  # We divide by 1_000 to convert from milliseconds
  # We multiply by 100 to have it in % scale
  (totals.sum / 1_000 / workers_count / timefactor * 100).round(2)
end