Merge pull request 'Correct og:description tag in static-fe' (#373) from sfr/akkoma...
[akkoma] / lib / pleroma / web / telemetry.ex
1 defmodule Pleroma.Web.Telemetry do
2 use Supervisor
3 import Telemetry.Metrics
4 alias Pleroma.Stats
5 alias Pleroma.Config
6
7 def start_link(arg) do
8 Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
9 end
10
11 @impl true
12 def init(_arg) do
13 children =
14 [
15 {:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
16 ] ++
17 prometheus_children()
18
19 Supervisor.init(children, strategy: :one_for_one)
20 end
21
22 defp prometheus_children do
23 config = Config.get([:instance, :export_prometheus_metrics], true)
24
25 if config do
26 [
27 {TelemetryMetricsPrometheus.Core, metrics: prometheus_metrics()},
28 Pleroma.PrometheusExporter
29 ]
30 else
31 []
32 end
33 end
34
35 # A seperate set of metrics for distributions because phoenix dashboard does NOT handle them well
36 defp distribution_metrics do
37 [
38 distribution(
39 "phoenix.router_dispatch.stop.duration",
40 # event_name: [:pleroma, :repo, :query, :total_time],
41 measurement: :duration,
42 unit: {:native, :second},
43 tags: [:route],
44 reporter_options: [
45 buckets: [0.1, 0.2, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
46 ]
47 ),
48
49 # Database Time Metrics
50 distribution(
51 "pleroma.repo.query.total_time",
52 # event_name: [:pleroma, :repo, :query, :total_time],
53 measurement: :total_time,
54 unit: {:native, :millisecond},
55 reporter_options: [
56 buckets: [0.1, 0.2, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
57 ]
58 ),
59 distribution(
60 "pleroma.repo.query.queue_time",
61 # event_name: [:pleroma, :repo, :query, :total_time],
62 measurement: :queue_time,
63 unit: {:native, :millisecond},
64 reporter_options: [
65 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
66 ]
67 ),
68 distribution(
69 "oban_job_exception",
70 event_name: [:oban, :job, :exception],
71 measurement: :duration,
72 tags: [:worker],
73 tag_values: fn tags -> Map.put(tags, :worker, tags.job.worker) end,
74 unit: {:native, :second},
75 reporter_options: [
76 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
77 ]
78 ),
79 distribution(
80 "tesla_request_completed",
81 event_name: [:tesla, :request, :stop],
82 measurement: :duration,
83 tags: [:response_code],
84 tag_values: fn tags -> Map.put(tags, :response_code, tags.env.status) end,
85 unit: {:native, :second},
86 reporter_options: [
87 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
88 ]
89 ),
90 distribution(
91 "oban_job_completion",
92 event_name: [:oban, :job, :stop],
93 measurement: :duration,
94 tags: [:worker],
95 tag_values: fn tags -> Map.put(tags, :worker, tags.job.worker) end,
96 unit: {:native, :second},
97 reporter_options: [
98 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
99 ]
100 )
101 ]
102 end
103
104 defp summary_metrics do
105 [
106 # Phoenix Metrics
107 summary("phoenix.endpoint.stop.duration",
108 unit: {:native, :millisecond}
109 ),
110 summary("phoenix.router_dispatch.stop.duration",
111 tags: [:route],
112 unit: {:native, :millisecond}
113 ),
114 summary("pleroma.repo.query.total_time", unit: {:native, :millisecond}),
115 summary("pleroma.repo.query.decode_time", unit: {:native, :millisecond}),
116 summary("pleroma.repo.query.query_time", unit: {:native, :millisecond}),
117 summary("pleroma.repo.query.queue_time", unit: {:native, :millisecond}),
118 summary("pleroma.repo.query.idle_time", unit: {:native, :millisecond}),
119
120 # VM Metrics
121 summary("vm.memory.total", unit: {:byte, :kilobyte}),
122 summary("vm.total_run_queue_lengths.total"),
123 summary("vm.total_run_queue_lengths.cpu"),
124 summary("vm.total_run_queue_lengths.io"),
125 last_value("pleroma.local_users.total"),
126 last_value("pleroma.domains.total"),
127 last_value("pleroma.local_statuses.total"),
128 last_value("pleroma.remote_users.total")
129 ]
130 end
131
132 def prometheus_metrics, do: summary_metrics() ++ distribution_metrics()
133 def live_dashboard_metrics, do: summary_metrics()
134
135 defp periodic_measurements do
136 [
137 {__MODULE__, :instance_stats, []}
138 ]
139 end
140
141 def instance_stats do
142 stats = Stats.get_stats()
143 :telemetry.execute([:pleroma, :local_users], %{total: stats.user_count}, %{})
144 :telemetry.execute([:pleroma, :domains], %{total: stats.domain_count}, %{})
145 :telemetry.execute([:pleroma, :local_statuses], %{total: stats.status_count}, %{})
146 :telemetry.execute([:pleroma, :remote_users], %{total: stats.remote_user_count}, %{})
147 end
148 end