FEATURE: Support #to_h on all metric types to fetch label value set for a metric (#98)

FEATURE: Support #to_h on all metric types to fetch label value set for a metric (#98)

diff --git a/lib/prometheus_exporter/metric/base.rb b/lib/prometheus_exporter/metric/base.rb
index ea712d2..c604dd9 100644
--- a/lib/prometheus_exporter/metric/base.rb
+++ b/lib/prometheus_exporter/metric/base.rb
@@ -38,6 +38,10 @@ module PrometheusExporter::Metric
       raise "Not implemented"
     end
 
+    def to_h
+      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 cdb3896..284a729 100644
--- a/lib/prometheus_exporter/metric/counter.rb
+++ b/lib/prometheus_exporter/metric/counter.rb
@@ -23,6 +23,10 @@ module PrometheusExporter::Metric
       end.join("\n")
     end
 
+    def to_h
+      @data.dup
+    end
+
     def observe(increment = 1, labels = {})
       @data[labels] ||= 0
       @data[labels] += increment
diff --git a/lib/prometheus_exporter/metric/gauge.rb b/lib/prometheus_exporter/metric/gauge.rb
index 72d5903..ecc3e4c 100644
--- a/lib/prometheus_exporter/metric/gauge.rb
+++ b/lib/prometheus_exporter/metric/gauge.rb
@@ -23,6 +23,10 @@ module PrometheusExporter::Metric
       @data = {}
     end
 
+    def to_h
+      @data.dup
+    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 8cf7dde..6354537 100644
--- a/lib/prometheus_exporter/metric/histogram.rb
+++ b/lib/prometheus_exporter/metric/histogram.rb
@@ -17,6 +17,16 @@ module PrometheusExporter::Metric
       @observations = {}
     end
 
+    def to_h
+      data = {}
+      @observations.each do |labels, buckets|
+        count = @counts[labels]
+        sum = @sums[labels]
+        data[labels] = { "count" => count, "sum" => sum }
+      end
+      data
+    end
+
     def type
       "histogram"
     end
diff --git a/lib/prometheus_exporter/metric/summary.rb b/lib/prometheus_exporter/metric/summary.rb
index 66c5239..07c0dcf 100644
--- a/lib/prometheus_exporter/metric/summary.rb
+++ b/lib/prometheus_exporter/metric/summary.rb
@@ -22,6 +22,16 @@ module PrometheusExporter::Metric
       @sums = {}
     end
 
+    def to_h
+      data = {}
+      calculate_all_quantiles.each do |labels, quantiles|
+        count = @counts[labels]
+        sum = @sums[labels]
+        data[labels] = { "count" => count, "sum" => sum }
+      end
+      data
+    end
+
     def type
       "summary"
     end
diff --git a/prometheus_exporter.gemspec b/prometheus_exporter.gemspec
index 7d9d5bc..3a31c58 100644
--- a/prometheus_exporter.gemspec
+++ b/prometheus_exporter.gemspec
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
   spec.add_development_dependency "oj", "~> 3.0"
   spec.add_development_dependency "rack-test", "~> 0.8.3"
   spec.add_development_dependency "minitest-stub-const", "~> 0.6"
+  spec.add_development_dependency 'rubocop-discourse', '~> 1.0'
   if !RUBY_ENGINE == 'jruby'
     spec.add_development_dependency "raindrops", "~> 0.19"
   end
diff --git a/test/metric/counter_test.rb b/test/metric/counter_test.rb
index 4151bf9..a2ac042 100644
--- a/test/metric/counter_test.rb
+++ b/test/metric/counter_test.rb
@@ -127,5 +127,12 @@ module PrometheusExporter::Metric
 
       assert_equal(counter.to_prometheus_text, text)
     end
+
+    it "can correctly return data set" do
+      counter.observe(5, sam: "ham")
+      counter.observe(10, foo: "bar")
+
+      assert_equal(counter.to_h, { sam: "ham" } => 5, { foo: "bar" } => 10)
+    end
   end
 end
diff --git a/test/metric/gauge_test.rb b/test/metric/gauge_test.rb
index 4746eb7..f11383f 100644
--- a/test/metric/gauge_test.rb
+++ b/test/metric/gauge_test.rb
@@ -133,5 +133,12 @@ module PrometheusExporter::Metric
 
       assert_equal(gauge.to_prometheus_text, text)
     end
+
+    it "can correctly return data set" do
+      gauge.observe(5, sam: "ham")
+      gauge.observe(10, foo: "bar")
+
+      assert_equal(gauge.to_h, { sam: "ham" } => 5, { foo: "bar" } => 10)
+    end
   end
 end
diff --git a/test/metric/histogram_test.rb b/test/metric/histogram_test.rb
index a55c5b8..9732253 100644
--- a/test/metric/histogram_test.rb
+++ b/test/metric/histogram_test.rb
@@ -118,5 +118,13 @@ module PrometheusExporter::Metric
 
       assert_equal(histogram.to_prometheus_text, expected)
     end
+
+    it "can correctly return data set" do
+      histogram.observe(0.1, name: "bob", family: "skywalker")
+      histogram.observe(0.7, name: "bob", family: "skywalker")
+      histogram.observe(0.99, name: "bob", family: "skywalker")
+
+      assert_equal(histogram.to_h, { name: "bob", family: "skywalker" } => { "count" => 3, "sum" => 1.79 })
+    end
   end
 end
diff --git a/test/metric/summary_test.rb b/test/metric/summary_test.rb
index f61c93c..633f56b 100644
--- a/test/metric/summary_test.rb
+++ b/test/metric/summary_test.rb
@@ -122,5 +122,12 @@ module PrometheusExporter::Metric
       end
     end
 
+    it "can correctly return data set" do
+      summary.observe(0.1, name: "bob", family: "skywalker")
+      summary.observe(0.7, name: "bob", family: "skywalker")
+      summary.observe(0.99, name: "bob", family: "skywalker")
+
+      assert_equal(summary.to_h, { name: "bob", family: "skywalker" } => { "count" => 3, "sum" => 1.79 })
+    end
   end
 end

GitHub sha: 7d295e04