1 defmodule Pleroma.Web.Telemetry do
3 import Telemetry.Metrics
8 Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
15 {:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
19 Supervisor.init(children, strategy: :one_for_one)
22 defp prometheus_children do
23 config = Config.get([:instance, :export_prometheus_metrics], true)
27 {TelemetryMetricsPrometheus.Core, metrics: prometheus_metrics()},
28 Pleroma.PrometheusExporter
35 # A seperate set of metrics for distributions because phoenix dashboard does NOT handle them well
36 defp distribution_metrics do
39 "phoenix.router_dispatch.stop.duration",
40 # event_name: [:pleroma, :repo, :query, :total_time],
41 measurement: :duration,
42 unit: {:native, :second},
45 buckets: [0.1, 0.2, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
49 # Database Time Metrics
51 "pleroma.repo.query.total_time",
52 # event_name: [:pleroma, :repo, :query, :total_time],
53 measurement: :total_time,
54 unit: {:native, :millisecond},
56 buckets: [0.1, 0.2, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
60 "pleroma.repo.query.queue_time",
61 # event_name: [:pleroma, :repo, :query, :total_time],
62 measurement: :queue_time,
63 unit: {:native, :millisecond},
65 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
70 event_name: [:oban, :job, :exception],
71 measurement: :duration,
73 tag_values: fn tags -> Map.put(tags, :worker, tags.job.worker) end,
74 unit: {:native, :second},
76 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
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},
87 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
91 "oban_job_completion",
92 event_name: [:oban, :job, :stop],
93 measurement: :duration,
95 tag_values: fn tags -> Map.put(tags, :worker, tags.job.worker) end,
96 unit: {:native, :second},
98 buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
104 defp summary_metrics do
107 summary("phoenix.endpoint.stop.duration",
108 unit: {:native, :millisecond}
110 summary("phoenix.router_dispatch.stop.duration",
112 unit: {:native, :millisecond}
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}),
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")
132 def prometheus_metrics, do: summary_metrics() ++ distribution_metrics()
133 def live_dashboard_metrics, do: summary_metrics()
135 defp periodic_measurements do
137 {__MODULE__, :instance_stats, []}
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}, %{})