From 0835f53e615350f432be9d7e759c7ec8327cf829 Mon Sep 17 00:00:00 2001 From: Adam McCrea Date: Thu, 14 Dec 2023 12:10:47 -0500 Subject: [PATCH] perf: Skip reporting if there are no metrics (#191) * Skip reporting if there are no metrics We'll still report the first time through the loop. This lets Judoscale know the adapter is set up correctly. * Fix test --- judoscale-ruby/lib/judoscale/reporter.rb | 9 +++++++++ judoscale-ruby/test/reporter_test.rb | 20 ++++++++++++++++---- judoscale-ruby/test/test_helper.rb | 6 ++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/judoscale-ruby/lib/judoscale/reporter.rb b/judoscale-ruby/lib/judoscale/reporter.rb index 232dc6a0..ab4d1c81 100644 --- a/judoscale-ruby/lib/judoscale/reporter.rb +++ b/judoscale-ruby/lib/judoscale/reporter.rb @@ -74,17 +74,26 @@ def stop! @_thread&.terminate @_thread = nil @pid = nil + @reported = false end private def report(config, metrics) + # Make sure we report at least once, even if there are no metrics, + # so Judoscale knows the adapter is installed and running. + if @reported && metrics.empty? + logger.debug "No metrics to report - skipping" + return + end + report = Report.new(Judoscale.adapters, config, metrics) logger.info "Reporting #{report.metrics.size} metrics" result = AdapterApi.new(config).report_metrics(report.as_json) case result when AdapterApi::SuccessResponse + @reported = true logger.debug "Reported successfully" when AdapterApi::FailureResponse logger.error "Reporter failed: #{result.failure_message}" diff --git a/judoscale-ruby/test/reporter_test.rb b/judoscale-ruby/test/reporter_test.rb index 28efef98..8aac54f6 100644 --- a/judoscale-ruby/test/reporter_test.rb +++ b/judoscale-ruby/test/reporter_test.rb @@ -10,9 +10,12 @@ module Judoscale Judoscale.configure do |config| config.current_runtime_container = Config::RuntimeContainer.new("web.1") config.api_base_url = "http://example.com/api/test-token" + config.test_job_config.enabled = true end } + after { Reporter.instance.stop! } + describe ".start" do it "initializes the reporter with the current configuration and loaded adapters" do reporter_mock = Minitest::Mock.new @@ -39,10 +42,6 @@ module Judoscale end describe "#start!" do - after { - Reporter.instance.stop! - } - def run_reporter_start_thread stub_reporter_loop { reporter_thread = Reporter.instance.start!(Config.instance, Judoscale.adapters) @@ -172,6 +171,19 @@ def stub_reporter_loop assert_requested stub end + it "only sends an empty (no metrics) report one time" do + metrics_collector = Test::TestEmptyMetricsCollector.new + + expected_body = Report.new(Judoscale.adapters, Config.instance, metrics_collector.collect).as_json + stub = stub_request(:post, "http://example.com/api/test-token/v3/reports") + .with(body: JSON.generate(expected_body)).to_return({status: 200}).times(1) + + Reporter.instance.run_metrics_collection Config.instance, [metrics_collector] + Reporter.instance.run_metrics_collection Config.instance, [metrics_collector] + + assert_requested stub + end + it "logs reporting failures" do metrics_collector = Test::TestWebMetricsCollector.new diff --git a/judoscale-ruby/test/test_helper.rb b/judoscale-ruby/test/test_helper.rb index 4d8346a2..2c771fc4 100644 --- a/judoscale-ruby/test/test_helper.rb +++ b/judoscale-ruby/test/test_helper.rb @@ -27,6 +27,12 @@ def collect [Metric.new(:qt, 1, Time.now)] end end + + class TestEmptyMetricsCollector < Judoscale::WebMetricsCollector + def collect + [] + end + end end add_adapter :test_web, {}, metrics_collector: Test::TestWebMetricsCollector