class PrometheusExporter::Server::UnicornCollector

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

Constants

MAX_UNICORN_METRIC_AGE
UNICORN_GAUGES

Public Class Methods

new() click to toggle source
# File lib/prometheus_exporter/server/unicorn_collector.rb, line 14
def initialize
  @unicorn_metrics = []
end

Public Instance Methods

collect(obj) click to toggle source
# 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
metrics() click to toggle source
# 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
type() click to toggle source
# File lib/prometheus_exporter/server/unicorn_collector.rb, line 18
def type
  'unicorn'
end