Class: PrometheusExporter::Instrumentation::PeriodicStats

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

Class Method Summary collapse

Class Method Details

.start(*args, frequency:, client: nil, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.start(*args, frequency:, client: nil, **kwargs)
  client ||= PrometheusExporter::Client.default

  raise ArgumentError.new("Expected frequency to be a number") if !(Numeric === frequency)

  raise ArgumentError.new("Expected frequency to be a positive number") if frequency < 0

  raise ArgumentError.new("Worker loop was not set") if !@worker_loop

  klass = self

  stop

  @stop_thread = false

  @thread =
    Thread.new do
      while !@stop_thread
        begin
          @worker_loop.call
        rescue => e
          client.logger.error("#{klass} Prometheus Exporter Failed To Collect Stats #{e}")
        ensure
          sleep frequency
        end
      end
    end
end

.started?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/prometheus_exporter/instrumentation/periodic_stats.rb', line 34

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

.stopObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prometheus_exporter/instrumentation/periodic_stats.rb', line 42

def self.stop
  # to avoid a warning
  @thread = nil if !defined?(@thread)

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

.worker_loop(&blk) ⇒ Object



38
39
40
# File 'lib/prometheus_exporter/instrumentation/periodic_stats.rb', line 38

def self.worker_loop(&blk)
  @worker_loop = blk
end