From e897070d84497a284d4f01056d24d2dedc29b2ff Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Fri, 27 Sep 2024 13:23:46 +0300 Subject: [PATCH] fix: Repair /metrics endpoint (#10813) * fix: Repair /metrics endpoint * Return back change in the application.ex --- .../controllers/metrics_contoller_test.exs | 11 +++++ apps/indexer/lib/indexer/application.ex | 21 --------- ...in_pending_address_operations_collector.ex | 44 ++++++++++--------- 3 files changed, 34 insertions(+), 42 deletions(-) create mode 100644 apps/block_scout_web/test/block_scout_web/controllers/metrics_contoller_test.exs diff --git a/apps/block_scout_web/test/block_scout_web/controllers/metrics_contoller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/metrics_contoller_test.exs new file mode 100644 index 0000000000..e9ea7e7e55 --- /dev/null +++ b/apps/block_scout_web/test/block_scout_web/controllers/metrics_contoller_test.exs @@ -0,0 +1,11 @@ +defmodule BlockScoutWeb.MetricsControllerTest do + use BlockScoutWeb.ConnCase + + describe "/metrics page" do + test "renders /metrics page", %{conn: conn} do + conn = get(conn, "/metrics") + + assert text_response(conn, 200) + end + end +end diff --git a/apps/indexer/lib/indexer/application.ex b/apps/indexer/lib/indexer/application.ex index 78d05b37b0..cb7b9a99f8 100644 --- a/apps/indexer/lib/indexer/application.ex +++ b/apps/indexer/lib/indexer/application.ex @@ -14,29 +14,8 @@ defmodule Indexer.Application do alias Indexer.Memory alias Prometheus.Registry - @default_prometheus_collectors [ - Indexer.Prometheus.Collector.PendingBlockOperations - ] - - case Application.compile_env(:explorer, :chain_type) do - :filecoin -> - @chain_type_prometheus_collectors [ - Indexer.Prometheus.Collector.FilecoinPendingAddressOperations - ] - - _ -> - @chain_type_prometheus_collectors [] - end - - @prometheus_collectors @default_prometheus_collectors ++ - @chain_type_prometheus_collectors - @impl Application def start(_type, _args) do - for collector <- @prometheus_collectors do - Registry.register_collector(collector) - end - memory_monitor_options = case Application.get_env(:indexer, :memory_limit) do nil -> %{} diff --git a/apps/indexer/lib/indexer/prometheus/collector/filecoin_pending_address_operations_collector.ex b/apps/indexer/lib/indexer/prometheus/collector/filecoin_pending_address_operations_collector.ex index 2078412575..207990ca45 100644 --- a/apps/indexer/lib/indexer/prometheus/collector/filecoin_pending_address_operations_collector.ex +++ b/apps/indexer/lib/indexer/prometheus/collector/filecoin_pending_address_operations_collector.ex @@ -1,29 +1,31 @@ -defmodule Indexer.Prometheus.Collector.FilecoinPendingAddressOperations do - @moduledoc """ - Custom collector to count number of records in filecoin_pending_address_operations table. - """ +if Application.compile_env(:explorer, :chain_type) == :filecoin do + defmodule Indexer.Prometheus.Collector.FilecoinPendingAddressOperations do + @moduledoc """ + Custom collector to count number of records in filecoin_pending_address_operations table. + """ - use Prometheus.Collector + use Prometheus.Collector - alias Explorer.Chain.Filecoin.PendingAddressOperation - alias Explorer.Repo - alias Prometheus.Model + alias Explorer.Chain.Filecoin.PendingAddressOperation + alias Explorer.Repo + alias Prometheus.Model - def collect_mf(_registry, callback) do - callback.( - create_gauge( - :filecoin_pending_address_operations, - "Number of records in filecoin_pending_address_operations table", - Repo.aggregate(PendingAddressOperation, :count, timeout: :infinity) + def collect_mf(_registry, callback) do + callback.( + create_gauge( + :filecoin_pending_address_operations, + "Number of records in filecoin_pending_address_operations table", + Repo.aggregate(PendingAddressOperation, :count, timeout: :infinity) + ) ) - ) - end + end - def collect_metrics(:filecoin_pending_address_operations, count) do - Model.gauge_metrics([{count}]) - end + def collect_metrics(:filecoin_pending_address_operations, count) do + Model.gauge_metrics([{count}]) + end - defp create_gauge(name, help, data) do - Model.create_mf(name, help, :gauge, __MODULE__, data) + defp create_gauge(name, help, data) do + Model.create_mf(name, help, :gauge, __MODULE__, data) + end end end