384c70fbc69018632130cd193e43bb660517b08b
[anni] / lib / pleroma / telemetry / logger.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Telemetry.Logger do
6   @moduledoc "Transforms Pleroma telemetry events to logs"
7
8   require Logger
9
10   @events [
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   ]
17   def attach do
18     :telemetry.attach_many("pleroma-logger", @events, &handle_event/4, [])
19   end
20
21   # Passing anonymous functions instead of strings to logger is intentional,
22   # that way strings won't be concatenated if the message is going to be thrown
23   # out anyway due to higher log level configured
24
25   def handle_event(
26         [:pleroma, :connection_pool, :reclaim, :start],
27         _,
28         %{max_connections: max_connections, reclaim_max: reclaim_max},
29         _
30       ) do
31     Logger.debug(fn ->
32       "Connection pool is exhausted (reached #{max_connections} connections). Starting idle connection cleanup to reclaim as much as #{reclaim_max} connections"
33     end)
34   end
35
36   def handle_event(
37         [:pleroma, :connection_pool, :reclaim, :stop],
38         %{reclaimed_count: 0},
39         _,
40         _
41       ) do
42     Logger.error(fn ->
43       "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"
44     end)
45   end
46
47   def handle_event(
48         [:pleroma, :connection_pool, :reclaim, :stop],
49         %{reclaimed_count: reclaimed_count},
50         _,
51         _
52       ) do
53     Logger.debug(fn -> "Connection pool cleaned up #{reclaimed_count} idle connections" end)
54   end
55
56   def handle_event(
57         [:pleroma, :connection_pool, :provision_failure],
58         %{opts: [key | _]},
59         _,
60         _
61       ) do
62     Logger.error(fn ->
63       "Connection pool had to refuse opening a connection to #{key} due to connection limit exhaustion"
64     end)
65   end
66
67   def handle_event(
68         [:pleroma, :connection_pool, :client, :dead],
69         %{client_pid: client_pid, reason: reason},
70         %{key: key},
71         _
72       ) do
73     Logger.warn(fn ->
74       "Pool worker for #{key}: Client #{inspect(client_pid)} died before releasing the connection with #{inspect(reason)}"
75     end)
76   end
77
78   def handle_event(
79         [:pleroma, :connection_pool, :client, :add],
80         %{clients: [_, _ | _] = clients},
81         %{key: key, protocol: :http},
82         _
83       ) do
84     Logger.info(fn ->
85       "Pool worker for #{key}: #{length(clients)} clients are using an HTTP1 connection at the same time, head-of-line blocking might occur."
86     end)
87   end
88
89   def handle_event([:pleroma, :connection_pool, :client, :add], _, _, _), do: :ok
90 end