Skip to content

Commit 7779472

Browse files
tjchambersjoshuap
andauthored
Move regex constants to functions (#635)
OTP 28 does not allow regex in constants. Cnnverting them to simple functions. cannot inject attribute @excluded_queries into function/macro because cannot escape #Reference<0.441870236.419561494.195505>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity --------- Co-authored-by: Joshua Wood <[email protected]>
1 parent 71de52f commit 7779472

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

lib/honeybadger/insights/ecto.ex

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ defmodule Honeybadger.Insights.Ecto do
5858
@required_dependencies [Ecto.Repo]
5959
@telemetry_events []
6060

61-
@excluded_queries [
62-
~r/^(begin|commit)( immediate)?( transaction)?$/i,
63-
# Also exclude pg_notify which is often used with Oban
64-
~r/SELECT pg_notify/,
65-
~r/schema_migrations/
66-
]
67-
6861
@excluded_sources [
6962
"schema_migrations",
7063
"oban_jobs",
@@ -143,7 +136,7 @@ defmodule Honeybadger.Insights.Ecto do
143136
true
144137
else
145138
:excluded_queries
146-
|> get_insights_config(@excluded_queries)
139+
|> get_insights_config(excluded_queries())
147140
|> Enum.any?(fn
148141
pattern when is_binary(pattern) -> query == pattern
149142
%Regex{} = pattern -> Regex.match?(pattern, query)
@@ -156,27 +149,35 @@ defmodule Honeybadger.Insights.Ecto do
156149
Keyword.get(repo.config(), :telemetry_prefix, []) ++ [:query]
157150
end
158151

159-
@escape_quotes ~r/(\\\"|\\')/
160-
@squote_data ~r/'(?:[^']|'')*'/
161-
@dquote_data ~r/"(?:[^"]|"")*"/
162-
@number_data ~r/\b\d+\b/
163-
@double_quoters ~r/(postgres|sqlite3|postgis)/i
152+
defp escape_quotes(), do: ~r/(\\\"|\\')/
153+
defp squote_data(), do: ~r/'(?:[^']|'')*'/
154+
defp dquote_data(), do: ~r/"(?:[^"]|"")*"/
155+
defp number_data(), do: ~r/\b\d+\b/
156+
defp double_quoters(), do: ~r/(postgres|sqlite3|postgis)/i
157+
158+
defp excluded_queries(),
159+
do: [
160+
~r/^(begin|commit)( immediate)?( transaction)?$/i,
161+
# Also exclude pg_notify which is often used with Oban
162+
~r/SELECT pg_notify/,
163+
~r/schema_migrations/
164+
]
164165

165166
def obfuscate(sql, adapter) when is_binary(sql) do
166167
sql
167168
|> String.replace(~r/\s+/, " ")
168-
|> String.replace(@escape_quotes, "")
169-
|> String.replace(@squote_data, "'?'")
169+
|> String.replace(escape_quotes(), "")
170+
|> String.replace(squote_data(), "'?'")
170171
|> maybe_replace_dquote(adapter)
171-
|> String.replace(@number_data, "?")
172+
|> String.replace(number_data(), "?")
172173
|> String.trim()
173174
end
174175

175176
defp maybe_replace_dquote(sql, adapter) do
176-
if Regex.match?(@double_quoters, to_string(adapter)) do
177+
if Regex.match?(double_quoters(), to_string(adapter)) do
177178
sql
178179
else
179-
String.replace(sql, @dquote_data, "\"?\"")
180+
String.replace(sql, dquote_data(), "\"?\"")
180181
end
181182
end
182183
end

0 commit comments

Comments
 (0)