Skip to content

Commit

Permalink
Split '_other' into discourse/ember; init metrics to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaylorhq committed Nov 29, 2023
1 parent 77fde93 commit 65607d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
16 changes: 2 additions & 14 deletions app/controllers/deprecation_collector/collector_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,13 @@ def log
if !DeprecationCollector::List.include?(key)
# We only collect data for a bound set of deprecation-ids so that clients
# can't inflate our prometheus metrics by sending bogus data.
key = "_other"
key = key.start_with?("discourse") ? "_other_discourse" : "_other_ember"
end

add_to_counter(key, 1)
DeprecationCollector.add_to_counter(key, 1)
end

render json: success_json
end

private

def add_to_counter(name, value)
metric = DiscoursePrometheus::InternalMetric::Custom.new
metric.type = "Counter"
metric.labels = { db: RailsMultisite::ConnectionManagement.current_db, deprecation_id: name }
metric.name = "js_deprecation_count"
metric.description = "js deprecations reported by clients"
metric.value = value
$prometheus_client.send_json(metric.to_h)
end
end
end
15 changes: 15 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@

module ::DeprecationCollector
PLUGIN_NAME = "discourse-deprecation-collector"

def self.add_to_counter(name, value)
metric = DiscoursePrometheus::InternalMetric::Custom.new
metric.type = "Counter"
metric.labels = { db: RailsMultisite::ConnectionManagement.current_db, deprecation_id: name }
metric.name = "js_deprecation_count"
metric.description = "js deprecations reported by clients"
metric.value = value
$prometheus_client.send_json(metric.to_h)
end
end

require_relative "lib/deprecation_collector/engine"

on(:web_fork_started) do
# initialize counters so that `rate()` works correctly in prometheus
DeprecationCollector::List.each { |key| DeprecationCollector.add_to_counter(key, 0) }
end
7 changes: 4 additions & 3 deletions spec/system/deprecation_collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
test_deprecation_id = DeprecationCollector::List.first

stub =
DeprecationCollector::CollectorController
.any_instance
DeprecationCollector
.stubs(:add_to_counter)
.with do |value|
logged_deprecations << value
Expand All @@ -30,6 +29,8 @@
# Refresh the page to trigger the collector
page.refresh

try_until_success { expect(logged_deprecations).to include(test_deprecation_id, "_other") }
try_until_success do
expect(logged_deprecations).to include(test_deprecation_id, "_other_discourse")
end
end
end

0 comments on commit 65607d0

Please sign in to comment.