DEV: move missing upload metrics to global collector (#3)

DEV: move missing upload metrics to global collector (#3)

  • DEV: move missing upload metrics to global collector

  • Set ‘nil’ as default value instead of 0

diff --git a/lib/collector.rb b/lib/collector.rb
index bab3206..db9ac31 100644
--- a/lib/collector.rb
+++ b/lib/collector.rb
@@ -24,6 +24,9 @@ module ::DiscoursePrometheus
       @sidekiq_job_duration_seconds = nil
       @sidekiq_job_count = nil
 
+      @missing_s3_uploads = nil
+      @missing_post_uploads = nil
+
       @process_metrics = []
       @global_metrics = []
 
@@ -151,6 +154,16 @@ module ::DiscoursePrometheus
         "Whether or not Sidekiq is paused"
       )
 
+      global_metrics << Gauge.new(
+        "missing_s3_uploads",
+        "Number of missing uploads in S3"
+      )
+
+      global_metrics << Gauge.new(
+        "missing_post_uploads",
+        "Number of missing post uploads"
+      )
+
       @global_metrics = global_metrics
     end
 
diff --git a/lib/internal_metric/global.rb b/lib/internal_metric/global.rb
index f041d11..9e5240a 100644
--- a/lib/internal_metric/global.rb
+++ b/lib/internal_metric/global.rb
@@ -16,7 +16,9 @@ module DiscoursePrometheus::InternalMetric
       :queued_app_reqs,
       :sidekiq_jobs_enqueued,
       :sidekiq_processes,
-      :sidekiq_paused
+      :sidekiq_paused,
+      :missing_post_uploads,
+      :missing_s3_uploads
 
     def initialize
       @active_app_reqs = 0
@@ -62,6 +64,9 @@ module DiscoursePrometheus::InternalMetric
 
       @sidekiq_processes = (Sidekiq::ProcessSet.new.size || 0) rescue 0
       @sidekiq_paused = sidekiq_paused_states
+
+      @missing_s3_uploads = missing_uploads("s3")
+      @missing_post_uploads = missing_uploads("post")
     end
 
     private
@@ -141,5 +146,15 @@ module DiscoursePrometheus::InternalMetric
 
       paused
     end
+
+    def missing_uploads(type)
+      missing = {}
+
+      RailsMultisite::ConnectionManagement.each_connection do |db|
+        missing[{ db: db }] = Discourse.stats.get("missing_#{type}_uploads")
+      end
+
+      missing
+    end
   end
 end
diff --git a/plugin.rb b/plugin.rb
index 298150c..9f81ad6 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -77,19 +77,6 @@ after_initialize do
     metric.job_name = stat.name
     metric.duration = stat.duration_ms * 0.001
     $prometheus_client.send_json metric.to_h
-
-    if stat.name == "Jobs::EnsureS3UploadsExistence"
-      $prometheus_client.send_json DiscoursePrometheus::InternalMetric::Custom.create_gauge_hash(
-        "missing_s3_uploads",
-        "Number of missing uploads in S3",
-        Discourse.stats.get("missing_s3_uploads") || 0
-      )
-      $prometheus_client.send_json DiscoursePrometheus::InternalMetric::Custom.create_gauge_hash(
-        "missing_post_uploads",
-        "Number of missing post uploads",
-        Discourse.stats.get("missing_post_uploads") || 0
-      )
-    end
   end
 
   DiscourseEvent.on(:sidekiq_job_ran) do |worker, msg, queue, duration|
diff --git a/spec/lib/internal_metric/global_spec.rb b/spec/lib/internal_metric/global_spec.rb
index 275cb15..13587b5 100644
--- a/spec/lib/internal_metric/global_spec.rb
+++ b/spec/lib/internal_metric/global_spec.rb
@@ -4,6 +4,8 @@ require 'rails_helper'
 
 module DiscoursePrometheus::InternalMetric
   describe Global do
+    let(:db) { RailsMultisite::ConnectionManagement.current_db }
+
     it "can collect global metrics" do
       metric = Global.new
       metric.collect
@@ -13,6 +15,21 @@ module DiscoursePrometheus::InternalMetric
       expect(metric.postgres_replica_available).to eq(nil)
     end
 
+    it "should collect the missing upload metrics" do
+      Discourse.stats.set("missing_s3_uploads", 2)
+      Discourse.stats.set("missing_post_uploads", 1)
+
+      metric = Global.new
+      metric.collect
+
+      expect(metric.missing_s3_uploads).to eq({
+        { db: db } => 2
+      })
+      expect(metric.missing_post_uploads).to eq({
+        { db: db } => 1
+      })
+    end
+
     describe 'sidekiq paused' do
       after do
         Sidekiq.unpause_all!
@@ -23,14 +40,14 @@ module DiscoursePrometheus::InternalMetric
         metric.collect
 
         expect(metric.sidekiq_paused).to eq(
-          { db: RailsMultisite::ConnectionManagement.current_db } => nil
+          { db: db } => nil
         )
 
         Sidekiq.pause!
         metric.collect
 
         expect(metric.sidekiq_paused).to eq(
-          { db: RailsMultisite::ConnectionManagement.current_db } => 1
+          { db: db } => 1
         )
       end
     end

GitHub sha: a9df7c80