2022-11-12 03:14:16 -07:00
|
|
|
defmodule Pleroma.Web.Telemetry do
|
|
|
|
use Supervisor
|
|
|
|
import Telemetry.Metrics
|
2022-11-12 09:13:39 -07:00
|
|
|
alias Pleroma.Stats
|
2023-01-01 11:32:14 -07:00
|
|
|
alias Pleroma.Config
|
2022-11-12 03:14:16 -07:00
|
|
|
|
|
|
|
def start_link(arg) do
|
|
|
|
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def init(_arg) do
|
2023-01-01 11:32:14 -07:00
|
|
|
children =
|
|
|
|
[
|
|
|
|
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
|
|
|
|
] ++
|
|
|
|
prometheus_children()
|
2022-11-12 03:14:16 -07:00
|
|
|
|
|
|
|
Supervisor.init(children, strategy: :one_for_one)
|
|
|
|
end
|
|
|
|
|
2023-01-01 11:32:14 -07:00
|
|
|
defp prometheus_children do
|
|
|
|
config = Config.get([:instance, :export_prometheus_metrics], true)
|
|
|
|
|
|
|
|
if config do
|
|
|
|
[
|
|
|
|
{TelemetryMetricsPrometheus.Core, metrics: prometheus_metrics()},
|
|
|
|
Pleroma.PrometheusExporter
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-15 20:32:51 -07:00
|
|
|
# A seperate set of metrics for distributions because phoenix dashboard does NOT handle them well
|
2022-12-14 19:02:07 -07:00
|
|
|
defp distribution_metrics do
|
2022-11-12 03:14:16 -07:00
|
|
|
[
|
|
|
|
distribution(
|
|
|
|
"phoenix.router_dispatch.stop.duration",
|
|
|
|
# event_name: [:pleroma, :repo, :query, :total_time],
|
|
|
|
measurement: :duration,
|
|
|
|
unit: {:native, :second},
|
|
|
|
tags: [:route],
|
|
|
|
reporter_options: [
|
2022-11-12 03:23:44 -07:00
|
|
|
buckets: [0.1, 0.2, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
|
2022-11-12 03:14:16 -07:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
# Database Time Metrics
|
|
|
|
distribution(
|
|
|
|
"pleroma.repo.query.total_time",
|
|
|
|
# event_name: [:pleroma, :repo, :query, :total_time],
|
|
|
|
measurement: :total_time,
|
|
|
|
unit: {:native, :millisecond},
|
|
|
|
reporter_options: [
|
2022-11-12 03:23:44 -07:00
|
|
|
buckets: [0.1, 0.2, 0.5, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000]
|
2022-11-12 03:14:16 -07:00
|
|
|
]
|
|
|
|
),
|
|
|
|
distribution(
|
|
|
|
"pleroma.repo.query.queue_time",
|
|
|
|
# event_name: [:pleroma, :repo, :query, :total_time],
|
|
|
|
measurement: :queue_time,
|
|
|
|
unit: {:native, :millisecond},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
|
|
|
|
]
|
|
|
|
),
|
|
|
|
distribution(
|
2022-12-14 19:02:07 -07:00
|
|
|
"oban_job_exception",
|
|
|
|
event_name: [:oban, :job, :exception],
|
2022-11-12 03:14:16 -07:00
|
|
|
measurement: :duration,
|
2022-11-12 03:54:35 -07:00
|
|
|
tags: [:worker],
|
|
|
|
tag_values: fn tags -> Map.put(tags, :worker, tags.job.worker) end,
|
2022-11-12 03:14:16 -07:00
|
|
|
unit: {:native, :second},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
|
|
|
|
]
|
2022-11-12 04:42:53 -07:00
|
|
|
),
|
|
|
|
distribution(
|
2022-12-14 19:02:07 -07:00
|
|
|
"tesla_request_completed",
|
|
|
|
event_name: [:tesla, :request, :stop],
|
2022-11-12 04:42:53 -07:00
|
|
|
measurement: :duration,
|
2022-12-14 19:02:07 -07:00
|
|
|
tags: [:response_code],
|
|
|
|
tag_values: fn tags -> Map.put(tags, :response_code, tags.env.status) end,
|
2022-11-12 04:42:53 -07:00
|
|
|
unit: {:native, :second},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
|
|
|
|
]
|
2022-11-12 08:11:38 -07:00
|
|
|
),
|
|
|
|
distribution(
|
2022-12-14 19:02:07 -07:00
|
|
|
"oban_job_completion",
|
|
|
|
event_name: [:oban, :job, :stop],
|
2022-11-12 08:11:38 -07:00
|
|
|
measurement: :duration,
|
2022-12-14 19:02:07 -07:00
|
|
|
tags: [:worker],
|
|
|
|
tag_values: fn tags -> Map.put(tags, :worker, tags.job.worker) end,
|
2022-11-12 08:11:38 -07:00
|
|
|
unit: {:native, :second},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: [0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2.5, 5, 10]
|
|
|
|
]
|
2022-12-14 19:02:07 -07:00
|
|
|
)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2024-02-03 09:30:00 -07:00
|
|
|
# Summary metrics are currently not (yet) supported by the prometheus exporter
|
2024-02-03 10:21:09 -07:00
|
|
|
defp summary_metrics(byte_unit) do
|
2022-12-14 19:02:07 -07:00
|
|
|
[
|
|
|
|
# Phoenix Metrics
|
|
|
|
summary("phoenix.endpoint.stop.duration",
|
|
|
|
unit: {:native, :millisecond}
|
|
|
|
),
|
|
|
|
summary("phoenix.router_dispatch.stop.duration",
|
|
|
|
tags: [:route],
|
|
|
|
unit: {:native, :millisecond}
|
2022-11-12 09:13:39 -07:00
|
|
|
),
|
2022-12-14 19:02:07 -07:00
|
|
|
summary("pleroma.repo.query.total_time", unit: {:native, :millisecond}),
|
|
|
|
summary("pleroma.repo.query.decode_time", unit: {:native, :millisecond}),
|
|
|
|
summary("pleroma.repo.query.query_time", unit: {:native, :millisecond}),
|
|
|
|
summary("pleroma.repo.query.queue_time", unit: {:native, :millisecond}),
|
|
|
|
summary("pleroma.repo.query.idle_time", unit: {:native, :millisecond}),
|
|
|
|
|
|
|
|
# VM Metrics
|
2024-02-03 10:21:09 -07:00
|
|
|
summary("vm.memory.total", unit: {:byte, byte_unit}),
|
2022-12-14 19:02:07 -07:00
|
|
|
summary("vm.total_run_queue_lengths.total"),
|
|
|
|
summary("vm.total_run_queue_lengths.cpu"),
|
2024-02-03 09:30:00 -07:00
|
|
|
summary("vm.total_run_queue_lengths.io")
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2024-02-03 10:28:55 -07:00
|
|
|
defp sum_counter_pair(basename, opts) do
|
|
|
|
[
|
|
|
|
sum(basename <> ".psum", opts),
|
|
|
|
counter(basename <> ".pcount", opts)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prometheus exporter doesn't support summaries, so provide fallbacks
|
|
|
|
defp summary_fallback_metrics(byte_unit \\ :byte) do
|
|
|
|
# Summary metrics are not supported by the Prometheus exporter
|
|
|
|
# https://github.com/beam-telemetry/telemetry_metrics_prometheus_core/issues/11
|
|
|
|
# and sum metrics currently only work with integers
|
|
|
|
# https://github.com/beam-telemetry/telemetry_metrics_prometheus_core/issues/35
|
|
|
|
#
|
|
|
|
# For VM metrics this is kindof ok as they appear to always be integers
|
|
|
|
# and we can use sum + counter to get the average between polls from their change
|
|
|
|
# But for repo query times we need to use a full distribution
|
|
|
|
|
|
|
|
simple_buckets = [0, 1, 2, 4, 8, 16]
|
|
|
|
simple_buckets_quick = for t <- simple_buckets, do: t / 100.0
|
|
|
|
|
|
|
|
# Already included in distribution metrics anyway:
|
|
|
|
# phoenix.router_dispatch.stop.duration
|
|
|
|
# pleroma.repo.query.total_time
|
|
|
|
# pleroma.repo.query.queue_time
|
2024-04-16 00:02:13 -06:00
|
|
|
dist_metrics = [
|
|
|
|
distribution("phoenix.endpoint.stop.duration.fdist",
|
|
|
|
event_name: [:phoenix, :endpoint, :stop],
|
|
|
|
measurement: :duration,
|
|
|
|
unit: {:native, :millisecond},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: simple_buckets
|
|
|
|
]
|
|
|
|
),
|
|
|
|
distribution("pleroma.repo.query.decode_time.fdist",
|
|
|
|
event_name: [:pleroma, :repo, :query],
|
|
|
|
measurement: :decode_time,
|
|
|
|
unit: {:native, :millisecond},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: simple_buckets_quick
|
|
|
|
]
|
|
|
|
),
|
|
|
|
distribution("pleroma.repo.query.query_time.fdist",
|
|
|
|
event_name: [:pleroma, :repo, :query],
|
|
|
|
measurement: :query_time,
|
|
|
|
unit: {:native, :millisecond},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: simple_buckets
|
|
|
|
]
|
|
|
|
),
|
|
|
|
distribution("pleroma.repo.query.idle_time.fdist",
|
|
|
|
event_name: [:pleroma, :repo, :query],
|
|
|
|
measurement: :idle_time,
|
|
|
|
unit: {:native, :millisecond},
|
|
|
|
reporter_options: [
|
|
|
|
buckets: simple_buckets
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2024-02-03 10:28:55 -07:00
|
|
|
|
|
|
|
vm_metrics =
|
|
|
|
sum_counter_pair("vm.memory.total",
|
|
|
|
event_name: [:vm, :memory],
|
|
|
|
measurement: :total,
|
|
|
|
unit: {:byte, byte_unit}
|
|
|
|
) ++
|
|
|
|
sum_counter_pair("vm.total_run_queue_lengths.total",
|
|
|
|
event_name: [:vm, :total_run_queue_lengths],
|
|
|
|
measurement: :total
|
|
|
|
) ++
|
|
|
|
sum_counter_pair("vm.total_run_queue_lengths.cpu",
|
|
|
|
event_name: [:vm, :total_run_queue_lengths],
|
|
|
|
measurement: :cpu
|
|
|
|
) ++
|
|
|
|
sum_counter_pair("vm.total_run_queue_lengths.io.fsum",
|
|
|
|
event_name: [:vm, :total_run_queue_lengths],
|
|
|
|
measurement: :io
|
|
|
|
)
|
|
|
|
|
|
|
|
dist_metrics ++ vm_metrics
|
|
|
|
end
|
|
|
|
|
2024-02-03 09:30:00 -07:00
|
|
|
defp common_metrics do
|
|
|
|
[
|
2022-11-12 09:13:39 -07:00
|
|
|
last_value("pleroma.local_users.total"),
|
|
|
|
last_value("pleroma.domains.total"),
|
2022-12-16 10:22:26 -07:00
|
|
|
last_value("pleroma.local_statuses.total"),
|
|
|
|
last_value("pleroma.remote_users.total")
|
2022-11-12 03:14:16 -07:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2024-02-03 10:28:55 -07:00
|
|
|
def prometheus_metrics,
|
|
|
|
do: common_metrics() ++ distribution_metrics() ++ summary_fallback_metrics()
|
|
|
|
|
2024-02-03 10:21:09 -07:00
|
|
|
def live_dashboard_metrics, do: common_metrics() ++ summary_metrics(:megabyte)
|
2022-12-14 19:02:07 -07:00
|
|
|
|
2022-11-12 03:14:16 -07:00
|
|
|
defp periodic_measurements do
|
2022-11-12 09:13:39 -07:00
|
|
|
[
|
|
|
|
{__MODULE__, :instance_stats, []}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def instance_stats do
|
|
|
|
stats = Stats.get_stats()
|
|
|
|
:telemetry.execute([:pleroma, :local_users], %{total: stats.user_count}, %{})
|
|
|
|
:telemetry.execute([:pleroma, :domains], %{total: stats.domain_count}, %{})
|
|
|
|
:telemetry.execute([:pleroma, :local_statuses], %{total: stats.status_count}, %{})
|
2022-12-16 10:22:26 -07:00
|
|
|
:telemetry.execute([:pleroma, :remote_users], %{total: stats.remote_user_count}, %{})
|
2022-11-12 03:14:16 -07:00
|
|
|
end
|
|
|
|
end
|