Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing metric gathering to the telemetry poller config #2167

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions packages/sync-service/lib/electric/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,34 @@ defmodule Electric.Config do
]}

iex> parse_postgresql_uri("postgres://super_user@localhost:7801/postgres?sslmode=yesplease")
{:error, "has invalid \"sslmode\" value: \"yesplease\""}
{:error, "invalid \"sslmode\" value: \"yesplease\""}

iex> parse_postgresql_uri("postgrex://localhost")
{:error, "has invalid URL scheme: \"postgrex\""}
{:error, "invalid URL scheme: \"postgrex\""}

iex> parse_postgresql_uri("postgresql://localhost")
{:error, "has invalid or missing username"}
{:error, "invalid or missing username"}

iex> parse_postgresql_uri("postgresql://:@localhost")
{:error, "has invalid or missing username"}
{:error, "invalid or missing username"}

iex> parse_postgresql_uri("postgresql://:password@localhost")
{:error, "has invalid or missing username"}
{:error, "invalid or missing username"}

iex> parse_postgresql_uri("postgresql://user:password")
{:error, "has invalid or missing username"}
{:error, "invalid or missing username"}

iex> parse_postgresql_uri("postgresql://user:password@")
{:error, "missing host"}

iex> parse_postgresql_uri("postgresql://user@localhost:5433/mydb?opts=-c%20synchronous_commit%3Doff&foo=bar")
{:error, "has unsupported query options: \"foo\", \"opts\""}
{:error, "unsupported query options: \"foo\", \"opts\""}

iex> parse_postgresql_uri("postgresql://electric@localhost/db?replication=database")
{:error, "has unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
{:error, "unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}

iex> parse_postgresql_uri("postgresql://electric@localhost/db?replication=off")
{:error, "has unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
{:error, "unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
"""
@spec parse_postgresql_uri(binary) :: {:ok, keyword} | {:error, binary}
def parse_postgresql_uri(uri_str) do
Expand Down Expand Up @@ -247,12 +247,12 @@ defmodule Electric.Config do
def parse_postgresql_uri!(uri_str) do
case parse_postgresql_uri(uri_str) do
{:ok, results} -> results
{:error, reason} -> raise reason
{:error, message} -> raise Dotenvy.Error, message: message
end
end

defp validate_url_scheme(scheme) when scheme in ["postgres", "postgresql"], do: :ok
defp validate_url_scheme(scheme), do: {:error, "has invalid URL scheme: #{inspect(scheme)}"}
defp validate_url_scheme(scheme), do: {:error, "invalid URL scheme: #{inspect(scheme)}"}

defp validate_url_host(str) do
if is_binary(str) and String.trim(str) != "" do
Expand All @@ -268,7 +268,7 @@ defmodule Electric.Config do
false <- String.trim(username) == "" do
{:ok, {username, password}}
else
_ -> {:error, "has invalid or missing username"}
_ -> {:error, "invalid or missing username"}
end
end

Expand All @@ -292,18 +292,18 @@ defmodule Electric.Config do

%{"sslmode" => sslmode} when sslmode in ~w[verify-ca verify-full] ->
{:error,
"has unsupported \"sslmode\" value #{inspect(sslmode)}. Consider using the DATABASE_REQUIRE_SSL configuration option"}
"unsupported \"sslmode\" value #{inspect(sslmode)}. Consider using the DATABASE_REQUIRE_SSL configuration option"}

%{"sslmode" => sslmode} ->
{:error, "has invalid \"sslmode\" value: #{inspect(sslmode)}"}
{:error, "invalid \"sslmode\" value: #{inspect(sslmode)}"}

%{"replication" => _} ->
{:error,
"has unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}
"unsupported \"replication\" query option. Electric opens both a replication connection and regular connections to Postgres as needed"}

map ->
{:error,
"has unsupported query options: " <>
"unsupported query options: " <>
(map |> Map.keys() |> Enum.sort() |> Enum.map_join(", ", &inspect/1))}
end
end
Expand All @@ -321,7 +321,7 @@ defmodule Electric.Config do
end

def parse_log_level(str) do
{:error, "has invalid value: #{inspect(str)}. Must be one of #{inspect(@public_log_levels)}"}
{:error, "invalid log level: #{inspect(str)}. Must be one of #{inspect(@public_log_levels)}"}
end

def parse_log_level!(str) when str in @log_levels, do: String.to_existing_atom(str)
Expand All @@ -334,7 +334,7 @@ defmodule Electric.Config do
def parse_telemetry_url(str) do
case URI.new(str) do
{:ok, %URI{scheme: scheme}} when scheme in ["http", "https"] -> {:ok, str}
_ -> {:error, "has invalid URL format: \"#{str}\""}
_ -> {:error, "invalid URL format: \"#{str}\""}
end
end

Expand All @@ -356,7 +356,7 @@ defmodule Electric.Config do
true <- suffix == "" or suffix in @time_units do
{:ok, trunc(num * time_multiplier(suffix))}
else
_ -> {:error, "has invalid value: #{inspect(str)}. Must be one of #{inspect(@time_units)}"}
_ -> {:error, "invalid time unit: #{inspect(str)}. Must be one of #{inspect(@time_units)}"}
end
end

Expand Down
14 changes: 13 additions & 1 deletion packages/sync-service/lib/electric/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ defmodule Electric.Telemetry do
otel_metrics? = not is_nil(Application.get_env(:otel_metric_exporter, :otlp_endpoint))

[
# The telemetry_poller application starts its own poller by default but we disable that
# and add its default measurements to the list of our custom ones. This allows for all
# periodic measurements to be defined in one place.
{:telemetry_poller,
measurements: periodic_measurements(opts),
period: system_metrics_poll_interval,
Expand Down Expand Up @@ -196,12 +199,16 @@ defmodule Electric.Telemetry do
[
last_value("system.cpu.core_count"),
last_value("system.cpu.utilization.total"),
last_value("electric.postgres.replication.wal_size", unit: :byte),
last_value("electric.storage.used", unit: {:byte, :kilobyte}),
last_value("electric.shapes.total_shapes.count"),
last_value("vm.memory.total", unit: :byte),
last_value("vm.memory.processes_used", unit: :byte),
last_value("vm.memory.binary", unit: :byte),
last_value("vm.memory.ets", unit: :byte),
last_value("vm.system_counts.process_count"),
last_value("vm.system_counts.atom_count"),
last_value("vm.system_counts.port_count"),
last_value("vm.total_run_queue_lengths.total"),
last_value("vm.total_run_queue_lengths.cpu"),
last_value("vm.total_run_queue_lengths.io"),
Expand Down Expand Up @@ -239,7 +246,12 @@ defmodule Electric.Telemetry do
stack_id = Keyword.fetch!(opts, :stack_id)

[
# A module, function and arguments to be invoked periodically.
# Default measurements included with the telemetry_poller application:
:memory,
icehaunter marked this conversation as resolved.
Show resolved Hide resolved
:total_run_queue_lengths,
:system_counts,

# Our custom measurements:
{__MODULE__, :uptime_event, []},
{__MODULE__, :count_shapes, [stack_id]},
{__MODULE__, :cpu_utilization, []},
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-service/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule Electric.MixProject do
{:sentry, "~> 10.0"},
{:telemetry_metrics_prometheus_core, "~> 1.1"},
{:telemetry_metrics_statsd, "~> 0.7"},
{:telemetry_poller, "~> 1.1"},
{:telemetry_poller, "~> 1.1", runtime: false},
{:tls_certificate_check, "~> 1.23"},
{:tz, "~> 0.27"}
],
Expand Down
Loading