From 0243726f9b30560e6f80f2baf2b9abd6b3dafd81 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Wed, 12 Dec 2018 11:26:02 -0600 Subject: [PATCH] Move block number out of prose and into metadata Realtime block number will set `Logger.metadata(block_number: ...)` for each block number is fetches separately after getting the `newHead` websocket message. --- apps/block_scout_web/config/config.exs | 2 +- apps/ethereum_jsonrpc/config/config.exs | 2 +- apps/explorer/config/config.exs | 2 +- apps/indexer/config/config.exs | 2 +- .../lib/indexer/block/realtime/fetcher.ex | 16 ++++------------ apps/indexer/lib/indexer/logger.ex | 16 +++++++++++++++- config/config.exs | 6 +++--- 7 files changed, 26 insertions(+), 20 deletions(-) diff --git a/apps/block_scout_web/config/config.exs b/apps/block_scout_web/config/config.exs index b49733662d..eb416d21e1 100644 --- a/apps/block_scout_web/config/config.exs +++ b/apps/block_scout_web/config/config.exs @@ -48,7 +48,7 @@ config :ex_cldr, config :logger, :block_scout_web, # keep synced with `config/config.exs` format: "$dateT$time $metadata[$level] $message\n", - metadata: ~w(application fetcher request_id)a, + metadata: ~w(application fetcher request_id block_number)a, metadata_filter: [application: :block_scout_web] config :spandex_phoenix, tracer: BlockScoutWeb.Tracer diff --git a/apps/ethereum_jsonrpc/config/config.exs b/apps/ethereum_jsonrpc/config/config.exs index 7f234ef7b3..e88a15c434 100644 --- a/apps/ethereum_jsonrpc/config/config.exs +++ b/apps/ethereum_jsonrpc/config/config.exs @@ -17,7 +17,7 @@ config :ethereum_jsonrpc, EthereumJSONRPC.Tracer, config :logger, :ethereum_jsonrpc, # keep synced with `config/config.exs` format: "$dateT$time $metadata[$level] $message\n", - metadata: ~w(application fetcher request_id)a, + metadata: ~w(application fetcher request_id block_number)a, metadata_filter: [application: :ethereum_jsonrpc] # Import environment specific config. This must remain at the bottom diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index 439fdad4cd..c535602ee8 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -50,7 +50,7 @@ config :explorer, config :logger, :explorer, # keep synced with `config/config.exs` format: "$dateT$time $metadata[$level] $message\n", - metadata: ~w(application fetcher request_id)a, + metadata: ~w(application fetcher request_id block_number)a, metadata_filter: [application: :explorer] config :spandex_ecto, SpandexEcto.EctoLogger, diff --git a/apps/indexer/config/config.exs b/apps/indexer/config/config.exs index b08cb0ef23..add2c6c8b9 100644 --- a/apps/indexer/config/config.exs +++ b/apps/indexer/config/config.exs @@ -19,7 +19,7 @@ config :indexer, Indexer.Tracer, config :logger, :indexer, # keep synced with `config/config.exs` format: "$dateT$time $metadata[$level] $message\n", - metadata: ~w(application fetcher request_id)a, + metadata: ~w(application fetcher request_id block_number)a, metadata_filter: [application: :indexer] # Import environment specific config. This must remain at the bottom diff --git a/apps/indexer/lib/indexer/block/realtime/fetcher.ex b/apps/indexer/lib/indexer/block/realtime/fetcher.ex index a1381871d4..f743281db5 100644 --- a/apps/indexer/lib/indexer/block/realtime/fetcher.ex +++ b/apps/indexer/lib/indexer/block/realtime/fetcher.ex @@ -159,8 +159,7 @@ defmodule Indexer.Block.Realtime.Fetcher do @decorate trace(name: "fetch", resource: "Indexer.Block.Realtime.Fetcher.fetch_and_import_block/3", tracer: Tracer) def fetch_and_import_block(block_number_to_fetch, block_fetcher, reorg?, retry \\ 3) do - Logger.metadata(fetcher: :block_realtime) - + Indexer.Logger.metadata(fn -> if reorg? do # give previous fetch attempt (for same block number) a chance to finish # before fetching again, to reduce block consensus mistakes @@ -168,6 +167,7 @@ defmodule Indexer.Block.Realtime.Fetcher do end do_fetch_and_import_block(block_number_to_fetch, block_fetcher, retry) + end, fetcher: :block_realtime, block_number: block_number_to_fetch) end @decorate span(tracer: Tracer) @@ -179,16 +179,12 @@ defmodule Indexer.Block.Realtime.Fetcher do Task.Supervisor.start_child(TaskSupervisor, ConsensusEnsurer, :perform, args) end - Logger.debug(fn -> - ["fetched and imported block ", to_string(block_number_to_fetch)] - end) + Logger.debug("Fetched and imported.") {:ok, %{inserted: _, errors: [_ | _] = errors}} -> Logger.error(fn -> [ - "failed to fetch block ", - to_string(block_number_to_fetch), - ": ", + "failed to fetch block: ", inspect(errors), ". Block will be retried by catchup indexer." ] @@ -199,8 +195,6 @@ defmodule Indexer.Block.Realtime.Fetcher do [ "failed to fetch ", to_string(step), - " for block ", - to_string(block_number_to_fetch), ": ", inspect(reason), ". Block will be retried by catchup indexer." @@ -232,8 +226,6 @@ defmodule Indexer.Block.Realtime.Fetcher do [ "failed to insert ", to_string(step), - " for block ", - to_string(block_number_to_fetch), ": ", inspect(failed_value), ". Block will be retried by catchup indexer." diff --git a/apps/indexer/lib/indexer/logger.ex b/apps/indexer/lib/indexer/logger.ex index 7d344e4f88..a34bcd6e47 100644 --- a/apps/indexer/lib/indexer/logger.ex +++ b/apps/indexer/lib/indexer/logger.ex @@ -1,8 +1,22 @@ defmodule Indexer.Logger do @moduledoc """ - Helpers for formatting `Logger` data as `t:iodata/0`. + Helpers for `Logger`. """ + @doc """ + Sets `keyword` in `Logger.metadata/1` around `fun`. + """ + def metadata(fun, keyword) when is_function(fun, 0) and is_list(keyword) do + metadata_before = Logger.metadata() + + try do + Logger.metadata(keyword) + fun.() + after + Logger.reset_metadata(metadata_before) + end + end + @doc """ The PID and its registered name (if it has one) as `t:iodata/0`. """ diff --git a/config/config.exs b/config/config.exs index 5a4e9ec4f6..2c4021e77e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -32,19 +32,19 @@ config :logger, config :logger, :console, # Use same format for all loggers, even though the level should only ever be `:error` for `:error` backend format: "$dateT$time $metadata[$level] $message\n", - metadata: ~w(application fetcher request_id)a + metadata: ~w(application fetcher request_id block_number)a config :logger, :ecto, # Use same format for all loggers, even though the level should only ever be `:error` for `:error` backend format: "$dateT$time $metadata[$level] $message\n", - metadata: ~w(application fetcher request_id)a, + metadata: ~w(application fetcher request_id block_number)a, metadata_filter: [application: :ecto] config :logger, :error, # Use same format for all loggers, even though the level should only ever be `:error` for `:error` backend format: "$dateT$time $metadata[$level] $message\n", level: :error, - metadata: ~w(application fetcher request_id)a + metadata: ~w(application fetcher request_id block_number)a # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above.