Skip to content

Commit

Permalink
Merge pull request blockscout#4998 from blockscout/vb-api-endpoints-l…
Browse files Browse the repository at this point in the history
…ogger

API endpoints logger
  • Loading branch information
vbaranov authored and jagdeep sidhu committed Dec 14, 2021
1 parent b01160b commit 4549642
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions apps/block_scout_web/config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions apps/block_scout_web/config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do
"""

require Logger
require APILogger

import Plug.Conn
import Phoenix.Controller, only: [put_view: 2]
Expand All @@ -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} ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/explorer/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4549642

Please sign in to comment.