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
|
if CONFIG.statistics_enabled
|
||||||
Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE)
|
Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE)
|
||||||
add_handler Metrics::METRICS_COLLECTOR
|
add_handler Metrics::RouteMetricsCollector.new
|
||||||
end
|
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)
|
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
|
module Metrics
|
||||||
record MetricLabels, request_method : String, request_route : String, response_code : Int32
|
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
|
class RouteMetricsCollector < Kemal::Handler
|
||||||
def initialize(
|
# Counts how many times a given route was used
|
||||||
@num_of_request_counters : Hash(MetricLabels, Int64),
|
@@num_of_request_counters = Hash(MetricLabels, Int64).new
|
||||||
@request_duration_seconds_sums : Hash(MetricLabels, Float32)
|
# 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
|
end
|
||||||
|
|
||||||
def call(context : HTTP::Server::Context)
|
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}")
|
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
|
metric_key = MetricLabels.new request_path, request_method, response_status
|
||||||
|
|
||||||
unless @num_of_request_counters.has_key?(metric_key)
|
unless @@num_of_request_counters.has_key?(metric_key)
|
||||||
@num_of_request_counters[metric_key] = 0
|
@@num_of_request_counters[metric_key] = 0
|
||||||
end
|
end
|
||||||
@num_of_request_counters[metric_key] += 1
|
@@num_of_request_counters[metric_key] += 1
|
||||||
|
|
||||||
unless @request_duration_seconds_sums.has_key?(metric_key)
|
unless @@request_duration_seconds_sums.has_key?(metric_key)
|
||||||
@request_duration_seconds_sums[metric_key] = 0.0
|
@@request_duration_seconds_sums[metric_key] = 0.0
|
||||||
end
|
end
|
||||||
@request_duration_seconds_sums[metric_key] += seconds_spent_handling
|
@@request_duration_seconds_sums[metric_key] += seconds_spent_handling
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,7 @@ module Invidious::Routes::API::V1::Misc
|
||||||
env.response.content_type = "text/plain"
|
env.response.content_type = "text/plain"
|
||||||
|
|
||||||
return String.build do |str|
|
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 << "http_requests_total{"
|
||||||
str << "method=\"" << metric_labels.request_method << "\" "
|
str << "method=\"" << metric_labels.request_method << "\" "
|
||||||
str << "route=\"" << metric_labels.request_route << "\" "
|
str << "route=\"" << metric_labels.request_route << "\" "
|
||||||
|
@ -41,7 +41,7 @@ module Invidious::Routes::API::V1::Misc
|
||||||
str << value << "\n"
|
str << value << "\n"
|
||||||
end
|
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 << "http_request_duration_seconds_sum{"
|
||||||
str << "method=\"" << metric_labels.request_method << "\" "
|
str << "method=\"" << metric_labels.request_method << "\" "
|
||||||
str << "route=\"" << metric_labels.request_route << "\" "
|
str << "route=\"" << metric_labels.request_route << "\" "
|
||||||
|
|
Loading…
Reference in New Issue