Sha256: d693145acccf6c4784777396fe06b0c4726ab525b8aff856960a26c4f0736849

Contents?: true

Size: 844 Bytes

Versions: 4

Compression:

Stored size: 844 Bytes

Contents

# frozen_string_literal: true

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

    def initialize(name, help)
      super
      reset!
    end

    def type
      "counter"
    end

    def reset!
      @data = {}
    end

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

    def to_h
      @data.dup
    end

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

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

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

    def reset(labels = {}, value = 0)
      @data[labels] = value
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
prometheus_exporter-0.5.2 lib/prometheus_exporter/metric/counter.rb
prometheus_exporter-0.5.1 lib/prometheus_exporter/metric/counter.rb
prometheus_exporter-0.5.0 lib/prometheus_exporter/metric/counter.rb
prometheus_exporter-0.4.17 lib/prometheus_exporter/metric/counter.rb