Sha256: 5387fa3450f8c4ad5fd0c95322254a727b4e59b9b80dec237daa02fe2f429f6c

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Server
  class GoodJobCollector < TypeCollector
    MAX_METRIC_AGE = 30
    GOOD_JOB_GAUGES = {
      scheduled: "Total number of scheduled GoodJob jobs.",
      retried: "Total number of retried GoodJob jobs.",
      queued: "Total number of queued GoodJob jobs.",
      running: "Total number of running GoodJob jobs.",
      finished: "Total number of finished GoodJob jobs.",
      succeeded: "Total number of succeeded GoodJob jobs.",
      discarded: "Total number of discarded GoodJob jobs."
    }

    def initialize
      @good_job_metrics = MetricsContainer.new(ttl: MAX_METRIC_AGE)
      @gauges = {}
    end

    def type
      "good_job"
    end

    def metrics
      return [] if good_job_metrics.length == 0

      good_job_metrics.map do |metric|
        labels = metric.fetch("custom_labels", {})

        GOOD_JOB_GAUGES.map do |name, help|
          value = metric[name.to_s]

          if value
            gauge = gauges[name] ||= PrometheusExporter::Metric::Gauge.new("good_job_#{name}", help)
            gauge.observe(value, labels)
          end
        end
      end

      gauges.values
    end

    def collect(object)
      @good_job_metrics << object
    end

    private

    attr_reader :good_job_metrics, :gauges
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prometheus_exporter-2.1.1 lib/prometheus_exporter/server/good_job_collector.rb
prometheus_exporter-2.1.0 lib/prometheus_exporter/server/good_job_collector.rb