Skip to content

Commit

Permalink
fix: clean up metrics tags (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Jun 11, 2024
1 parent d0a3f61 commit bb39c5d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.58
1.1.59
22 changes: 22 additions & 0 deletions lib/supavisor/monitoring/prom_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ defmodule Supavisor.Monitoring.PromEx do
line

[_, key, tags, value] ->
tags = clean_string(tags)

tags =
if tags == "" do
def_tags
Expand All @@ -135,4 +137,24 @@ defmodule Supavisor.Monitoring.PromEx do
"#{key}{#{tags}} #{value}"
end
end

@spec clean_string(String.t()) :: String.t()
def clean_string(metric_string) do
regex = ~r/=\s*"([^"]*?)"/

String.replace(metric_string, regex, fn match ->
[_, value] = Regex.run(regex, match)

cleaned =
value
|> String.replace(~r/\n+/, "")
|> String.trim()

if value != cleaned do
Logger.error("Tag validation: #{inspect(value)} / #{inspect(cleaned)}")
end

"=\"#{cleaned}\""
end)
end
end
10 changes: 10 additions & 0 deletions test/supavisor/prom_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ defmodule Supavisor.PromExTest do

refute PromEx.get_metrics() =~ "tenant=\"#{@tenant}\""
end

test "clean_string/1 removes extra spaces from metric string" do
input =
"db_name=\"postgres \",mode=\" transaction\",tenant=\"dev_tenant \n\",type=\"\n single\",user=\"\npostgres\n\""

expected_output =
"db_name=\"postgres\",mode=\"transaction\",tenant=\"dev_tenant\",type=\"single\",user=\"postgres\""

assert expected_output == PromEx.clean_string(input)
end
end

0 comments on commit bb39c5d

Please sign in to comment.