Skip to content

Commit 7ef3b00

Browse files
committed
Moved from misused constants to class variables in MetricsCollector, MetricsCollector is no longer initialized as a singleton
1 parent d3bd33f commit 7ef3b00

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/invidious.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ end
171171

172172
if CONFIG.statistics_enabled
173173
Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE)
174-
add_handler Metrics::METRICS_COLLECTOR
174+
add_handler Metrics::RouteMetricsCollector.new()
175175
end
176176

177177
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)

src/invidious/metrics.cr

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
module Metrics
44
record MetricLabels, request_method : String, request_route : String, response_code : Int32
55

6-
# Counts how many a given route was used
7-
REQUEST_COUNTERS = Hash(MetricLabels, Int64).new
8-
9-
# Counts how much time was used to handle requests to each route
10-
REQUEST_DURATION_SECONDS_SUMS = Hash(MetricLabels, Float32).new
6+
class RouteMetricsCollector < Kemal::Handler
7+
# Counts how many a given route was used
8+
@@num_of_request_counters = Hash(MetricLabels, Int64).new
9+
# Counts how much time was used to handle requests to each route
10+
@@request_duration_seconds_sums = Hash(MetricLabels, Float32).new
1111

12-
# The handler which will record metrics when registered in a Kemal application
13-
METRICS_COLLECTOR = RouteMetricsCollector.new(REQUEST_COUNTERS, REQUEST_DURATION_SECONDS_SUMS)
12+
def self.num_of_request_counters
13+
return @@num_of_request_counters
14+
end
1415

15-
class RouteMetricsCollector < Kemal::Handler
16-
def initialize(
17-
@num_of_request_counters : Hash(MetricLabels, Int64),
18-
@request_duration_seconds_sums : Hash(MetricLabels, Float32)
19-
)
16+
def self.request_duration_seconds_sums
17+
return @@request_duration_seconds_sums
2018
end
2119

2220
def call(context : HTTP::Server::Context)
@@ -33,15 +31,15 @@ module Metrics
3331
LOGGER.trace("Collecting metrics: handling #{request_method} #{request_path} took #{seconds_spent_handling}s and finished with status #{response_status}")
3432
metric_key = MetricLabels.new request_path, request_method, response_status
3533

36-
unless @num_of_request_counters.has_key?(metric_key)
37-
@num_of_request_counters[metric_key] = 0
34+
unless @@num_of_request_counters.has_key?(metric_key)
35+
@@num_of_request_counters[metric_key] = 0
3836
end
39-
@num_of_request_counters[metric_key] += 1
37+
@@num_of_request_counters[metric_key] += 1
4038

41-
unless @request_duration_seconds_sums.has_key?(metric_key)
42-
@request_duration_seconds_sums[metric_key] = 0.0
39+
unless @@request_duration_seconds_sums.has_key?(metric_key)
40+
@@request_duration_seconds_sums[metric_key] = 0.0
4341
end
44-
@request_duration_seconds_sums[metric_key] += seconds_spent_handling
42+
@@request_duration_seconds_sums[metric_key] += seconds_spent_handling
4543
end
4644
end
4745
end

src/invidious/routes/api/v1/misc.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Invidious::Routes::API::V1::Misc
1616
env.response.content_type = "text/plain"
1717

1818
return String.build do |str|
19-
Metrics::REQUEST_COUNTERS.each do |metric_labels, value|
19+
Metrics::RouteMetricsCollector.num_of_request_counters.each do |metric_labels, value|
2020
str << "http_requests_total{"
2121
str << "method=\"" << metric_labels.request_method << "\" "
2222
str << "route=\"" << metric_labels.request_route << "\" "
@@ -25,7 +25,7 @@ module Invidious::Routes::API::V1::Misc
2525
str << value << "\n"
2626
end
2727

28-
Metrics::REQUEST_DURATION_SECONDS_SUMS.each do |metric_labels, value|
28+
Metrics::RouteMetricsCollector.request_duration_seconds_sums.each do |metric_labels, value|
2929
str << "http_request_duration_seconds_sum{"
3030
str << "method=\"" << metric_labels.request_method << "\" "
3131
str << "route=\"" << metric_labels.request_route << "\" "

0 commit comments

Comments
 (0)