From 4549642bfd020418b10a1cf0c6d3e44a22d3ca96 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 12 Dec 2021 16:47:06 +0400 Subject: [PATCH] Merge pull request #4998 from blockscout/vb-api-endpoints-logger API endpoints logger --- CHANGELOG.md | 1 + apps/block_scout_web/config/dev.exs | 5 +++++ apps/block_scout_web/config/prod.exs | 6 ++++++ .../controllers/api/api_logger.ex | 19 +++++++++++++++++++ .../controllers/api/rpc/rpc_translator.ex | 2 ++ .../decompiled_smart_contract_controller.ex | 4 ++++ .../controllers/api/v1/health_controller.ex | 4 ++++ .../controllers/api/v1/supply_controller.ex | 2 ++ .../v1/verified_smart_contract_controller.ex | 4 ++++ apps/explorer/mix.exs | 2 +- config/config.exs | 3 ++- mix.exs | 2 +- 12 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 apps/block_scout_web/lib/block_scout_web/controllers/api/api_logger.ex diff --git a/CHANGELOG.md b/CHANGELOG.md index 67d415080b3c..69f4137b226b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [#4888](https://github.com/blockscout/blockscout/pull/4888) - Fix fetch_top_tokens method: add nulls last for token holders desc order ### Chore +- [#4998](https://github.com/blockscout/blockscout/pull/4998) - API endpoints logger ## 4.0.0-beta diff --git a/apps/block_scout_web/config/dev.exs b/apps/block_scout_web/config/dev.exs index b841d8efe1a3..82f9fc6629a7 100644 --- a/apps/block_scout_web/config/dev.exs +++ b/apps/block_scout_web/config/dev.exs @@ -78,6 +78,11 @@ config :logger, :block_scout_web, level: :debug, path: Path.absname("logs/dev/block_scout_web.log") +config :logger, :api, + level: :debug, + path: Path.absname("logs/dev/api.log"), + metadata_filter: [fetcher: :api] + # Set a higher stacktrace during development. Avoid configuring such # in production as building large stacktraces may be expensive. config :phoenix, :stacktrace_depth, 20 diff --git a/apps/block_scout_web/config/prod.exs b/apps/block_scout_web/config/prod.exs index 4d97721ce4c7..2882ea50f634 100644 --- a/apps/block_scout_web/config/prod.exs +++ b/apps/block_scout_web/config/prod.exs @@ -33,3 +33,9 @@ config :logger, :block_scout_web, level: :info, path: Path.absname("logs/prod/block_scout_web.log"), rotate: %{max_bytes: 52_428_800, keep: 19} + +config :logger, :api, + level: :debug, + path: Path.absname("logs/prod/api.log"), + metadata_filter: [fetcher: :api], + rotate: %{max_bytes: 52_428_800, keep: 19} diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/api_logger.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/api_logger.ex new file mode 100644 index 000000000000..65fff404d714 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/api_logger.ex @@ -0,0 +1,19 @@ +defmodule APILogger do + @moduledoc """ + Logger of API ednpoins usage + """ + require Logger + + def log(conn) do + endpoint = + if conn.query_string do + "#{conn.request_path}?#{conn.query_string}" + else + conn.request_path + end + + Logger.debug(endpoint, + fetcher: :api + ) + end +end diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex index b6380fc97536..9f9de75e8ae2 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex @@ -14,6 +14,7 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do """ require Logger + require APILogger import Plug.Conn import Phoenix.Controller, only: [put_view: 2] @@ -29,6 +30,7 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do {:ok, action} <- translate_action(action), true <- action_accessed?(action, write_actions), {:ok, conn} <- call_controller(conn, controller, action) do + APILogger.log(conn) conn else {:error, :no_action} -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex index 6b03afe82ac7..b04aed7405b7 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex @@ -1,10 +1,14 @@ defmodule BlockScoutWeb.API.V1.DecompiledSmartContractController do use BlockScoutWeb, :controller + require APILogger + alias Explorer.Chain alias Explorer.Chain.Hash.Address def create(conn, params) do + APILogger.log(conn) + if auth_token(conn) == actual_token() do with {:ok, hash} <- validate_address_hash(params["address_hash"]), :ok <- Chain.check_address_exists(hash), diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex index 1e65fa75ba1f..46eff9f5d497 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex @@ -1,9 +1,13 @@ defmodule BlockScoutWeb.API.V1.HealthController do use BlockScoutWeb, :controller + require APILogger + alias Explorer.Chain def health(conn, _) do + APILogger.log(conn) + with {:ok, number, timestamp} <- Chain.last_db_block_status(), {:ok, cache_number, cache_timestamp} <- Chain.last_cache_block_status() do send_resp(conn, :ok, result(number, timestamp, cache_number, cache_timestamp)) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex index 6b3cf3edd6a2..14ef262d7ec2 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex @@ -1,9 +1,11 @@ defmodule BlockScoutWeb.API.V1.SupplyController do use BlockScoutWeb, :controller + require APILogger alias Explorer.Chain def supply(conn, _) do + APILogger.log(conn) total_supply = Chain.total_supply() circulating_supply = Chain.circulating_supply() diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex index 59431742ad87..f2852b192bdf 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex @@ -1,11 +1,15 @@ defmodule BlockScoutWeb.API.V1.VerifiedSmartContractController do use BlockScoutWeb, :controller + require APILogger + alias Explorer.Chain alias Explorer.Chain.Hash.Address alias Explorer.SmartContract.Solidity.Publisher def create(conn, params) do + APILogger.log(conn) + with {:ok, hash} <- validate_address_hash(params["address_hash"]), :ok <- Chain.check_address_exists(hash), {:contract, :not_found} <- {:contract, Chain.check_verified_smart_contract_exists(hash)} do diff --git a/apps/explorer/mix.exs b/apps/explorer/mix.exs index bb6834499d38..31512e362478 100644 --- a/apps/explorer/mix.exs +++ b/apps/explorer/mix.exs @@ -104,7 +104,7 @@ defmodule Explorer.Mixfile do # `:spandex` tracing of `:ecto` {:spandex_ecto, "~> 0.6.2"}, # Attach `:prometheus_ecto` to `:ecto` - {:telemetry, "~> 0.4.1"}, + {:telemetry, "~> 0.4.3"}, # `Timex.Duration` for `Explorer.Counters.AverageBlockTime.average_block_time/0` {:timex, "~> 3.7.1"}, {:con_cache, "~> 1.0"}, diff --git a/config/config.exs b/config/config.exs index 6a4dcb75ad9f..939702dadeb0 100644 --- a/config/config.exs +++ b/config/config.exs @@ -31,7 +31,8 @@ config :logger, {LoggerFileBackend, :token_instances}, {LoggerFileBackend, :reading_token_functions}, {LoggerFileBackend, :pending_transactions_to_refetch}, - {LoggerFileBackend, :empty_blocks_to_refetch} + {LoggerFileBackend, :empty_blocks_to_refetch}, + {LoggerFileBackend, :api} ] config :logger, :console, diff --git a/mix.exs b/mix.exs index c5629a2feda8..174eaffeff4e 100644 --- a/mix.exs +++ b/mix.exs @@ -11,7 +11,7 @@ defmodule BlockScout.Mixfile do apps_path: "apps", deps: deps(), dialyzer: dialyzer(), - elixir: "~> 1.10", + elixir: "~> 1.12", preferred_cli_env: [ credo: :test, dialyzer: :test