1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Telemetry.Logger do
6 @moduledoc "Transforms Pleroma telemetry events to logs"
11 [:pleroma, :connection_pool, :reclaim, :start],
12 [:pleroma, :connection_pool, :reclaim, :stop],
13 [:pleroma, :connection_pool, :provision_failure],
14 [:pleroma, :connection_pool, :client, :dead],
15 [:pleroma, :connection_pool, :client, :add],
16 [:pleroma, :repo, :query]
19 :telemetry.attach_many(
22 &Pleroma.Telemetry.Logger.handle_event/4,
27 # Passing anonymous functions instead of strings to logger is intentional,
28 # that way strings won't be concatenated if the message is going to be thrown
29 # out anyway due to higher log level configured
32 [:pleroma, :connection_pool, :reclaim, :start],
34 %{max_connections: max_connections, reclaim_max: reclaim_max},
38 "Connection pool is exhausted (reached #{max_connections} connections). Starting idle connection cleanup to reclaim as much as #{reclaim_max} connections"
43 [:pleroma, :connection_pool, :reclaim, :stop],
44 %{reclaimed_count: 0},
49 "Connection pool failed to reclaim any connections due to all of them being in use. It will have to drop requests for opening connections to new hosts"
54 [:pleroma, :connection_pool, :reclaim, :stop],
55 %{reclaimed_count: reclaimed_count},
59 Logger.debug(fn -> "Connection pool cleaned up #{reclaimed_count} idle connections" end)
63 [:pleroma, :connection_pool, :provision_failure],
69 "Connection pool had to refuse opening a connection to #{key} due to connection limit exhaustion"
74 [:pleroma, :connection_pool, :client, :dead],
75 %{client_pid: client_pid, reason: reason},
80 "Pool worker for #{key}: Client #{inspect(client_pid)} died before releasing the connection with #{inspect(reason)}"
85 [:pleroma, :connection_pool, :client, :add],
86 %{clients: [_, _ | _] = clients},
87 %{key: key, protocol: :http},
91 "Pool worker for #{key}: #{length(clients)} clients are using an HTTP1 connection at the same time, head-of-line blocking might occur."
95 def handle_event([:pleroma, :connection_pool, :client, :add], _, _, _), do: :ok
98 [:pleroma, :repo, :query] = _name,
99 %{query_time: query_time} = measurements,
100 %{source: source} = metadata,
103 logging_config = Pleroma.Config.get([:telemetry, :slow_queries_logging], [])
105 if logging_config[:enabled] &&
106 logging_config[:min_duration] &&
107 query_time > logging_config[:min_duration] and
108 (is_nil(logging_config[:exclude_sources]) or
109 source not in logging_config[:exclude_sources]) do
110 log_slow_query(measurements, metadata, config)
117 %{query_time: query_time} = _measurements,
118 %{source: _source, query: query, params: query_params, repo: repo} = _metadata,
122 with {:ok, %{rows: explain_result_rows}} <-
123 repo.query("EXPLAIN " <> query, query_params, log: false) do
124 Enum.map_join(explain_result_rows, "\n", & &1)
127 {:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
130 Enum.filter(stacktrace, fn
131 {__MODULE__, _, _, _} ->
137 |> String.starts_with?("Elixir.Pleroma.")
144 Total time: #{round(query_time / 1_000)} ms
148 #{inspect(query_params, limit: :infinity)}
152 #{Exception.format_stacktrace(pleroma_stacktrace)}