Class: Karafka::Core::Monitoring::StatisticsDecorator

Inherits:
Object
  • Object
show all
Includes:
Helpers::Time
Defined in:
lib/karafka/core/monitoring/statistics_decorator.rb

Overview

Many of the librdkafka statistics are absolute values instead of a gauge. This means, that for example number of messages sent is an absolute growing value instead of being a value of messages sent from the last statistics report. This decorator calculates the diff against previously emited stats, so we get also the diff together with the original values

It adds two extra values to numerics: - KEY_d - delta of the previous value and current - KEY_fd - freeze duration - describes how long the delta remains unchanged (zero) and can be useful for detecting values that “hang” for extended period of time and do not have any change (delta always zero). This value is in ms for the consistency with other time operators we use.

Instance Method Summary collapse

Methods included from Helpers::Time

#float_now, #monotonic_now

Constructor Details

#initializeStatisticsDecorator

Returns a new instance of StatisticsDecorator.



26
27
28
29
30
31
# File 'lib/karafka/core/monitoring/statistics_decorator.rb', line 26

def initialize
  @previous = EMPTY_HASH
  # Operate on ms precision only
  @previous_at = monotonic_now.round
  @current_at = @previous_at
end

Instance Method Details

#call(emited_stats) ⇒ Hash

Note:

We modify the emited statistics, instead of creating new. Since we don’t expose any API to get raw data, users can just assume that the result of this decoration is the proper raw stats that they can use

Returns emited statistics extended with the diff data.

Parameters:

  • emited_stats (Hash)

    original emited statistics

Returns:

  • (Hash)

    emited statistics extended with the diff data



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/karafka/core/monitoring/statistics_decorator.rb', line 38

def call(emited_stats)
  @current_at = monotonic_now.round

  @change_d = @current_at - @previous_at

  diff(
    @previous,
    emited_stats
  )

  @previous = emited_stats
  @previous_at = @current_at

  emited_stats.freeze
end