FEATURE: Allow for custom labels for default metrics

FEATURE: Allow for custom labels for default metrics

Co-authored-by: Andy Lau 614205+ihurrahi@users.noreply.github.com

diff --git a/README.md b/README.md
index 4490aa2..2f01b99 100644
--- a/README.md
+++ b/README.md
@@ -203,6 +203,21 @@ $ bundle exec prometheus_exporter
 All metrics have a `controller` and an `action` label.  
 `http_requests_total` additionally has a (HTTP response) `status` label.  
 
+To add your own labels to the default metrics, create a subclass of `PrometheusExporter::Middleware`, override `custom_labels`, and use it in your initializer.
+`‍``ruby
+class MyMiddleware < PrometheusExporter::Middleware
+  def custom_labels(env)
+    labels = {}
+
+    if env['HTTP_X_PLATFORM']
+      labels['platform'] = env['HTTP_X_PLATFORM']
+    end
+
+    labels
+  end
+end
+`‍``
+
 ¹) Only available when Redis is used.  
 ²) Only available when Mysql or PostgreSQL are used.  
 ³) Only available when [Instrumenting Request Queueing Time](#instrumenting-request-queueing-time) is set up.  
diff --git a/lib/prometheus_exporter/client.rb b/lib/prometheus_exporter/client.rb
index b05815e..3c83fa3 100644
--- a/lib/prometheus_exporter/client.rb
+++ b/lib/prometheus_exporter/client.rb
@@ -107,7 +107,16 @@ module PrometheusExporter
     end
 
     def send_json(obj)
-      payload = @custom_labels.nil? ? obj : obj.merge(custom_labels: @custom_labels)
+      payload = 
+        if @custom_labels
+          if obj[:custom_labels]
+            obj.merge(custom_labels: @custom_labels.merge(obj[:custom_labels]))
+          else
+            obj.merge(custom_labels: @custom_labels)
+          end
+        else
+          obj
+        end
       send(@json_serializer.dump(payload))
     end
 
diff --git a/lib/prometheus_exporter/instrumentation/sidekiq.rb b/lib/prometheus_exporter/instrumentation/sidekiq.rb
index 540cfe0..d4c0d05 100644
--- a/lib/prometheus_exporter/instrumentation/sidekiq.rb
+++ b/lib/prometheus_exporter/instrumentation/sidekiq.rb
@@ -44,6 +44,7 @@ module PrometheusExporter::Instrumentation
       @client.send_json(
         type: "sidekiq",
         name: get_name(worker, msg),
+        queue: queue,
         success: success,
         shutdown: shutdown,
         duration: duration
diff --git a/lib/prometheus_exporter/middleware.rb b/lib/prometheus_exporter/middleware.rb
index 705526c..03e114c 100644
--- a/lib/prometheus_exporter/middleware.rb
+++ b/lib/prometheus_exporter/middleware.rb
@@ -44,14 +44,25 @@ class PrometheusExporter::Middleware
       controller = params["controller"]
     end
 
-    @client.send_json(
+    obj = {
       type: "web",
       timings: info,
       queue_time: queue_time,
       action: action,
       controller: controller,
       status: status
-    )
+    }
+    labels = custom_labels(env)
+    if labels
+      obj = obj.merge(custom_labels: labels)
+    end
+
+    @client.send_json(obj)
+  end
+
+  # allows subclasses to add custom labels based on env
+  def custom_labels(env)
+    nil
   end
 
   private
diff --git a/lib/prometheus_exporter/server/sidekiq_collector.rb b/lib/prometheus_exporter/server/sidekiq_collector.rb
index 4d25a45..c0aff88 100644
--- a/lib/prometheus_exporter/server/sidekiq_collector.rb
+++ b/lib/prometheus_exporter/server/sidekiq_collector.rb
@@ -17,7 +17,7 @@ module PrometheusExporter::Server
     end
 
     def collect(obj)
-      default_labels = { job_name: obj['name'] }
+      default_labels = { job_name: obj['name'], queue: obj['queue'] }
       custom_labels = obj['custom_labels']
       labels = custom_labels.nil? ? default_labels : default_labels.merge(custom_labels)
 

GitHub sha: 823710f2

This commit appears in #127 which was merged by SamSaffron.