mirror of https://github.com/iv-org/invidious.git
Moved from misused constants to class variables in MetricsCollector, MetricsCollector is no longer initialized as a singleton
This commit is contained in:
parent
9db6eb058c
commit
8d4c16c79c
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 << "\" "
|
||||
|
|
Loading…
Reference in New Issue