Repository: discourse/prometheus_exporter (Ruby)

Issue:
ActiveRecord collector does not expose hostname by default
AR Instrumentation exposes the `hostname` in https://github.com/discourse/prometheus_exporter/blob/main/lib/prometheus_exporter/instrumentation/active_record.rb#L57
but its not picked up in the collector: https://github.com/discourse/prometheus_exporter/blob/main/lib/prometheus_exporter/server/active_record_collector.rb#L28-L39

Is this behavior intentional? 


Discussion:
Interesting, no I do not think this is intentional. 
I've discovered this issue while working on #268 refactor. I'll send a PR with a fix.



Patch:
diff --git a/lib/prometheus_exporter/server/active_record_collector.rb b/lib/prometheus_exporter/server/active_record_collector.rb
index d22ef320..eee5aae3 100644
--- a/lib/prometheus_exporter/server/active_record_collector.rb
+++ b/lib/prometheus_exporter/server/active_record_collector.rb
@@ -26,7 +26,7 @@ def metrics
       metrics = {}
 
       @active_record_metrics.map do |m|
-        metric_key = (m["metric_labels"] || {}).merge("pid" => m["pid"])
+        metric_key = (m["metric_labels"] || {}).merge("pid" => m["pid"], "hostname" => m["hostname"])
         metric_key.merge!(m["custom_labels"]) if m["custom_labels"]
 
         ACTIVE_RECORD_GAUGES.map do |k, help|


Test patch:
diff --git a/test/custom_type_collector.rb b/test/custom_type_collector.rb
index d1a2b267..f2c3abdb 100644
--- a/test/custom_type_collector.rb
+++ b/test/custom_type_collector.rb
@@ -1,4 +1,5 @@
 # frozen_string_literal: true
+# rubocop: disable Lint/Debugger
 
 class CustomTypeCollector < PrometheusExporter::Server::TypeCollector
   def type
diff --git a/test/server/active_record_collector_test.rb b/test/server/active_record_collector_test.rb
index 5be92150..cb318011 100644
--- a/test/server/active_record_collector_test.rb
+++ b/test/server/active_record_collector_test.rb
@@ -14,6 +14,7 @@ def test_collecting_metrics
     collector.collect(
       "type" => "active_record",
       "pid" => "1000",
+      "hostname" => "localhost",
       "connections" => 50,
       "busy" => 20,
       "dead" => 10,
@@ -30,20 +31,21 @@ def test_collecting_metrics_with_custom_labels
     collector.collect(
       "type" => "active_record",
       "pid" => "1000",
+      "hostname" => "localhost",
       "connections" => 50,
       "busy" => 20,
       "dead" => 10,
       "idle" => 20,
       "waiting" => 0,
       "size" => 120,
-      'metric_labels' => {
-        'service' => 'service1'
+      "metric_labels" => {
+        "service" => "service1"
       }
     )
 
     metrics = collector.metrics
     assert_equal 6, metrics.size
-    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{service="service1",pid="1000"} 50'))
+    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{service="service1",pid="1000",hostname="localhost"} 50'))
   end
 
   def test_collecting_metrics_with_client_default_labels
@@ -51,6 +53,7 @@ def test_collecting_metrics_with_client_default_labels
     collector.collect(
       "type" => "active_record",
       "pid" => "1000",
+      "hostname" => "localhost",
       "connections" => 50,
       "busy" => 20,
       "dead" => 10,
@@ -67,7 +70,7 @@ def test_collecting_metrics_with_client_default_labels
 
     metrics = collector.metrics
     assert_equal 6, metrics.size
-    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{service="service1",pid="1000",environment="test"} 50'))
+    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{service="service1",pid="1000",hostname="localhost",environment="test"} 50'))
   end
 
   def test_collecting_metrics_for_multiple_pools
@@ -102,7 +105,7 @@ def test_collecting_metrics_for_multiple_pools
 
     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'))
+    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{pool_name="primary",pid="1000",hostname="localhost"} 50'))
+    assert(metrics.first.metric_text.include?('active_record_connection_pool_connections{pool_name="other",pid="1000",hostname="localhost"} 5'))
   end
 end