Configurability of slow queries logging ([:pleroma, :telemetry, :slow_queries_logging...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Sun, 26 Dec 2021 19:49:00 +0000 (22:49 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Sun, 26 Dec 2021 19:49:00 +0000 (22:49 +0300)
config/config.exs
lib/pleroma/telemetry/logger.ex

index b50c910b12b985ed1c8f6311fc6b8a25c6f8c550..656778007e2a2db231236e44393ddf94ad87f569 100644 (file)
@@ -148,6 +148,8 @@ config :pleroma, Pleroma.Web.Endpoint,
   ]
 
 # Configures Elixir's Logger
+config :logger, truncate: 65536
+
 config :logger, :console,
   level: :debug,
   format: "\n$time $metadata[$level] $message\n",
@@ -852,6 +854,9 @@ config :pleroma, ConcurrentLimiter, [
   {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
 # of this file so it overrides the configuration defined above.
 import_config "#{Mix.env()}.exs"
index c079f34f2657150eb28e0918bc5d339cbe6d2c0b..0f73ecc02e6c5bd2a4b078dfcc1c00b263e80683 100644 (file)
@@ -100,19 +100,34 @@ defmodule Pleroma.Telemetry.Logger do
 
   def handle_event(
         [:pleroma, :repo, :query] = _name,
-        %{query_time: query_time} = _measurements,
-        %{source: source, query: query, params: query_params, repo: repo} = _metadata,
-        _config
-      )
-      when query_time > 500_000 and source not in [nil, "oban_jobs"] do
-    {:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
+        %{query_time: query_time} = measurements,
+        %{source: source} = metadata,
+        config
+      ) do
+    logging_config = Pleroma.Config.get([:telemetry, :slow_queries_logging], [])
+
+    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 =
       with {:ok, %{rows: explain_result_rows}} <-
              repo.query("EXPLAIN " <> query, query_params, log: false) do
         Enum.map_join(explain_result_rows, "\n", & &1)
       end
 
+    {:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
+
     pleroma_stacktrace =
       Enum.filter(stacktrace, fn
         {__MODULE__, _, _, _} ->
@@ -128,11 +143,11 @@ defmodule Pleroma.Telemetry.Logger do
       """
       Slow query!
 
-      Total time: #{query_time / 1_000}ms
+      Total time: #{round(query_time / 1_000)} ms
 
       #{query}
 
-      #{inspect(query_params)}
+      #{inspect(query_params, limit: :infinity)}
 
       #{sql_explain}
 
@@ -140,6 +155,4 @@ defmodule Pleroma.Telemetry.Logger do
       """
     end)
   end
-
-  def handle_event([:pleroma, :repo, :query], _measurements, _metadata, _config), do: :ok
 end