Add metrics around image resize/convert/dominant color extration

This commit is contained in:
Claudio Ortolina
2026-03-08 13:56:31 +00:00
parent 4584b7bcb8
commit b30f0fa1ee
3 changed files with 50 additions and 11 deletions
+18 -4
View File
@@ -14,8 +14,15 @@ defmodule MusicLibrary.Assets.Image do
@spec resize(binary(), pos_integer(), String.t()) :: {:ok, binary()} | {:error, term()}
def resize(cover_data, size \\ @default_size, format \\ @default_format) do
{:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
Image.write_to_buffer(thumb, extension(format))
:telemetry.span(
[:music_library, :assets, :image, :resize],
%{},
fn ->
{:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
result = Image.write_to_buffer(thumb, extension(format))
{result, %{}}
end
)
end
@spec convert(binary(), String.t(), String.t()) :: {:ok, binary()}
@@ -23,8 +30,15 @@ defmodule MusicLibrary.Assets.Image do
if data_format == target_format do
{:ok, cover_data}
else
{:ok, image} = Image.new_from_buffer(cover_data)
Image.write_to_buffer(image, extension(target_format))
:telemetry.span(
[:music_library, :assets, :image, :convert],
%{},
fn ->
{:ok, image} = Image.new_from_buffer(cover_data)
result = Image.write_to_buffer(image, extension(target_format))
{result, %{}}
end
)
end
end
+16 -7
View File
@@ -7,12 +7,21 @@ defmodule MusicLibrary.Colors.KMeansExtractor do
@spec extract_dominant_colors(binary(), pos_integer()) :: {:ok, [String.t()]} | {:error, term()}
def extract_dominant_colors(image_data, num_colors \\ 5) do
with {:ok, dir} <- Briefly.create(type: :directory),
path = dir <> "/temp_image.jpg",
{:ok, image} <- Image.new_from_buffer(image_data),
:ok <- Image.write_to_file(image, path),
{:ok, colors} <- DominantColors.dominant_colors(path) do
{:ok, Enum.take(colors, num_colors)}
end
:telemetry.span(
[:music_library, :colors, :extract],
%{},
fn ->
result =
with {:ok, dir} <- Briefly.create(type: :directory),
path = dir <> "/temp_image.jpg",
{:ok, image} <- Image.new_from_buffer(image_data),
:ok <- Image.write_to_file(image, path),
{:ok, colors} <- DominantColors.dominant_colors(path) do
{:ok, Enum.take(colors, num_colors)}
end
{result, %{}}
end
)
end
end
+16
View File
@@ -157,6 +157,22 @@ defmodule MusicLibraryWeb.Telemetry do
reporter_options: [nav: "Markdown"]
),
# Color Extraction Metrics
summary("music_library.colors.extract.stop.duration",
unit: {:native, :millisecond},
reporter_options: [nav: "Image Processing"]
),
# Image Processing Metrics
summary("music_library.assets.image.resize.stop.duration",
unit: {:native, :millisecond},
reporter_options: [nav: "Image Processing"]
),
summary("music_library.assets.image.convert.stop.duration",
unit: {:native, :millisecond},
reporter_options: [nav: "Image Processing"]
),
# VM Metrics
summary("vm.memory.total", unit: {:byte, :megabyte}),
summary("vm.total_run_queue_lengths.total"),