Class: PrometheusExporter::Server::UnicornCollector

Inherits:
TypeCollector
  • Object
show all
Defined in:
lib/prometheus_exporter/server/unicorn_collector.rb

Overview

custom type collector for prometheus_exporter for handling the metrics sent from PrometheusExporter::Instrumentation::Unicorn

Constant Summary collapse

MAX_UNICORN_METRIC_AGE =
60
UNICORN_GAUGES =
{
  workers_total: 'Number of unicorn workers.',
  active_workers_total: 'Number of active unicorn workers',
  request_backlog_total: 'Number of requests waiting to be processed by a unicorn worker.'
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeUnicornCollector

Returns a new instance of UnicornCollector.



14
15
16
# File 'lib/prometheus_exporter/server/unicorn_collector.rb', line 14

def initialize
  @unicorn_metrics = []
end

Instance Method Details

#collect(obj) ⇒ Object



42
43
44
45
46
47
# File 'lib/prometheus_exporter/server/unicorn_collector.rb', line 42

def collect(obj)
  now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  obj["created_at"] = now
  @unicorn_metrics.delete_if { |m| m['created_at'] + MAX_UNICORN_METRIC_AGE < now }
  @unicorn_metrics << obj
end

#metricsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/prometheus_exporter/server/unicorn_collector.rb', line 22

def metrics
  return [] if @unicorn_metrics.length.zero?

  metrics = {}

  @unicorn_metrics.map do |m|
    labels = m["custom_labels"] || {}

    UNICORN_GAUGES.map do |k, help|
      k = k.to_s
      if (v = m[k])
        g = metrics[k] ||= PrometheusExporter::Metric::Gauge.new("unicorn_#{k}", help)
        g.observe(v, labels)
      end
    end
  end

  metrics.values
end

#typeObject



18
19
20
# File 'lib/prometheus_exporter/server/unicorn_collector.rb', line 18

def type
  'unicorn'
end