Skip to content

Commit

Permalink
feat: use iodata for metrics export
Browse files Browse the repository at this point in the history
This prevents memory bloat form creating all intermediate binaries that
are later dropped. Instead we try to reuse as much of binaries as
possible and then let the VM do the concatenation if needed.
  • Loading branch information
hauleth committed Jan 30, 2025
1 parent 0fe7773 commit cb49404
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.5
2.1.6
12 changes: 6 additions & 6 deletions lib/supavisor/monitoring/prom_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule Supavisor.Monitoring.PromEx do
end
end

@spec get_metrics() :: String.t()
@spec get_metrics() :: iodata()
def get_metrics do
metrics_tags =
case Application.fetch_env(:supavisor, :metrics_tags) do
Expand All @@ -71,7 +71,7 @@ defmodule Supavisor.Monitoring.PromEx do
metrics =
PromEx.get_metrics(__MODULE__)
|> String.split("\n")
|> Enum.map_join("\n", &parse_and_add_tags(&1, def_tags))
|> Enum.map(&parse_and_add_tags(&1, def_tags))

Supavisor.Monitoring.PromEx.ETSCronFlusher
|> PromEx.ETSCronFlusher.defer_ets_flush()
Expand All @@ -81,7 +81,7 @@ defmodule Supavisor.Monitoring.PromEx do

@spec do_cache_tenants_metrics() :: list
def do_cache_tenants_metrics do
metrics = get_metrics() |> String.split("\n")
metrics = get_metrics() |> IO.iodata_to_binary() |> String.split("\n")

pools =
Registry.select(Supavisor.Registry.TenantClients, [{{:"$1", :_, :_}, [], [:"$1"]}])
Expand Down Expand Up @@ -109,7 +109,7 @@ defmodule Supavisor.Monitoring.PromEx do
end
end

@spec parse_and_add_tags(String.t(), String.t()) :: String.t()
@spec parse_and_add_tags(String.t(), String.t()) :: iodata()
defp parse_and_add_tags(line, def_tags) do
case Regex.run(~r/(?!\#)^(\w+)(?:{(.*?)})?\s*(.+)$/, line) do
nil ->
Expand All @@ -122,10 +122,10 @@ defmodule Supavisor.Monitoring.PromEx do
if tags == "" do
def_tags
else
"#{tags},#{def_tags}"
[tags, ",", def_tags]
end

"#{key}{#{tags}} #{value}"
[key, "{", tags, "}", value, "\n"]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/supavisor_web/controllers/metrics_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ defmodule SupavisorWeb.MetricsController do
end

def merge_node_metrics({_, {_node, metrics}}, acc) do
[metrics <> "\n" | acc]
[metrics, "\n" | acc]
end
end

0 comments on commit cb49404

Please sign in to comment.