Sha256: 3770fdd748000aec22e66db8e38b856adcc55298930fc1f086a37831d95076f0

Contents?: true

Size: 1.11 KB

Versions: 13

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Metric
  class Gauge < Base
    attr_reader :data

    def initialize(name, help)
      if name.end_with?("_total")
        raise ArgumentError, "The metric name of gauge must not have _total suffix. Given: #{name}"
      end

      super
      reset!
    end

    def type
      "gauge"
    end

    def metric_text
      @data.map do |labels, value|
        "#{prefix(@name)}#{labels_text(labels)} #{value}"
      end.join("\n")
    end

    def reset!
      @data = {}
    end

    def to_h
      @data.dup
    end

    def remove(labels)
      @data.delete(labels)
    end

    def observe(value, labels = {})
      if value.nil?
        data.delete(labels)
      else
        if !(Numeric === value)
          raise ArgumentError, 'value must be a number'
        end
        @data[labels] = value
      end
    end

    alias_method :set, :observe

    def increment(labels = {}, value = 1)
      @data[labels] ||= 0
      @data[labels] += value
    end

    def decrement(labels = {}, value = 1)
      @data[labels] ||= 0
      @data[labels] -= value
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
prometheus_exporter-2.1.1 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.1.0 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.8 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.7 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.6 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.5 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.4 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.3 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.2 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.1 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-2.0.0 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-1.0.1 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-1.0.0 lib/prometheus_exporter/metric/gauge.rb