FEATURE: add increment and decrement methods to Counter

approved

#1

FEATURE: add increment and decrement methods to Counter

Previously Gauge had increment and decrement, for parity and migration ease add it to Counter as well

diff --git a/lib/prometheus_exporter/metric/counter.rb b/lib/prometheus_exporter/metric/counter.rb
index cd03861..0867265 100644
--- a/lib/prometheus_exporter/metric/counter.rb
+++ b/lib/prometheus_exporter/metric/counter.rb
@@ -23,5 +23,15 @@ module PrometheusExporter::Metric
       @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
   end
 end
diff --git a/lib/prometheus_exporter/version.rb b/lib/prometheus_exporter/version.rb
index 934c627..79d8c87 100644
--- a/lib/prometheus_exporter/version.rb
+++ b/lib/prometheus_exporter/version.rb
@@ -1,3 +1,3 @@
 module PrometheusExporter
-  VERSION = "0.4.2"
+  VERSION = "0.4.3"
 end
diff --git a/test/metric/counter_test.rb b/test/metric/counter_test.rb
index 713f2fc..50283b1 100644
--- a/test/metric/counter_test.rb
+++ b/test/metric/counter_test.rb
@@ -40,6 +40,32 @@ module PrometheusExporter::Metric
       assert_equal(counter.to_prometheus_text, text)
     end
 
+    it "can correctly increment" do
+      counter.observe(1, sam: "ham")
+      counter.increment({ sam: "ham" }, 2)
+
+      text = <<~TEXT
+        # HELP a_counter my amazing counter
+        # TYPE a_counter counter
+        a_counter{sam="ham"} 3
+      TEXT
+
+      assert_equal(counter.to_prometheus_text, text)
+    end
+
+    it "can correctly decrement" do
+      counter.observe(5, sam: "ham")
+      counter.decrement({ sam: "ham" }, 2)
+
+      text = <<~TEXT
+        # HELP a_counter my amazing counter
+        # TYPE a_counter counter
+        a_counter{sam="ham"} 3
+      TEXT
+
+      assert_equal(counter.to_prometheus_text, text)
+    end
+
     it "can correctly log multiple increments" do
 
       counter.observe

GitHub sha: fd565596


Approved #2