From 8d4c16c79c4af2138ad791130e763ce128606d64 Mon Sep 17 00:00:00 2001 From: wint3rmute Date: Thu, 20 Jul 2023 20:16:15 +0200 Subject: [PATCH] Moved from misused constants to class variables in MetricsCollector, MetricsCollector is no longer initialized as a singleton --- src/invidious.cr | 2 +- src/invidious/metrics.cr | 36 ++++++++++++++--------------- src/invidious/routes/api/v1/misc.cr | 4 ++-- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 66a93bab..67fa0498 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -170,7 +170,7 @@ end if CONFIG.statistics_enabled Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE) - add_handler Metrics::METRICS_COLLECTOR + add_handler Metrics::RouteMetricsCollector.new end if (CONFIG.use_pubsub_feeds.is_a?(Bool) && CONFIG.use_pubsub_feeds.as(Bool)) || (CONFIG.use_pubsub_feeds.is_a?(Int32) && CONFIG.use_pubsub_feeds.as(Int32) > 0) diff --git a/src/invidious/metrics.cr b/src/invidious/metrics.cr index 7165b14f..6fdd42b5 100644 --- a/src/invidious/metrics.cr +++ b/src/invidious/metrics.cr @@ -3,20 +3,18 @@ module Metrics record MetricLabels, request_method : String, request_route : String, response_code : Int32 - # Counts how many a given route was used - REQUEST_COUNTERS = Hash(MetricLabels, Int64).new - - # Counts how much time was used to handle requests to each route - REQUEST_DURATION_SECONDS_SUMS = Hash(MetricLabels, Float32).new - - # The handler which will record metrics when registered in a Kemal application - METRICS_COLLECTOR = RouteMetricsCollector.new(REQUEST_COUNTERS, REQUEST_DURATION_SECONDS_SUMS) - class RouteMetricsCollector < Kemal::Handler - def initialize( - @num_of_request_counters : Hash(MetricLabels, Int64), - @request_duration_seconds_sums : Hash(MetricLabels, Float32) - ) + # Counts how many times a given route was used + @@num_of_request_counters = Hash(MetricLabels, Int64).new + # Counts how much time was used to handle requests to each route + @@request_duration_seconds_sums = Hash(MetricLabels, Float32).new + + def self.num_of_request_counters + return @@num_of_request_counters + end + + def self.request_duration_seconds_sums + return @@request_duration_seconds_sums end def call(context : HTTP::Server::Context) @@ -33,15 +31,15 @@ module Metrics LOGGER.trace("Collecting metrics: handling #{request_method} #{request_path} took #{seconds_spent_handling}s and finished with status #{response_status}") metric_key = MetricLabels.new request_path, request_method, response_status - unless @num_of_request_counters.has_key?(metric_key) - @num_of_request_counters[metric_key] = 0 + unless @@num_of_request_counters.has_key?(metric_key) + @@num_of_request_counters[metric_key] = 0 end - @num_of_request_counters[metric_key] += 1 + @@num_of_request_counters[metric_key] += 1 - unless @request_duration_seconds_sums.has_key?(metric_key) - @request_duration_seconds_sums[metric_key] = 0.0 + unless @@request_duration_seconds_sums.has_key?(metric_key) + @@request_duration_seconds_sums[metric_key] = 0.0 end - @request_duration_seconds_sums[metric_key] += seconds_spent_handling + @@request_duration_seconds_sums[metric_key] += seconds_spent_handling end end end diff --git a/src/invidious/routes/api/v1/misc.cr b/src/invidious/routes/api/v1/misc.cr index d827b622..2a0ce5a7 100644 --- a/src/invidious/routes/api/v1/misc.cr +++ b/src/invidious/routes/api/v1/misc.cr @@ -32,7 +32,7 @@ module Invidious::Routes::API::V1::Misc env.response.content_type = "text/plain" return String.build do |str| - Metrics::REQUEST_COUNTERS.each do |metric_labels, value| + Metrics::RouteMetricsCollector.num_of_request_counters.each do |metric_labels, value| str << "http_requests_total{" str << "method=\"" << metric_labels.request_method << "\" " str << "route=\"" << metric_labels.request_route << "\" " @@ -41,7 +41,7 @@ module Invidious::Routes::API::V1::Misc str << value << "\n" end - Metrics::REQUEST_DURATION_SECONDS_SUMS.each do |metric_labels, value| + Metrics::RouteMetricsCollector.request_duration_seconds_sums.each do |metric_labels, value| str << "http_request_duration_seconds_sum{" str << "method=\"" << metric_labels.request_method << "\" " str << "route=\"" << metric_labels.request_route << "\" "