FEATURE: Add reset method to Counter (#63)

approved
#1

FEATURE: Add reset method to Counter (#63)

  • Add reset method to Counter

  • Fix mismatches in CHANGELOG, bump version v0.4.6

diff --git a/CHANGELOG b/CHANGELOG
index e59f5a0..d8f644a 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,13 +1,18 @@
-0.4.5 - 22-02-2019
+0.4.6 - 02-04-2019
 
+- Feature: Allow resetting a counter
 - Feature: Add sidekiq metrics: restarted, dead jobs counters
 - Fix: Client shutting down before sending metrics to collector
 
-0.4.4 - 14-02-2019
+0.4.5 - 14-02-2019
 
 - Feature: Allow process collector to ship custom labels for all process metrics
 - Fix: Always scope process metrics on hostname in collector
 
+0.4.4 - 13-02-2019
+
+- Feature: add support for local metric collection without using HTTP 
+
 0.4.3 - 11-02-2019
 
 - Feature: Add alias for Gauge #observe called #set, this makes it a bit easier to migrate from prom
diff --git a/lib/prometheus_exporter/metric/counter.rb b/lib/prometheus_exporter/metric/counter.rb
index 0867265..24bbcd4 100644
--- a/lib/prometheus_exporter/metric/counter.rb
+++ b/lib/prometheus_exporter/metric/counter.rb
@@ -33,5 +33,9 @@ module PrometheusExporter::Metric
       @data[labels] ||= 0
       @data[labels] -= value
     end
+
+    def reset(labels = {}, value = 0)
+      @data[labels] = value
+    end
   end
 end
diff --git a/lib/prometheus_exporter/version.rb b/lib/prometheus_exporter/version.rb
index 73efc4e..773a4c4 100644
--- a/lib/prometheus_exporter/version.rb
+++ b/lib/prometheus_exporter/version.rb
@@ -1,3 +1,3 @@
 module PrometheusExporter
-  VERSION = "0.4.5"
+  VERSION = "0.4.6"
 end
diff --git a/test/metric/counter_test.rb b/test/metric/counter_test.rb
index 50283b1..c7226ff 100644
--- a/test/metric/counter_test.rb
+++ b/test/metric/counter_test.rb
@@ -80,5 +80,31 @@ module PrometheusExporter::Metric
 
       assert_equal(counter.to_prometheus_text, text)
     end
+
+    it "can correctly reset to a default value" do
+      counter.observe(5, sam: "ham")
+      counter.reset({ sam: "ham" })
+
+      text = <<~TEXT
+        # HELP a_counter my amazing counter
+        # TYPE a_counter counter
+        a_counter{sam="ham"} 0
+      TEXT
+
+      assert_equal(counter.to_prometheus_text, text)
+    end
+
+    it "can correctly reset to an explicit value" do
+      counter.observe(5, sam: "ham")
+      counter.reset({ sam: "ham" }, 2)
+
+      text = <<~TEXT
+        # HELP a_counter my amazing counter
+        # TYPE a_counter counter
+        a_counter{sam="ham"} 2
+      TEXT
+
+      assert_equal(counter.to_prometheus_text, text)
+    end
   end
 end

GitHub sha: b3967435

Approved #2