Collecting num of requests and handling time from each Kemal route

This commit is contained in:
wint3rmute
2023-03-19 16:28:10 +01:00
committed by Mateusz Bączek
parent 4d410d124f
commit 27dd94f60d
3 changed files with 75 additions and 1 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)
@@ -28,7 +30,30 @@ module Invidious::Routes::API::V1::Misc
def self.metrics(env)
env.response.content_type = "text/plain"
return to_prometheus_metrics(Invidious::Jobs::StatisticsRefreshJob::STATISTICS_PROMETHEUS)
return String.build do |str|
Metrics::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::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