Merge branch 'add-prometheus-metrics-endpoint'

From https://github.com/iv-org/invidious/pull/3576
This commit is contained in:
Fijxu
2025-03-01 03:39:04 -03:00
6 changed files with 99 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
require "../../../metrics.cr"
module Invidious::Routes::API::V1::Misc
# Stats API endpoint for Invidious
def self.stats(env)
@@ -26,6 +28,39 @@ module Invidious::Routes::API::V1::Misc
end
end
def self.metrics(env)
if !CONFIG.statistics_enabled
env.response.status_code = 204
return
end
env.response.content_type = "text/plain"
return String.build do |str|
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 << "\" "
str << "response_code=\"" << metric_labels.response_code << "\""
str << "} "
str << value << "\n"
end
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 << "\" "
str << "response_code=\"" << metric_labels.response_code << "\""
str << "} "
str << value << "\n"
end
Invidious::Jobs::StatisticsRefreshJob::STATISTICS_PROMETHEUS.each.each do |key, value|
str << key << " " << value << "\n"
end
end
end
# APIv1 currently uses the same logic for both
# user playlists and Invidious playlists. This means that we can't
# reasonably split them yet. This should be addressed in APIv2