Class: Upfluence::Instrumentation::PeriodicInstrumenter

Inherits:
Object
  • Object
show all
Defined in:
lib/upfluence/instrumentation/periodic_instrumenter.rb

Instance Method Summary collapse

Constructor Details

#initialize(interval: 30, registry: ::Prometheus::Client.registry) ⇒ PeriodicInstrumenter

Returns a new instance of PeriodicInstrumenter.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/upfluence/instrumentation/periodic_instrumenter.rb', line 4

def initialize(interval: 30, registry: ::Prometheus::Client.registry)
  @interval = interval
  @gauges = metrics.reduce({}) do |acc, (metric, values)|
    metric_name = [prefix, metric].compact.join("_").to_sym

    acc.merge(
      metric => registry.get(metric_name) || registry.gauge(
        metric_name,
        docstring: values[:docstring] || '',
        labels:    values[:labels] || []
      )
    )
  end

  @stop_thread = false
end

Instance Method Details

#metricsObject



56
57
58
# File 'lib/upfluence/instrumentation/periodic_instrumenter.rb', line 56

def metrics
  raise "Please implement a subcalss"
end

#prefixObject



48
49
50
# File 'lib/upfluence/instrumentation/periodic_instrumenter.rb', line 48

def prefix
  nil
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/upfluence/instrumentation/periodic_instrumenter.rb', line 21

def start
  @thread ||= Thread.new do
    until @stop_thread
      begin
        values.each do |(metric, vs)|
          vs.each do |v|
            @gauges[metric].set(v[:value], labels: v[:labels] || {})
          end
        end
      rescue => e
        Upfluence.error_logger.notify(e)
      ensure
        sleep @interval
      end
    end
  end
end

#stopObject



39
40
41
42
43
44
45
46
# File 'lib/upfluence/instrumentation/periodic_instrumenter.rb', line 39

def stop
  return unless @thread&.alive?

  @stop_thread = true
  @thread.wakeup
  @thread.join
  @thread = nil
end

#valuesObject



52
53
54
# File 'lib/upfluence/instrumentation/periodic_instrumenter.rb', line 52

def values
  raise "Please implement a subcalss"
end