Sha256: ec4eadfe3b54ae59f75cf34651816edf4731be47fcf0625c657bd1fd59dda8f7

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require_relative 'config'
require 'prometheus_exporter/server'

module PgqPrometheus
  class Collector < PrometheusExporter::Server::TypeCollector

    def initialize
      @data = []
      @observers = {}

      Config._metrics.each do |name, opts|
        metric_class = Kernel.const_get opts[:metric_class].to_s
        help = opts[:help]
        metric_args = opts[:metric_args]
        @observers[name] = metric_class.new("#{type}_#{name}", help, *metric_args)
      end
    end

    def type
      Config.type
    end

    def metrics
      return [] if @data.length == 0

      metrics = {}

      @data.map do |obj|
        labels = {}
        # labels are passed by PgqPrometheus::Processor
        labels.merge!(obj['metric_labels']) if obj['metric_labels']
        # custom_labels are passed by PrometheusExporter::Client
        labels.merge!(obj['custom_labels']) if obj['custom_labels']

        @observers.each do |name, observer|
          name = name.to_s
          value = obj[name]
          if value
            observer.observe(value, labels)
            metrics[name] = observer
          end
        end
      end

      metrics.values
    end

    def collect(obj)
      @data << obj
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pgq_prometheus-0.2.1 lib/pgq_prometheus/collector.rb
pgq_prometheus-0.2.0 lib/pgq_prometheus/collector.rb