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



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
33
34
35
36
37
38
39
# File 'lib/prometheus_exporter/instrumentation/periodic_stats.rb', line 6

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

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

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

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

  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)


41
42
43
# File 'lib/prometheus_exporter/instrumentation/periodic_stats.rb', line 41

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

.stopObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/prometheus_exporter/instrumentation/periodic_stats.rb', line 49

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



45
46
47
# File 'lib/prometheus_exporter/instrumentation/periodic_stats.rb', line 45

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