Add prometheus metrics at /api/v1/metrics

This commit is contained in:
wint3rmute 2023-01-18 00:33:55 +01:00 committed by Mateusz Bączek
parent 1c0b4205d4
commit 4d410d124f
4 changed files with 24 additions and 0 deletions

View File

@ -225,3 +225,11 @@ def get_playback_statistic
return tracker.as(Hash(String, Int64 | Float64))
end
def to_prometheus_metrics(metrics_struct : Hash(String, Number)) : String
return String.build do |str|
metrics_struct.each.each do |key, value|
str << key << " " << value << "\n"
end
end
end

View File

@ -27,6 +27,11 @@ class Invidious::Jobs::StatisticsRefreshJob < Invidious::Jobs::BaseJob
"playback" => {} of String => Int64 | Float64,
}
STATISTICS_PROMETHEUS = {
"invidious_updated_at" => Time.utc.to_unix,
"invidious_last_channel_refreshed_at" => 0_i64,
}
private getter db : DB::Database
def initialize(@db, @software_config : Hash(String, String))
@ -59,6 +64,9 @@ class Invidious::Jobs::StatisticsRefreshJob < Invidious::Jobs::BaseJob
users["activeHalfyear"] = Invidious::Database::Statistics.count_users_active_1m
users["activeMonth"] = Invidious::Database::Statistics.count_users_active_6m
STATISTICS_PROMETHEUS["invidious_updated_at"] = Time.utc.to_unix
STATISTICS_PROMETHEUS["invidious_last_channel_refreshed_at"] = Invidious::Database::Statistics.channel_last_update.try &.to_unix || 0_i64
STATISTICS["metadata"] = {
"updatedAt" => Time.utc.to_unix,
"lastChannelRefreshedAt" => Invidious::Database::Statistics.channel_last_update.try &.to_unix || 0_i64,

View File

@ -26,6 +26,11 @@ module Invidious::Routes::API::V1::Misc
end
end
def self.metrics(env)
env.response.content_type = "text/plain"
return to_prometheus_metrics(Invidious::Jobs::StatisticsRefreshJob::STATISTICS_PROMETHEUS)
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

View File

@ -310,6 +310,9 @@ module Invidious::Routing
# Misc
get "/api/v1/stats", {{namespace}}::Misc, :stats
if CONFIG.statistics_enabled
get "/api/v1/metrics", {{namespace}}::Misc, :metrics
end
get "/api/v1/playlists/:plid", {{namespace}}::Misc, :get_playlist
get "/api/v1/auth/playlists/:plid", {{namespace}}::Misc, :get_playlist
get "/api/v1/mixes/:rdid", {{namespace}}::Misc, :mixes