chore: disable public metrics by default, set 1 day as default period of update (#10469)

pull/10479/head
Victor Baranov 4 months ago committed by GitHub
parent 98f299beea
commit f2b10cc6b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      apps/explorer/lib/explorer/chain/metrics.ex
  2. 70
      apps/explorer/lib/explorer/chain/metrics/queries.ex
  3. 56
      apps/explorer/lib/explorer/prometheus/instrumenter.ex
  4. 4
      config/runtime.exs
  5. 3
      docker-compose/envs/common-blockscout.env

@ -13,13 +13,13 @@ defmodule Explorer.Chain.Metrics do
@interval :timer.hours(1) @interval :timer.hours(1)
@options [timeout: 60_000, api?: true] @options [timeout: 60_000, api?: true]
@metrics_list [ @metrics_list [
:weekly_success_transactions_number, :success_transactions_number,
:weekly_deployed_smart_contracts_number, :deployed_smart_contracts_number,
:weekly_verified_smart_contracts_number, :verified_smart_contracts_number,
:weekly_new_addresses_number, :new_addresses_number,
:weekly_new_tokens_number, :new_tokens_number,
:weekly_new_token_transfers_number, :new_token_transfers_number,
:weekly_simplified_active_addresses_number :simplified_active_addresses_number
] ]
@spec start_link(term()) :: GenServer.on_start() @spec start_link(term()) :: GenServer.on_start()
@ -28,11 +28,11 @@ defmodule Explorer.Chain.Metrics do
end end
def init(_) do def init(_) do
if Application.get_env(:explorer, __MODULE__)[:disabled?] do if Application.get_env(:explorer, __MODULE__)[:enabled] do
:ignore
else
send(self(), :set_metrics) send(self(), :set_metrics)
{:ok, %{}} {:ok, %{}}
else
:ignore
end end
end end
@ -69,12 +69,12 @@ defmodule Explorer.Chain.Metrics do
defp set_handler_metric(metric) do defp set_handler_metric(metric) do
func = String.to_atom(to_string(metric) <> "_query") func = String.to_atom(to_string(metric) <> "_query")
weekly_transactions_count = transactions_count =
Queries Queries
|> apply(func, []) |> apply(func, [])
|> select_repo(@options).one() |> select_repo(@options).one()
apply(Instrumenter, metric, [weekly_transactions_count]) apply(Instrumenter, metric, [transactions_count])
end end
defp schedule_next_run do defp schedule_next_run do

@ -31,18 +31,18 @@ defmodule Explorer.Chain.Metrics.Queries do
@doc """ @doc """
Retrieves the query for fetching the number of successful transactions in a week. Retrieves the query for fetching the number of successful transactions in a week.
""" """
@spec weekly_success_transactions_number_query() :: Ecto.Query.t() @spec success_transactions_number_query() :: Ecto.Query.t()
def weekly_success_transactions_number_query do def success_transactions_number_query do
if DenormalizationHelper.transactions_denormalization_finished?() do if DenormalizationHelper.transactions_denormalization_finished?() do
Transaction Transaction
|> where([tx], tx.block_timestamp >= ago(7, "day")) |> where([tx], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx], tx.block_consensus == true) |> where([tx], tx.block_consensus == true)
|> where([tx], tx.status == ^1) |> where([tx], tx.status == ^1)
|> select([tx], count(tx.hash)) |> select([tx], count(tx.hash))
else else
Transaction Transaction
|> join(:inner, [tx], block in assoc(tx, :block)) |> join(:inner, [tx], block in assoc(tx, :block))
|> where([tx, block], block.timestamp >= ago(7, "day")) |> where([tx, block], block.timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx, block], block.consensus == true) |> where([tx, block], block.consensus == true)
|> where([tx, block], tx.status == ^1) |> where([tx, block], tx.status == ^1)
|> select([tx, block], count(tx.hash)) |> select([tx, block], count(tx.hash))
@ -52,13 +52,13 @@ defmodule Explorer.Chain.Metrics.Queries do
@doc """ @doc """
Retrieves the query for the number of smart contracts deployed in the current week. Retrieves the query for the number of smart contracts deployed in the current week.
""" """
@spec weekly_deployed_smart_contracts_number_query() :: Ecto.Query.t() @spec deployed_smart_contracts_number_query() :: Ecto.Query.t()
def weekly_deployed_smart_contracts_number_query do def deployed_smart_contracts_number_query do
transactions_query = transactions_query =
if DenormalizationHelper.transactions_denormalization_finished?() do if DenormalizationHelper.transactions_denormalization_finished?() do
Transaction Transaction
|> where([tx], not is_nil(tx.created_contract_address_hash)) |> where([tx], not is_nil(tx.created_contract_address_hash))
|> where([tx], tx.block_timestamp >= ago(7, "day")) |> where([tx], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx], tx.block_consensus == true) |> where([tx], tx.block_consensus == true)
|> where([tx], tx.status == ^1) |> where([tx], tx.status == ^1)
|> select([tx], tx.created_contract_address_hash) |> select([tx], tx.created_contract_address_hash)
@ -67,7 +67,7 @@ defmodule Explorer.Chain.Metrics.Queries do
|> join(:inner, [tx], block in assoc(tx, :block)) |> join(:inner, [tx], block in assoc(tx, :block))
|> where([tx], not is_nil(tx.created_contract_address_hash)) |> where([tx], not is_nil(tx.created_contract_address_hash))
|> where([tx, block], block.consensus == true) |> where([tx, block], block.consensus == true)
|> where([tx, block], block.timestamp >= ago(7, "day")) |> where([tx, block], block.timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx, block], tx.status == ^1) |> where([tx, block], tx.status == ^1)
|> select([tx, block], tx.created_contract_address_hash) |> select([tx, block], tx.created_contract_address_hash)
end end
@ -77,7 +77,7 @@ defmodule Explorer.Chain.Metrics.Queries do
# InternalTransaction # InternalTransaction
# |> join(:inner, [it], transaction in assoc(it, :transaction)) # |> join(:inner, [it], transaction in assoc(it, :transaction))
# |> where([it, tx], not is_nil(it.created_contract_address_hash)) # |> where([it, tx], not is_nil(it.created_contract_address_hash))
# |> where([it, tx], tx.block_timestamp >= ago(7, "day")) # |> where([it, tx], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
# |> where([it, tx], tx.block_consensus == true) # |> where([it, tx], tx.block_consensus == true)
# |> where([it, tx], tx.status == ^1) # |> where([it, tx], tx.status == ^1)
# |> select([it, tx], it.created_contract_address_hash) # |> select([it, tx], it.created_contract_address_hash)
@ -97,41 +97,41 @@ defmodule Explorer.Chain.Metrics.Queries do
@doc """ @doc """
Retrieves the query for the number of verified smart contracts in the current week. Retrieves the query for the number of verified smart contracts in the current week.
""" """
@spec weekly_verified_smart_contracts_number_query() :: Ecto.Query.t() @spec verified_smart_contracts_number_query() :: Ecto.Query.t()
def weekly_verified_smart_contracts_number_query do def verified_smart_contracts_number_query do
SmartContract SmartContract
|> where([sc], sc.inserted_at >= ago(7, "day")) |> where([sc], sc.inserted_at >= ago(^update_period_hours(), "hour"))
|> select([sc], count(sc.address_hash)) |> select([sc], count(sc.address_hash))
end end
@doc """ @doc """
Retrieves the query for the number of new addresses in the current week. Retrieves the query for the number of new addresses in the current week.
""" """
@spec weekly_new_addresses_number_query() :: Ecto.Query.t() @spec new_addresses_number_query() :: Ecto.Query.t()
def weekly_new_addresses_number_query do def new_addresses_number_query do
Address Address
|> where([a], a.inserted_at >= ago(7, "day")) |> where([a], a.inserted_at >= ago(^update_period_hours(), "hour"))
|> select([a], count(a.hash)) |> select([a], count(a.hash))
end end
@doc """ @doc """
Retrieves the query for the number of new tokens detected in the current week. Retrieves the query for the number of new tokens detected in the current week.
""" """
@spec weekly_new_tokens_number_query() :: Ecto.Query.t() @spec new_tokens_number_query() :: Ecto.Query.t()
def weekly_new_tokens_number_query do def new_tokens_number_query do
Token Token
|> where([token], token.inserted_at >= ago(7, "day")) |> where([token], token.inserted_at >= ago(^update_period_hours(), "hour"))
|> select([token], count(token.contract_address_hash)) |> select([token], count(token.contract_address_hash))
end end
@doc """ @doc """
Retrieves the query for the number of new token transfers detected in the current week. Retrieves the query for the number of new token transfers detected in the current week.
""" """
@spec weekly_new_token_transfers_number_query() :: Ecto.Query.t() @spec new_token_transfers_number_query() :: Ecto.Query.t()
def weekly_new_token_transfers_number_query do def new_token_transfers_number_query do
TokenTransfer TokenTransfer
|> join(:inner, [tt], block in Block, on: block.number == tt.block_number) |> join(:inner, [tt], block in Block, on: block.number == tt.block_number)
|> where([tt, block], block.timestamp >= ago(7, "day")) |> where([tt, block], block.timestamp >= ago(^update_period_hours(), "hour"))
|> where([tt, block], block.consensus == true) |> where([tt, block], block.consensus == true)
|> select([tt, block], fragment("COUNT(*)")) |> select([tt, block], fragment("COUNT(*)"))
end end
@ -139,17 +139,17 @@ defmodule Explorer.Chain.Metrics.Queries do
@doc """ @doc """
Retrieves the query for the number of addresses initiated transactions in the current week. Retrieves the query for the number of addresses initiated transactions in the current week.
""" """
@spec weekly_simplified_active_addresses_number_query() :: Ecto.Query.t() @spec simplified_active_addresses_number_query() :: Ecto.Query.t()
def weekly_simplified_active_addresses_number_query do def simplified_active_addresses_number_query do
if DenormalizationHelper.transactions_denormalization_finished?() do if DenormalizationHelper.transactions_denormalization_finished?() do
Transaction Transaction
|> where([tx], tx.block_timestamp >= ago(7, "day")) |> where([tx], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx], tx.block_consensus == true) |> where([tx], tx.block_consensus == true)
|> select([tx], fragment("COUNT(DISTINCT(?))", tx.from_address_hash)) |> select([tx], fragment("COUNT(DISTINCT(?))", tx.from_address_hash))
else else
Transaction Transaction
|> join(:inner, [tx], block in assoc(tx, :block)) |> join(:inner, [tx], block in assoc(tx, :block))
|> where([tx, block], block.timestamp >= ago(7, "day")) |> where([tx, block], block.timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx, block], block.consensus == true) |> where([tx, block], block.consensus == true)
|> select([tx], fragment("COUNT(DISTINCT(?))", tx.from_address_hash)) |> select([tx], fragment("COUNT(DISTINCT(?))", tx.from_address_hash))
end end
@ -159,12 +159,12 @@ defmodule Explorer.Chain.Metrics.Queries do
Retrieves the query for the number of active EOA and smart-contract addresses (from/to/contract participated in transactions, internal transactions, token transfers) in the current week. Retrieves the query for the number of active EOA and smart-contract addresses (from/to/contract participated in transactions, internal transactions, token transfers) in the current week.
This query is currently unused since the very low performance: it doesn't return results in 1 hour. This query is currently unused since the very low performance: it doesn't return results in 1 hour.
""" """
@spec weekly_active_addresses_number_query() :: Ecto.Query.t() @spec active_addresses_number_query() :: Ecto.Query.t()
def weekly_active_addresses_number_query do def active_addresses_number_query do
transactions_query = transactions_query =
if DenormalizationHelper.transactions_denormalization_finished?() do if DenormalizationHelper.transactions_denormalization_finished?() do
Transaction Transaction
|> where([tx], tx.block_timestamp >= ago(7, "day")) |> where([tx], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx], tx.block_consensus == true) |> where([tx], tx.block_consensus == true)
|> distinct(true) |> distinct(true)
|> select([tx], %{ |> select([tx], %{
@ -179,7 +179,7 @@ defmodule Explorer.Chain.Metrics.Queries do
else else
Transaction Transaction
|> join(:inner, [tx], block in assoc(tx, :block)) |> join(:inner, [tx], block in assoc(tx, :block))
|> where([tx, block], block.timestamp >= ago(7, "day")) |> where([tx, block], block.timestamp >= ago(^update_period_hours(), "hour"))
|> where([tx, block], block.consensus == true) |> where([tx, block], block.consensus == true)
|> distinct(true) |> distinct(true)
|> select([tx, block], %{ |> select([tx, block], %{
@ -197,7 +197,7 @@ defmodule Explorer.Chain.Metrics.Queries do
if DenormalizationHelper.transactions_denormalization_finished?() do if DenormalizationHelper.transactions_denormalization_finished?() do
InternalTransaction InternalTransaction
|> join(:inner, [it], transaction in assoc(it, :transaction)) |> join(:inner, [it], transaction in assoc(it, :transaction))
|> where([it, tx], tx.block_timestamp >= ago(7, "day")) |> where([it, tx], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([it, tx], tx.block_consensus == true) |> where([it, tx], tx.block_consensus == true)
|> where([it, tx], tx.status == ^1) |> where([it, tx], tx.status == ^1)
|> select([it, tx], %{ |> select([it, tx], %{
@ -214,7 +214,7 @@ defmodule Explorer.Chain.Metrics.Queries do
InternalTransaction InternalTransaction
|> join(:inner, [it], transaction in assoc(it, :transaction)) |> join(:inner, [it], transaction in assoc(it, :transaction))
|> join(:inner, [tx], block in assoc(tx, :block)) |> join(:inner, [tx], block in assoc(tx, :block))
|> where([it, tx, block], tx.block_timestamp >= ago(7, "day")) |> where([it, tx, block], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([it, tx, block], block.consensus == true) |> where([it, tx, block], block.consensus == true)
|> where([it, tx, block], tx.status == ^1) |> where([it, tx, block], tx.status == ^1)
|> select([it, tx, block], %{ |> select([it, tx, block], %{
@ -233,7 +233,7 @@ defmodule Explorer.Chain.Metrics.Queries do
if DenormalizationHelper.transactions_denormalization_finished?() do if DenormalizationHelper.transactions_denormalization_finished?() do
TokenTransfer TokenTransfer
|> join(:inner, [tt], transaction in assoc(tt, :transaction)) |> join(:inner, [tt], transaction in assoc(tt, :transaction))
|> where([tt, tx], tx.block_timestamp >= ago(7, "day")) |> where([tt, tx], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([tt, tx], tx.block_consensus == true) |> where([tt, tx], tx.block_consensus == true)
|> where([tt, tx], tx.status == ^1) |> where([tt, tx], tx.status == ^1)
|> select([tt, tx], %{ |> select([tt, tx], %{
@ -245,7 +245,7 @@ defmodule Explorer.Chain.Metrics.Queries do
TokenTransfer TokenTransfer
|> join(:inner, [tt], transaction in assoc(tt, :transaction)) |> join(:inner, [tt], transaction in assoc(tt, :transaction))
|> join(:inner, [tx], block in assoc(tx, :block)) |> join(:inner, [tx], block in assoc(tx, :block))
|> where([tt, tx, block], tx.block_timestamp >= ago(7, "day")) |> where([tt, tx, block], tx.block_timestamp >= ago(^update_period_hours(), "hour"))
|> where([tt, tx, block], block.consensus == true) |> where([tt, tx, block], block.consensus == true)
|> where([tt, tx, block], tx.status == ^1) |> where([tt, tx, block], tx.status == ^1)
|> select([tt, tx, block], %{ |> select([tt, tx, block], %{
@ -266,4 +266,8 @@ defmodule Explorer.Chain.Metrics.Queries do
select: fragment("COUNT(DISTINCT ?)", q.address_hash) select: fragment("COUNT(DISTINCT ?)", q.address_hash)
) )
end end
defp update_period_hours do
Application.get_env(:explorer, Explorer.Chain.Metrics)[:update_period_hours]
end
end end

@ -14,45 +14,45 @@ defmodule Explorer.Prometheus.Instrumenter do
] ]
@gauge [ @gauge [
name: :weekly_success_transactions_number, name: :success_transactions_number,
help: "Number of successful transactions in the last 7 days", help: "Number of successful transactions in the period (default is 1 day)",
registry: :public registry: :public
] ]
@gauge [ @gauge [
name: :weekly_deployed_smart_contracts_number, name: :deployed_smart_contracts_number,
help: help:
"Number of deployed smart-contracts (smart-contracts from internal transactions are not accounted) in the last 7 days", "Number of deployed smart-contracts (smart-contracts from internal transactions are not accounted) in the period (default is 1 day)",
registry: :public registry: :public
] ]
@gauge [ @gauge [
name: :weekly_verified_smart_contracts_number, name: :verified_smart_contracts_number,
help: "Number of verified smart-contracts in the last 7 days", help: "Number of verified smart-contracts in the period (default is 1 day)",
registry: :public registry: :public
] ]
@gauge [ @gauge [
name: :weekly_new_addresses_number, name: :new_addresses_number,
help: "Number of new wallet addresses in the last 7 days", help: "Number of new wallet addresses in the period (default is 1 day)",
registry: :public registry: :public
] ]
@gauge [ @gauge [
name: :weekly_new_tokens_number, name: :new_tokens_number,
help: "Number of new tokens detected in the last 7 days", help: "Number of new tokens detected in the period (default is 1 day)",
registry: :public registry: :public
] ]
@gauge [ @gauge [
name: :weekly_new_token_transfers_number, name: :new_token_transfers_number,
help: "Number of new token transfers detected in the last 7 days", help: "Number of new token transfers detected in the period (default is 1 day)",
registry: :public registry: :public
] ]
@gauge [ @gauge [
name: :weekly_active_addresses_number, name: :active_addresses_number,
help: "Number of active EOA addresses (participated in transactions in to/from) in the last 7 days", help: "Number of active EOA addresses (participated in transactions in to/from) in the period (default is 1 day)",
registry: :public registry: :public
] ]
@ -64,31 +64,31 @@ defmodule Explorer.Prometheus.Instrumenter do
result result
end end
def weekly_success_transactions_number(number) do def success_transactions_number(number) do
Gauge.set([name: :weekly_success_transactions_number, registry: :public], number) Gauge.set([name: :success_transactions_number, registry: :public], number)
end end
def weekly_deployed_smart_contracts_number(number) do def deployed_smart_contracts_number(number) do
Gauge.set([name: :weekly_deployed_smart_contracts_number, registry: :public], number) Gauge.set([name: :deployed_smart_contracts_number, registry: :public], number)
end end
def weekly_verified_smart_contracts_number(number) do def verified_smart_contracts_number(number) do
Gauge.set([name: :weekly_verified_smart_contracts_number, registry: :public], number) Gauge.set([name: :verified_smart_contracts_number, registry: :public], number)
end end
def weekly_new_addresses_number(number) do def new_addresses_number(number) do
Gauge.set([name: :weekly_new_addresses_number, registry: :public], number) Gauge.set([name: :new_addresses_number, registry: :public], number)
end end
def weekly_new_tokens_number(number) do def new_tokens_number(number) do
Gauge.set([name: :weekly_new_tokens_number, registry: :public], number) Gauge.set([name: :new_tokens_number, registry: :public], number)
end end
def weekly_new_token_transfers_number(number) do def new_token_transfers_number(number) do
Gauge.set([name: :weekly_new_token_transfers_number, registry: :public], number) Gauge.set([name: :new_token_transfers_number, registry: :public], number)
end end
def weekly_simplified_active_addresses_number(number) do def simplified_active_addresses_number(number) do
Gauge.set([name: :weekly_active_addresses_number, registry: :public], number) Gauge.set([name: :active_addresses_number, registry: :public], number)
end end
end end

@ -596,7 +596,9 @@ config :explorer, Explorer.Chain.TokenTransfer,
whitelisted_weth_contracts: ConfigHelper.parse_list_env_var("WHITELISTED_WETH_CONTRACTS", ""), whitelisted_weth_contracts: ConfigHelper.parse_list_env_var("WHITELISTED_WETH_CONTRACTS", ""),
weth_token_transfers_filtering_enabled: ConfigHelper.parse_bool_env_var("WETH_TOKEN_TRANSFERS_FILTERING_ENABLED") weth_token_transfers_filtering_enabled: ConfigHelper.parse_bool_env_var("WETH_TOKEN_TRANSFERS_FILTERING_ENABLED")
config :explorer, Explorer.Chain.Metrics, disabled?: ConfigHelper.parse_bool_env_var("METRICS_DISABLE_PUBLIC", "false") config :explorer, Explorer.Chain.Metrics,
enabled: ConfigHelper.parse_bool_env_var("PUBLIC_METRICS_ENABLED", "false"),
update_period_hours: ConfigHelper.parse_integer_env_var("PUBLIC_METRICS_UPDATE_PERIOD_HOURS", 24)
############### ###############
### Indexer ### ### Indexer ###

@ -398,4 +398,5 @@ TENDERLY_CHAIN_PATH=
# WHITELISTED_WETH_CONTRACTS= # WHITELISTED_WETH_CONTRACTS=
# SANITIZE_INCORRECT_WETH_BATCH_SIZE=100 # SANITIZE_INCORRECT_WETH_BATCH_SIZE=100
# SANITIZE_INCORRECT_WETH_CONCURRENCY=1 # SANITIZE_INCORRECT_WETH_CONCURRENCY=1
# METRICS_DISABLE_PUBLIC= # PUBLIC_METRICS_ENABLED=
# PUBLIC_METRICS_UPDATE_PERIOD_HOURS=
Loading…
Cancel
Save