Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Implement extended metrics for persistor client (#5791)

Adrian Gruntkowskie097afea8f51
diff --git a/lib/plausible/application.ex b/lib/plausible/application.ex
index 483841ca8d..234d9b8c05 100644
--- a/lib/plausible/application.ex
+++ b/lib/plausible/application.ex
@@ -196,6 +196,7 @@ defmodule Plausible.Application do
setup_request_logging()
setup_sentry()
setup_opentelemetry()
+ Plausible.Ingestion.Persistor.TelemetryHandler.install()
setup_geolocation()
Location.load_all()
diff --git a/lib/plausible/ingestion/persistor/telemetry_handler.ex b/lib/plausible/ingestion/persistor/telemetry_handler.ex
new file mode 100644
index 0000000000..c0567f5fa8
--- /dev/null
+++ b/lib/plausible/ingestion/persistor/telemetry_handler.ex
@@ -0,0 +1,175 @@
+defmodule Plausible.Ingestion.Persistor.TelemetryHandler do
+ @moduledoc """
+ Sets up telemetry for remote calls to persistor via `Finch.Telemetry` events.
+
+ Emits persistor specific telemetry events for tracking metrics.
+ """
+
+ @finch_request_event [:finch, :request, :stop]
+ @finch_connect_event [:finch, :connect, :stop]
+ @finch_send_event [:finch, :send, :stop]
+ @finch_receive_event [:finch, :recv, :stop]
+
+ @persistor_request_event [:persistor, :remote, :request]
+ @persistor_connect_event [:persistor, :remote, :connect]
+ @persistor_send_event [:persistor, :remote, :send]
+ @persistor_receive_event [:persistor, :remote, :receive]
+
+ @telemetry_events [
+ @finch_request_event,
+ @finch_connect_event,
+ @finch_send_event,
+ @finch_receive_event
+ ]
+
+ @telemetry_handler &__MODULE__.handle_event/4
+
+ def request_event(), do: @persistor_request_event
+ def connect_event(), do: @persistor_connect_event
+ def send_event(), do: @persistor_send_event
+ def receive_event(), do: @persistor_receive_event
+
+ @spec install() :: :ok
+ def install() do
+ if persistor_backend() in [
+ Plausible.Ingestion.Persistor.Remote,
+ Plausible.Ingestion.Persistor.EmbeddedWithRelay
+ ] do
+ persistor_host =
+ persistor_url()
+ |> URI.parse()
+ |> Map.fetch!(:host)
+
+ persistor_count = persistor_count()
+
+ if is_binary(persistor_host) do
+ :ok =
+ :telemetry.attach_many(
+ "persistor-remote-finch-metrics",
+ @telemetry_events,
+ @telemetry_handler,
+ %{remote_host: persistor_host, pool_size: persistor_count}
+ )
+ else
+ :ok
+ end
+ else
+ :ok
+ end
+ end
+
+ @spec handle_event([atom()], any(), map(), any()) :: :ok
+ def handle_event(
+ @finch_request_event,
+ %{duration: duration},
+ %{request: request, result: result},
+ config
+ ) do
+ if request.host == config.remote_host do
+ result =
+ case result do
+ {:ok, %Finch.Response{status: status}} when status >= 200 and status < 300 ->
+ "ok"
+
+ {:ok, %Finch.Response{}} ->
+ "bad_status"
+
+ {:ok, _} ->
+ "stream"
+
+ {:error, %{reason: :timeout}} ->
+ "timeout_error"
+
+ {:error, _} ->
+ "unknown_error"
+ end
+
+ :telemetry.execute(
+ @persistor_request_event,
+ %{duration: duration},
+ %{result: result, path: normalize_path(request.path)}
+ )
+ end
+
+ :ok
+ end
+
+ def handle_event(
+ @finch_connect_event,
+ %{duration: duration},
+ %{host: host} = meta,
+ config
+ ) do
+ if host == config.remote_host do
+ :telemetry.execute(
+ @persistor_connect_event,
+ %{duration: duration},
+ %{status: if(meta[:error], do: "error", else: "ok")}
+ )
+ end
+
+ :ok
+ end
+
+ def handle_event(
+ @finch_send_event,
+ %{duration: duration},
+ %{request: request} = meta,
+ config
+ ) do
+ if request.host == config.remote_host do
+ :telemetry.execute(
+ @persistor_send_event,
+ %{duration: duration},
+ %{status: if(meta[:error], do: "error", else: "ok")}
+ )
+ end
+
+ :ok
+ end
+
+ def handle_event(
+ @finch_receive_event,
+ %{duration: duration},
+ %{request: request, status: status} = meta,
+ config
+ ) do
+ if request.host == config.remote_host do
+ :telemetry.execute(
+ @persistor_receive_event,
+ %{duration: duration},
+ %{
+ status: if(meta[:error] || status < 200 || status > 299, do: "error", else: "ok")
+ }
+ )
+ end
+
+ :ok
+ end
+
+ defp normalize_path(path) do
+ if path == "/event" do
+ "/event"
+ else
+ "unknown"
+ end
+ end
+
+ defp persistor_url() do
+ :plausible
+ |> Application.fetch_env!(Plausible.Ingestion.Persistor.Remote)
+ |> Keyword.fetch!(:url)
+ end
+
+ defp persistor_backend() do
+ :plausible
+ |> Application.fetch_env!(Plausible.Ingestion.Persistor)
+ |> Keyword.fetch!(:backend)
+ end
+
+ defp persistor_count() do
+ :plausible
+ |> Application.fetch_env!(Plausible.Ingestion.Persistor.Remote)
+ |> Keyword.fetch!(:count)
+ end
+end
diff --git a/lib/plausible/telemetry/plausible_metrics.ex b/lib/plausible/telemetry/plausible_metrics.ex
index c6b918040a..4b5e3af1fd 100644
--- a/lib/plausible/telemetry/plausible_metrics.ex
+++ b/lib/plausible/telemetry/plausible_metrics.ex
@@ -6,6 +6,7 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics do
use PromEx.Plugin
alias Plausible.Site
alias Plausible.Ingestion
+ alias Plausible.Ingestion.Persistor
on_ee do
alias Plausible.InstallationSupport
@@ -209,6 +210,7 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics do
)
)
]
+ |> Enum.concat(persistor_metrics(metric_prefix))
|> Enum.reject(&is_nil/1)
)
end
@@ -263,6 +265,51 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics do
result
end
+ defp persistor_metrics(metric_prefix) do
+ [
+ distribution(
+ metric_prefix ++ [:persistor, :remote, :request, :duration, :millisecond],
+ event_name: Persistor.TelemetryHandler.request_event(),
+ reporter_options: [
+ buckets: [10, 100, 500, 1_000, 5_000, 10_000, 30_000]
+ ],
+ unit: {:native, :millisecond},
+ measurement: :duration,
+ tags: [:result, :path]
+ ),
+ distribution(
+ metric_prefix ++ [:persistor, :remote, :connect, :duration, :millisecond],
+ event_name: Persistor.TelemetryHandler.connect_event(),
+ reporter_options: [
+ buckets: [1, 10, 50, 100, 500, 1_000, 10_000]
+ ],
+ unit: {:native, :millisecond},
+ measurement: :duration,
+ tags: [:status]
+ ),
+ distribution(
+ metric_prefix ++ [:persistor, :remote, :send, :duration, :millisecond],
+ event_name: Persistor.TelemetryHandler.send_event(),
+ reporter_options: [
+ buckets: [1, 10, 50, 100, 500, 1_000, 10_000]
+ ],
+ unit: {:native, :millisecond},
+ measurement: :duration,
+ tags: [:status]
+ ),
+ distribution(
+ metric_prefix ++ [:persistor, :remote, :receive, :duration, :millisecond],
+ event_name: Persistor.TelemetryHandler.receive_event(),
+ reporter_options: [
+ buckets: [10, 100, 500, 1_000, 5_000, 10_000, 30_000]
+ ],
+ unit: {:native, :millisecond},
+ measurement: :duration,
+ tags: [:status]
+ )
+ ]
+ end
+
defp time_it(fun) do
start = System.monotonic_time()
result = fun.()