ConnectionPool: Log possible HTTP1 blocks
[akkoma] / lib / pleroma / telemetry / logger.ex
1 defmodule Pleroma.Telemetry.Logger do
2 @moduledoc "Transforms Pleroma telemetry events to logs"
3
4 require Logger
5
6 @events [
7 [:pleroma, :connection_pool, :reclaim, :start],
8 [:pleroma, :connection_pool, :reclaim, :stop],
9 [:pleroma, :connection_pool, :provision_failure],
10 [:pleroma, :connection_pool, :client, :dead],
11 [:pleroma, :connection_pool, :client, :add]
12 ]
13 def attach do
14 :telemetry.attach_many("pleroma-logger", @events, &handle_event/4, [])
15 end
16
17 # Passing anonymous functions instead of strings to logger is intentional,
18 # that way strings won't be concatenated if the message is going to be thrown
19 # out anyway due to higher log level configured
20
21 def handle_event(
22 [:pleroma, :connection_pool, :reclaim, :start],
23 _,
24 %{max_connections: max_connections, reclaim_max: reclaim_max},
25 _
26 ) do
27 Logger.debug(fn ->
28 "Connection pool is exhausted (reached #{max_connections} connections). Starting idle connection cleanup to reclaim as much as #{
29 reclaim_max
30 } connections"
31 end)
32 end
33
34 def handle_event(
35 [:pleroma, :connection_pool, :reclaim, :stop],
36 %{reclaimed_count: 0},
37 _,
38 _
39 ) do
40 Logger.error(fn ->
41 "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"
42 end)
43 end
44
45 def handle_event(
46 [:pleroma, :connection_pool, :reclaim, :stop],
47 %{reclaimed_count: reclaimed_count},
48 _,
49 _
50 ) do
51 Logger.debug(fn -> "Connection pool cleaned up #{reclaimed_count} idle connections" end)
52 end
53
54 def handle_event(
55 [:pleroma, :connection_pool, :provision_failure],
56 %{opts: [key | _]},
57 _,
58 _
59 ) do
60 Logger.error(fn ->
61 "Connection pool had to refuse opening a connection to #{key} due to connection limit exhaustion"
62 end)
63 end
64
65 def handle_event(
66 [:pleroma, :connection_pool, :client, :dead],
67 %{client_pid: client_pid, reason: reason},
68 %{key: key},
69 _
70 ) do
71 Logger.warn(fn ->
72 "Pool worker for #{key}: Client #{inspect(client_pid)} died before releasing the connection with #{
73 inspect(reason)
74 }"
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