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