Track both content and cache size

This commit is contained in:
Claudio Ortolina
2025-09-20 09:49:06 +03:00
parent 68a2159d7d
commit dd09fb907b
4 changed files with 31 additions and 12 deletions
+1 -1
View File
@@ -12,11 +12,11 @@ defmodule MusicLibrary.Application do
_ = Assets.Cache.new()
children = [
MusicLibraryWeb.Telemetry,
MusicLibrary.Vault,
MusicLibrary.Repo,
MusicLibrary.ErrorRepo,
MusicLibrary.BackgroundRepo,
MusicLibraryWeb.Telemetry,
{Oban, Application.fetch_env!(:music_library, Oban)},
{Ecto.Migrator,
repos: Application.fetch_env!(:music_library, :ecto_repos), skip: skip_migrations?()},
+26 -1
View File
@@ -1,7 +1,9 @@
defmodule MusicLibrary.Assets do
alias MusicLibrary.Assets.Asset
alias MusicLibrary.Assets.{Asset, Cache}
alias MusicLibrary.Repo
import Ecto.Query
@doc """
Store any file type - the responsibility to correctly populate format and
properties is left to the caller.
@@ -28,4 +30,27 @@ defmodule MusicLibrary.Assets do
def get!(hash) do
Repo.get!(Asset, hash)
end
def total_content_size do
q =
from p in Asset, select: sum(fragment("length(content)"))
Repo.one(q)
end
def track_total_content_size do
:telemetry.execute(
[:music_library, :assets],
%{content_size: total_content_size()},
%{}
)
end
def track_total_cache_size do
:telemetry.execute(
[:music_library, :assets],
%{cache_size: Cache.total_content_size()},
%{}
)
end
end
-8
View File
@@ -23,14 +23,6 @@ defmodule MusicLibrary.Assets.Cache do
)
end
def track_total_content_size do
:telemetry.execute(
[:music_library, :assets, :cache],
%{total_content_size: total_content_size()},
%{}
)
end
def prune(older_than_seconds) do
threshold =
DateTime.utc_now()
+4 -2
View File
@@ -116,7 +116,8 @@ defmodule MusicLibraryWeb.Telemetry do
),
# Assets
summary("music_library.assets.cache.total_content_size", unit: {:byte, :kilobyte}),
summary("music_library.assets.cache_size", unit: {:byte, :kilobyte}),
summary("music_library.assets.content_size", unit: {:byte, :megabyte}),
# VM Metrics
summary("vm.memory.total", unit: {:byte, :megabyte}),
@@ -128,7 +129,8 @@ defmodule MusicLibraryWeb.Telemetry do
defp periodic_measurements do
[
{MusicLibrary.Assets.Cache, :track_total_content_size, []}
{MusicLibrary.Assets, :track_total_cache_size, []},
{MusicLibrary.Assets, :track_total_content_size, []}
# A module, function and arguments to be invoked periodically.
# This function must call :telemetry.execute/3 and a metric must be added above.
# {MusicLibraryWeb, :count_users, []}