Reduce cyclomatic complexity in database metrics collection
Extract each metric collection into separate helper functions: - collect_database_size: Database size metrics - collect_connection_stats: Connection pool statistics - collect_packets_table_stats: Table-level statistics - collect_query_performance_stats: Query performance metrics - collect_replication_lag: Replication lag metrics This breaks down the complexity 27 function into manageable pieces. Co-Authored-By: Graham <noreply@anthropic.com>
This commit is contained in:
parent
0f8b60c2f4
commit
12cf324d4c
1 changed files with 25 additions and 22 deletions
|
|
@ -122,20 +122,27 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp collect_database_metrics do
|
defp collect_database_metrics do
|
||||||
# Database size
|
collect_database_size()
|
||||||
|
collect_connection_stats()
|
||||||
|
collect_packets_table_stats()
|
||||||
|
collect_query_performance_stats()
|
||||||
|
collect_replication_lag()
|
||||||
|
rescue
|
||||||
|
e ->
|
||||||
|
Logger.debug("Error collecting PostgreSQL metrics: #{inspect(e)}")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp collect_database_size do
|
||||||
case Aprsme.Repo.query("SELECT pg_database_size(current_database()) as size") do
|
case Aprsme.Repo.query("SELECT pg_database_size(current_database()) as size") do
|
||||||
{:ok, %{rows: [[size]]}} ->
|
{:ok, %{rows: [[size]]}} ->
|
||||||
:telemetry.execute(
|
:telemetry.execute([:aprsme, :postgres, :database], %{size_bytes: size}, %{})
|
||||||
[:aprsme, :postgres, :database],
|
|
||||||
%{size_bytes: size},
|
|
||||||
%{}
|
|
||||||
)
|
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Connection stats
|
defp collect_connection_stats do
|
||||||
case Aprsme.Repo.query("""
|
case Aprsme.Repo.query("""
|
||||||
SELECT
|
SELECT
|
||||||
count(*) as total,
|
count(*) as total,
|
||||||
|
|
@ -162,8 +169,9 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
|
||||||
_ ->
|
_ ->
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Table stats for packets table
|
defp collect_packets_table_stats do
|
||||||
case Aprsme.Repo.query("""
|
case Aprsme.Repo.query("""
|
||||||
SELECT
|
SELECT
|
||||||
n_live_tup as live_tuples,
|
n_live_tup as live_tuples,
|
||||||
|
|
@ -195,8 +203,9 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
|
||||||
_ ->
|
_ ->
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Query performance stats
|
defp collect_query_performance_stats do
|
||||||
case Aprsme.Repo.query("""
|
case Aprsme.Repo.query("""
|
||||||
SELECT
|
SELECT
|
||||||
sum(calls) as total_calls,
|
sum(calls) as total_calls,
|
||||||
|
|
@ -221,26 +230,20 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
|
||||||
_ ->
|
_ ->
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Replication lag (if applicable)
|
defp collect_replication_lag do
|
||||||
case Aprsme.Repo.query("""
|
case Aprsme.Repo.query("""
|
||||||
SELECT
|
SELECT
|
||||||
extract(epoch from (now() - pg_last_xact_replay_timestamp()))::int as lag_seconds
|
extract(epoch from (now() - pg_last_xact_replay_timestamp()))::int as lag_seconds
|
||||||
WHERE pg_is_in_recovery()
|
WHERE pg_is_in_recovery()
|
||||||
""") do
|
""") do
|
||||||
{:ok, %{rows: [[lag]]}} when not is_nil(lag) ->
|
{:ok, %{rows: [[lag]]}} when not is_nil(lag) ->
|
||||||
:telemetry.execute(
|
:telemetry.execute([:aprsme, :postgres, :replication], %{lag_seconds: lag}, %{})
|
||||||
[:aprsme, :postgres, :replication],
|
|
||||||
%{lag_seconds: lag},
|
|
||||||
%{}
|
|
||||||
)
|
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
rescue
|
|
||||||
e ->
|
|
||||||
Logger.debug("Error collecting PostgreSQL metrics: #{inspect(e)}")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def collect_pgbouncer_metrics do
|
def collect_pgbouncer_metrics do
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue