Sha256: 2142d677a97189f37ef4d5bbe07457f3c511c1a7276cf06085b5f18c5acbb0b6

Contents?: true

Size: 989 Bytes

Versions: 5

Compression:

Stored size: 989 Bytes

Contents

# frozen_string_literal: true

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

    def initialize(name, help)
      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

5 entries across 5 versions & 1 rubygems

Version Path
prometheus_exporter-0.8.1 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-0.8.0 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-0.7.0 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-0.6.0 lib/prometheus_exporter/metric/gauge.rb
prometheus_exporter-0.5.3 lib/prometheus_exporter/metric/gauge.rb