Class: Karafka::Web::Processing::Consumers::Aggregators::State::Steps::RefreshCurrentStats

Inherits:
Base
  • Object
show all
Defined in:
lib/karafka/web/processing/consumers/aggregators/state/steps/refresh_current_stats.rb

Overview

Refreshes the counters that are computed based on incoming reports and not a total sum.

For this we use active reports we have in memory. It may not be accurate for the first few seconds but it is much more optimal from performance perspective than computing this fetching all data from Kafka for each view.

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Karafka::Web::Processing::Consumers::Aggregators::State::Steps::Base

Instance Method Details

#callObject

Resets and recomputes the context.state[:stats] snapshot from context.active_reports



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/karafka/web/processing/consumers/aggregators/state/steps/refresh_current_stats.rb', line 19

def call
  stats = context.state[:stats]

  stats[:busy] = 0
  stats[:enqueued] = 0
  stats[:waiting] = 0
  stats[:workers] = 0
  stats[:processes] = 0
  stats[:rss] = 0
  stats[:listeners] = { active: 0, standby: 0 }
  stats[:lag_hybrid] = 0
  stats[:bytes_received] = 0
  stats[:bytes_sent] = 0
  utilization = 0

  context
    .active_reports
    .values
    .reject { |report| report[:process][:status] == "stopped" }
    .each do |report|
      report_stats = report[:stats]
      report_process = report[:process]

      lags_hybrid = []

      iterate_partitions(report) do |partition_stats|
        lag_stored = partition_stats[:lag_stored]
        lag = partition_stats[:lag]

        lags_hybrid << (lag_stored.negative? ? lag : lag_stored)
      end

      stats[:busy] += report_stats[:busy]
      stats[:enqueued] += report_stats[:enqueued]
      stats[:waiting] += report_stats[:waiting] || 0
      stats[:workers] += report_process[:workers] || 0
      stats[:bytes_received] += report_process[:bytes_received] || 0
      stats[:bytes_sent] += report_process[:bytes_sent] || 0
      stats[:listeners][:active] += report_process[:listeners][:active]
      stats[:listeners][:standby] += report_process[:listeners][:standby]
      stats[:processes] += 1
      stats[:rss] += report_process[:memory_usage]
      stats[:lag_hybrid] += lags_hybrid.compact.reject(&:negative?).sum
      utilization += report_stats[:utilization]
    end

  stats[:utilization] = stats[:processes].zero? ? 0.0 : utilization / stats[:processes]
end