FEATURE: support reset! on all metric types

FEATURE: support reset! on all metric types

This method allows you to clear internal storage of a metric without needing to throw away the object.

diff --git a/CHANGELOG b/CHANGELOG
index a3af6f6..527760c 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+0.4.16 - 04-11-2019
+
+- FEATURE: Support #reset! on all metric types to reset a metric to default
+
 0.4.15 - 04-11-2019
 
 - FEATURE: Improve delayed job collector, add pending counts
diff --git a/lib/prometheus_exporter/metric/base.rb b/lib/prometheus_exporter/metric/base.rb
index 88f8c23..ea712d2 100644
--- a/lib/prometheus_exporter/metric/base.rb
+++ b/lib/prometheus_exporter/metric/base.rb
@@ -34,6 +34,10 @@ module PrometheusExporter::Metric
       raise "Not implemented"
     end
 
+    def reset!
+      raise "Not implemented"
+    end
+
     def from_json(json)
       json = JSON.parse(json) if String === json
       @name = json["name"]
diff --git a/lib/prometheus_exporter/metric/counter.rb b/lib/prometheus_exporter/metric/counter.rb
index 24bbcd4..cdb3896 100644
--- a/lib/prometheus_exporter/metric/counter.rb
+++ b/lib/prometheus_exporter/metric/counter.rb
@@ -6,13 +6,17 @@ module PrometheusExporter::Metric
 
     def initialize(name, help)
       super
-      @data = {}
+      reset!
     end
 
     def type
       "counter"
     end
 
+    def reset!
+      @data = {}
+    end
+
     def metric_text
       @data.map do |labels, value|
         "#{prefix(@name)}#{labels_text(labels)} #{value}"
diff --git a/lib/prometheus_exporter/metric/gauge.rb b/lib/prometheus_exporter/metric/gauge.rb
index ec55778..72d5903 100644
--- a/lib/prometheus_exporter/metric/gauge.rb
+++ b/lib/prometheus_exporter/metric/gauge.rb
@@ -6,7 +6,7 @@ module PrometheusExporter::Metric
 
     def initialize(name, help)
       super
-      @data = {}
+      reset!
     end
 
     def type
@@ -19,6 +19,10 @@ module PrometheusExporter::Metric
       end.join("\n")
     end
 
+    def reset!
+      @data = {}
+    end
+
     def observe(value, labels = {})
       if value.nil?
         data.delete(labels)
diff --git a/lib/prometheus_exporter/metric/histogram.rb b/lib/prometheus_exporter/metric/histogram.rb
index c997bbe..8cf7dde 100644
--- a/lib/prometheus_exporter/metric/histogram.rb
+++ b/lib/prometheus_exporter/metric/histogram.rb
@@ -8,6 +8,10 @@ module PrometheusExporter::Metric
     def initialize(name, help, opts = {})
       super(name, help)
       @buckets = (opts[:buckets] || DEFAULT_BUCKETS).sort.reverse
+      reset!
+    end
+
+    def reset!
       @sums = {}
       @counts = {}
       @observations = {}
diff --git a/lib/prometheus_exporter/metric/summary.rb b/lib/prometheus_exporter/metric/summary.rb
index a550432..66c5239 100644
--- a/lib/prometheus_exporter/metric/summary.rb
+++ b/lib/prometheus_exporter/metric/summary.rb
@@ -10,12 +10,16 @@ module PrometheusExporter::Metric
 
     def initialize(name, help, opts = {})
       super(name, help)
+      reset!
+      @quantiles = opts[:quantiles] || DEFAULT_QUANTILES
+    end
+
+    def reset!
       @buffers = [{}, {}]
       @last_rotated = Process.clock_gettime(Process::CLOCK_MONOTONIC)
       @current_buffer = 0
       @counts = {}
       @sums = {}
-      @quantiles = opts[:quantiles] || DEFAULT_QUANTILES
     end
 
     def type
diff --git a/lib/prometheus_exporter/version.rb b/lib/prometheus_exporter/version.rb
index 9675ba0..db26a5e 100644
--- a/lib/prometheus_exporter/version.rb
+++ b/lib/prometheus_exporter/version.rb
@@ -1,5 +1,5 @@
 # frozen_string_literal: true
 
 module PrometheusExporter
-  VERSION = "0.4.15"
+  VERSION = "0.4.16"
 end
diff --git a/test/metric/base_test.rb b/test/metric/base_test.rb
index bafa98a..f7c20d8 100644
--- a/test/metric/base_test.rb
+++ b/test/metric/base_test.rb
@@ -9,6 +9,11 @@ module PrometheusExporter::Metric
       Counter.new("a_counter", "my amazing counter")
     end
 
+    before  do
+      Base.default_prefix = ''
+      Base.default_labels = {}
+    end
+
     after do
       Base.default_prefix = ''
       Base.default_labels = {}
@@ -42,5 +47,69 @@ module PrometheusExporter::Metric
 
       assert_equal(counter.to_prometheus_text, text)
     end
+
+    it "supports reset! for Gauge" do
+
+      gauge = Gauge.new("test", "test")
+
+      gauge.observe(999)
+      gauge.observe(100, a: "a")
+      gauge.reset!
+
+      text = <<~TEXT
+        # HELP test test
+        # TYPE test gauge
+      TEXT
+
+      assert_equal(gauge.to_prometheus_text.strip, text.strip)
+    end
+
+    it "supports reset! for Counter" do
+
+      counter = Counter.new("test", "test")
+
+      counter.observe(999)
+      counter.observe(100, a: "a")
+      counter.reset!
+
+      text = <<~TEXT
+        # HELP test test
+        # TYPE test counter
+      TEXT
+
+      assert_equal(counter.to_prometheus_text.strip, text.strip)
+    end
+
+    it "supports reset! for Histogram" do
+
+      histogram = Histogram.new("test", "test")
+
+      histogram.observe(999)
+      histogram.observe(100, a: "a")
+      histogram.reset!
+
+      text = <<~TEXT
+        # HELP test test
+        # TYPE test histogram
+      TEXT
+
+      assert_equal(histogram.to_prometheus_text.strip, text.strip)
+    end
+
+    it "supports reset! for Summary" do
+
+      summary = Summary.new("test", "test")
+
+      summary.observe(999)
+      summary.observe(100, a: "a")
+      summary.reset!
+
+      text = <<~TEXT
+        # HELP test test
+        # TYPE test summary
+      TEXT
+
+      assert_equal(summary.to_prometheus_text.strip, text.strip)
+    end
   end
 end

GitHub sha: b8fc6838

1 Like