Merge branch 'develop' into 'fix/prune-hashtags'
[akkoma] / lib / pleroma / telemetry / logger.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 #{
33 reclaim_max
34 } connections"
35 end)
36 end
37
38 def handle_event(
39 [:pleroma, :connection_pool, :reclaim, :stop],
40 %{reclaimed_count: 0},
41 _,
42 _
43 ) do
44 Logger.error(fn ->
45 "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"
46 end)
47 end
48
49 def handle_event(
50 [:pleroma, :connection_pool, :reclaim, :stop],
51 %{reclaimed_count: reclaimed_count},
52 _,
53 _
54 ) do
55 Logger.debug(fn -> "Connection pool cleaned up #{reclaimed_count} idle connections" end)
56 end
57
58 def handle_event(
59 [:pleroma, :connection_pool, :provision_failure],
60 %{opts: [key | _]},
61 _,
62 _
63 ) do
64 Logger.error(fn ->
65 "Connection pool had to refuse opening a connection to #{key} due to connection limit exhaustion"
66 end)
67 end
68
69 def handle_event(
70 [:pleroma, :connection_pool, :client, :dead],
71 %{client_pid: client_pid, reason: reason},
72 %{key: key},
73 _
74 ) do
75 Logger.warn(fn ->
76 "Pool worker for #{key}: Client #{inspect(client_pid)} died before releasing the connection with #{
77 inspect(reason)
78 }"
79 end)
80 end
81
82 def handle_event(
83 [:pleroma, :connection_pool, :client, :add],
84 %{clients: [_, _ | _] = clients},
85 %{key: key, protocol: :http},
86 _
87 ) do
88 Logger.info(fn ->
89 "Pool worker for #{key}: #{length(clients)} clients are using an HTTP1 connection at the same time, head-of-line blocking might occur."
90 end)
91 end
92
93 def handle_event([:pleroma, :connection_pool, :client, :add], _, _, _), do: :ok
94 end