FIX: support multiple pools in active record collector (#167)

FIX: support multiple pools in active record collector (#167)

Use pool_name in addition to pid and hostname when deleting duplicate metrics.

diff --git a/lib/prometheus_exporter/server/active_record_collector.rb b/lib/prometheus_exporter/server/active_record_collector.rb
index 4f9a228..d22ef32 100644
--- a/lib/prometheus_exporter/server/active_record_collector.rb
+++ b/lib/prometheus_exporter/server/active_record_collector.rb
@@ -47,7 +47,8 @@ module PrometheusExporter::Server
       obj["created_at"] = now
 
       @active_record_metrics.delete_if do |current|
-        (obj["pid"] == current["pid"] && obj["hostname"] == current["hostname"]) ||
+        (obj["pid"] == current["pid"] && obj["hostname"] == current["hostname"] &&
+         obj["metric_labels"]["pool_name"] == current["metric_labels"]["pool_name"]) ||
           (current["created_at"] + MAX_ACTIVERECORD_METRIC_AGE < now)
       end
 
diff --git a/test/server/active_record_collector_test.rb b/test/server/active_record_collector_test.rb
index 91e13c6..938532a 100644
--- a/test/server/active_record_collector_test.rb
+++ b/test/server/active_record_collector_test.rb
@@ -69,4 +69,40 @@ class PrometheusActiveRecordCollectorTest < Minitest::Test
     assert_equal 6, metrics.size
     assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{service="service1",pid="1000",environment="test"} 50'))
   end
+
+  def test_collecting_metrics_for_multiple_pools
+    collector.collect(
+      "type" => "active_record",
+      "hostname" => "localhost",
+      "pid" => "1000",
+      "connections" => 50,
+      "busy" => 20,
+      "dead" => 10,
+      "idle" => 20,
+      "waiting" => 0,
+      "size" => 120,
+      "metric_labels" => {
+        "pool_name" => "primary"
+      }
+    )
+    collector.collect(
+      "type" => "active_record",
+      "hostname" => "localhost",
+      "pid" => "1000",
+      "connections" => 5,
+      "busy" => 2,
+      "dead" => 1,
+      "idle" => 2,
+      "waiting" => 0,
+      "size" => 12,
+      "metric_labels" => {
+        "pool_name" => "other"
+      }
+    )
+
+    metrics = collector.metrics
+    assert_equal 6, metrics.size
+    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{pool_name="primary",pid="1000"} 50'))
+    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{pool_name="other",pid="1000"} 5'))
+  end
 end

GitHub sha: 35246a8c

This commit appears in #167 which was merged by OsamaSayegh.