From bfdbd4983e8c299262ccabc9e8b4e46fd11259f2 Mon Sep 17 00:00:00 2001 From: Oleksii Sholik Date: Tue, 24 Dec 2024 13:46:07 +0200 Subject: [PATCH] Do not start telemetry_poller when no metric exports are configured (#2190) While I was testing the shut down of `Electric.Connection.Supervisor`, I saw errors getting logged when one of telemetry_poller's probes timed out on a `GenServer.call()` to the connection manager. The thing is, we didn't even need telemetry poller running in that scenario. --- packages/sync-service/lib/electric/config.ex | 12 ++++++++++++ packages/sync-service/lib/electric/telemetry.ex | 12 ++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/sync-service/lib/electric/config.ex b/packages/sync-service/lib/electric/config.ex index 30aec0442f..e339cf781f 100644 --- a/packages/sync-service/lib/electric/config.ex +++ b/packages/sync-service/lib/electric/config.ex @@ -109,6 +109,18 @@ defmodule Electric.Config do Application.fetch_env!(:electric, key) end + @doc """ + True when at least one metric exporter is enabled. + + This function is used to skip starting the Electric.Telemetry supervisor when there's no need + to capture periodic measurements. Useful in the dev and test environments. + """ + def telemetry_export_enabled? do + not is_nil(Electric.Config.get_env(:telemetry_statsd_host)) or + not is_nil(Electric.Config.get_env(:prometheus_port)) or + Electric.Config.get_env(:call_home_telemetry?) + end + @doc ~S""" Parse a PostgreSQL URI into a keyword list. diff --git a/packages/sync-service/lib/electric/telemetry.ex b/packages/sync-service/lib/electric/telemetry.ex index c221c4df48..5341548e07 100644 --- a/packages/sync-service/lib/electric/telemetry.ex +++ b/packages/sync-service/lib/electric/telemetry.ex @@ -6,13 +6,17 @@ defmodule Electric.Telemetry do require Logger def start_link(init_arg) do - Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) + if Electric.Config.telemetry_export_enabled?() do + Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) + else + # Avoid starting the telemetry supervisor and its telemetry_poller child if we're not + # intending to export periodic measurements metrics anywhere. + :ignore + end end def init(opts) do - system_metrics_poll_interval = - Electric.Config.get_env(:system_metrics_poll_interval) - + system_metrics_poll_interval = Electric.Config.get_env(:system_metrics_poll_interval) statsd_host = Electric.Config.get_env(:telemetry_statsd_host) prometheus? = not is_nil(Electric.Config.get_env(:prometheus_port)) call_home_telemetry? = Electric.Config.get_env(:call_home_telemetry?)