Merge pull request #754 from poanetwork/iff-include-token-balances-logs

New log file for TokenBalances interaction errors
pull/756/head
Igor Florian 6 years ago committed by GitHub
commit 6a65500cbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      apps/indexer/config/dev.exs
  2. 5
      apps/indexer/config/prod.exs
  3. 5
      apps/indexer/config/test.exs
  4. 1
      apps/indexer/lib/indexer/block/realtime/fetcher.ex
  5. 6
      apps/indexer/lib/indexer/token_balance/fetcher.ex
  6. 30
      apps/indexer/lib/indexer/token_balances.ex
  7. 61
      apps/indexer/test/indexer/token_balances_test.exs
  8. 3
      config/config.exs

@ -4,6 +4,11 @@ config :logger, :indexer,
level: :debug,
path: Path.absname("logs/dev/indexer.log")
config :logger, :indexer_token_balances,
level: :debug,
path: Path.absname("logs/dev/indexer/token_balances/error.log"),
metadata_filter: [fetcher: :token_balances]
variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"

@ -4,6 +4,11 @@ config :logger, :indexer,
level: :info,
path: Path.absname("logs/prod/indexer.log")
config :logger, :indexer_token_balances,
level: :debug,
path: Path.absname("logs/prod/indexer/token_balances/error.log"),
metadata_filter: [fetcher: :token_balances]
variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"

@ -3,3 +3,8 @@ use Mix.Config
config :logger, :indexer,
level: :warn,
path: Path.absname("logs/test/indexer.log")
config :logger, :indexer_token_balances,
level: :debug,
path: Path.absname("logs/test/indexer/token_balances/error.log"),
metadata_filter: [fetcher: :token_balances]

@ -146,6 +146,7 @@ defmodule Indexer.Block.Realtime.Fetcher do
|> put_in([Access.key(:internal_transactions, %{}), :params], internal_transactions_params)
|> put_in([Access.key(:token_balances), :params], token_balances),
{:ok, results} = ok <- Chain.import(chain_import_options) do
TokenBalances.log_fetching_errors(__MODULE__, token_balances)
async_import_remaining_block_data(results)
ok
end

@ -69,12 +69,14 @@ defmodule Indexer.TokenBalance.Fetcher do
end
def fetch_from_blockchain(token_balances) do
{:ok, token_balances_params} =
{:ok, token_balances} =
token_balances
|> Stream.map(&format_params/1)
|> TokenBalances.fetch_token_balances_from_blockchain()
token_balances_params
TokenBalances.log_fetching_errors(__MODULE__, token_balances)
token_balances
end
def import_token_balances(token_balances_params) do

@ -3,6 +3,8 @@ defmodule Indexer.TokenBalances do
Reads Token's balances using Smart Contract functions from the blockchain.
"""
require Logger
alias Explorer.Token.BalanceReader
@doc """
@ -42,11 +44,11 @@ defmodule Indexer.TokenBalances do
end
defp set_token_balance_value({:ok, balance}, token_balance) do
Map.merge(token_balance, %{value: balance, value_fetched_at: DateTime.utc_now()})
Map.merge(token_balance, %{value: balance, value_fetched_at: DateTime.utc_now(), error: nil})
end
defp set_token_balance_value({:error, _}, token_balance) do
Map.merge(token_balance, %{value: nil, value_fetched_at: nil})
defp set_token_balance_value({:error, error_message}, token_balance) do
Map.merge(token_balance, %{value: nil, value_fetched_at: nil, error: error_message})
end
def format_task_results({:exit, :timeout}), do: {:error, :timeout}
@ -54,4 +56,26 @@ defmodule Indexer.TokenBalances do
def ignore_request_with_timeouts({:error, :timeout}), do: false
def ignore_request_with_timeouts(_token_balance), do: true
def log_fetching_errors(from, token_balances_params) do
error_messages =
token_balances_params
|> Stream.filter(fn token_balance -> token_balance.error != nil end)
|> Enum.map(fn token_balance ->
"<address_hash: #{token_balance.token_contract_address_hash}, " <>
"contract_address_hash: #{token_balance.address_hash}, " <>
"block_number: #{token_balance.block_number}, " <> "error: #{token_balance.error}> \n"
end)
if Enum.any?(error_messages) do
Logger.debug(
[
"<#{from}> ",
"Errors while fetching TokenBalances through Contract interaction: \n",
error_messages
],
fetcher: :token_balances
)
end
end
end

@ -8,6 +8,7 @@ defmodule Indexer.TokenBalancesTest do
alias Explorer.Chain.Hash
import Mox
import ExUnit.CaptureLog
setup :verify_on_exit!
setup :set_mox_global
@ -88,6 +89,66 @@ defmodule Indexer.TokenBalancesTest do
end
end
describe "log_fetching_errors" do
test "logs the given from argument in final message" do
token_balance_params_with_error = Map.put(build(:token_balance), :error, "Error")
params = [token_balance_params_with_error]
from = "Tests"
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response =~ "<Tests"
end
test "log when there is a token_balance param with errors" do
from = "Tests"
token_balance_params_with_error = Map.put(build(:token_balance), :error, "Error")
params = [token_balance_params_with_error]
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response =~ "Error"
end
test "log multiple token balances params with errors" do
from = "Tests"
error_1 = "Error"
error_2 = "BadGateway"
params = [
Map.put(build(:token_balance), :error, error_1),
Map.put(build(:token_balance), :error, error_2)
]
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response =~ error_1
assert log_message_response =~ error_2
end
test "doesn't log when there aren't errors after fetching token balances" do
from = "Tests"
token_balance_params = Map.put(build(:token_balance), :error, nil)
params = [token_balance_params]
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response == ""
end
end
defp get_balance_from_blockchain() do
expect(
EthereumJSONRPC.Mox,

@ -24,7 +24,8 @@ config :logger,
# only :explorer, but all levels
{LoggerFileBackend, :explorer},
# only :indexer, but all levels
{LoggerFileBackend, :indexer}
{LoggerFileBackend, :indexer},
{LoggerFileBackend, :indexer_token_balances}
]
config :logger, :console,

Loading…
Cancel
Save