Configurability of slow queries logging ([:pleroma, :telemetry, :slow_queries_logging]). Adjusted log messages truncation to 65 kb (was default: 8 kb). Non-truncated logging of slow query params.

This commit is contained in:
Ivan Tashkinov 2021-12-26 22:49:00 +03:00
parent e009950845
commit 3e9e7178bc
2 changed files with 28 additions and 10 deletions

View file

@ -148,6 +148,8 @@
] ]
# Configures Elixir's Logger # Configures Elixir's Logger
config :logger, truncate: 65536
config :logger, :console, config :logger, :console,
level: :debug, level: :debug,
format: "\n$time $metadata[$level] $message\n", format: "\n$time $metadata[$level] $message\n",
@ -852,6 +854,9 @@
{Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy, [max_running: 5, max_waiting: 5]} {Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy, [max_running: 5, max_waiting: 5]}
] ]
config :pleroma, :telemetry,
slow_queries_logging: [exclude_sources: [nil, "oban_jobs"], min_duration: 500_000]
# Import environment specific config. This must remain at the bottom # Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above. # of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs" import_config "#{Mix.env()}.exs"

View file

@ -100,19 +100,34 @@ def handle_event([:pleroma, :connection_pool, :client, :add], _, _, _), do: :ok
def handle_event( def handle_event(
[:pleroma, :repo, :query] = _name, [:pleroma, :repo, :query] = _name,
%{query_time: query_time} = _measurements, %{query_time: query_time} = measurements,
%{source: source, query: query, params: query_params, repo: repo} = _metadata, %{source: source} = metadata,
_config config
) ) do
when query_time > 500_000 and source not in [nil, "oban_jobs"] do logging_config = Pleroma.Config.get([:telemetry, :slow_queries_logging], [])
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
if logging_config[:min_duration] && query_time > logging_config[:min_duration] and
(is_nil(logging_config[:exclude_sources]) or
source not in logging_config[:exclude_sources]) do
log_slow_query(measurements, metadata, config)
else
:ok
end
end
defp log_slow_query(
%{query_time: query_time} = _measurements,
%{source: _source, query: query, params: query_params, repo: repo} = _metadata,
_config
) do
sql_explain = sql_explain =
with {:ok, %{rows: explain_result_rows}} <- with {:ok, %{rows: explain_result_rows}} <-
repo.query("EXPLAIN " <> query, query_params, log: false) do repo.query("EXPLAIN " <> query, query_params, log: false) do
Enum.map_join(explain_result_rows, "\n", & &1) Enum.map_join(explain_result_rows, "\n", & &1)
end end
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
pleroma_stacktrace = pleroma_stacktrace =
Enum.filter(stacktrace, fn Enum.filter(stacktrace, fn
{__MODULE__, _, _, _} -> {__MODULE__, _, _, _} ->
@ -128,11 +143,11 @@ def handle_event(
""" """
Slow query! Slow query!
Total time: #{query_time / 1_000}ms Total time: #{round(query_time / 1_000)} ms
#{query} #{query}
#{inspect(query_params)} #{inspect(query_params, limit: :infinity)}
#{sql_explain} #{sql_explain}
@ -140,6 +155,4 @@ def handle_event(
""" """
end) end)
end end
def handle_event([:pleroma, :repo, :query], _measurements, _metadata, _config), do: :ok
end end