Class: PrometheusExporter::Ext::Instrumentation::PeriodicStats

Inherits:
BaseStats
  • Object
show all
Defined in:
lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb

Direct Known Subclasses

ProcCpu

Class Method Summary collapse

Methods inherited from BaseStats

#collect, #initialize, #type

Constructor Details

This class inherits a constructor from PrometheusExporter::Ext::Instrumentation::BaseStats

Class Method Details

.start(frequency: 30, client: PrometheusExporter::Client.default) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb', line 8

def start(frequency: 30, client: PrometheusExporter::Client.default, **)
  raise ArgumentError, 'Expected frequency to be a number' unless frequency.is_a?(Numeric)
  raise ArgumentError, 'Expected frequency to be a positive number' if frequency.negative?

  klass = self
  stop
  instrumentation = new(client:, **)
  @stop_thread = false

  @thread = Thread.new do
    until @stop_thread
      begin
        instrumentation.collect
      rescue StandardError => e
        client.logger.error("#{klass} Prometheus Exporter Failed To Collect Stats")
        client.logger.error("#{e.class} #{e.backtrace&.join("\n")}")
      ensure
        sleep frequency
      end
    end
  end
end

.started?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb', line 31

def started?
  !!@thread&.alive?
end

.stopObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb', line 35

def stop
  # to avoid a warning
  @thread = nil unless defined?(@thread)

  if @thread&.alive?
    @stop_thread = true
    @thread.wakeup
    @thread.join
  end
  @thread = nil
end