Monitoring your Ethereum node

Monitoring Ethereum node with JUnit

Is my Crypto node up and running (… and making $$$ for me)?

You’re operating an Ethereum node and you’d like to monitor it beyond simply checking availability of the TCP port? RealLoad can help with that.

There is a Java library called Web3j (see https://docs.web3j.io/ external link ) which allows you to connect to an Ethereum node and perform queries. I’ve leveraged this library in order to implement a simple Ethereum client that retrieves the latest block number from an Ethereum node.

I’ve implemented a simple JUnit test implementing this process, which will fail if:

  • An exception is raised while querying the Ethereum node; or
  • The block number is 0.

Obviously more complex could be implemented for an actual production monitoring test. For example, it would be possible to connect to multiple nodes and verify that the block number is similar, etc…

Putting it all together

This article assumes some familiarity with the RealLoad platform, it’s not a step-by-step guid on how to set this up. Our documentation will assist with the details, see here external link .

First, you’ll have to prepare a Java JUnit test executing the test logic. You can use this code as a starting point (make sure you update the value of the string ETHEREUM_NODE_URL to point to an actual URL):

EthereumClient.java

import com.dkfqs.tools.javatest.AbstractJUnitTest;
import static com.dkfqs.tools.javatest.AbstractJUnitTest.isArgDebugExecution;
import static com.dkfqs.tools.javatest.AbstractJavaTest.LOG_ERROR;
import static com.dkfqs.tools.logging.LogAdapterInterface.LOG_DEBUG;
import com.dkfqs.tools.logging.MemoryLogAdapter;
import okhttp3.OkHttpClient;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;

public class EthereumClient extends AbstractJUnitTest {

    private final MemoryLogAdapter log = new MemoryLogAdapter();  // default log level is LOG_INFO
    private final String ETHEREUM_NODE_URL = "https://ETH_NODE_URL/";

    @Before
    public void setUp() {
        if (isArgDebugExecution()) {
            log.setLogLevel(LOG_DEBUG);
        }
        openAllPurposeInterface();
    }

    @Test
    public void TestWeb3NodeConnection() {
        okhttp3.OkHttpClient httpClient = new OkHttpClient();
        Web3j web3 = Web3j.build(new HttpService(ETHEREUM_NODE_URL, httpClient, true));
        log.message(LOG_DEBUG, "Testing node: " + ETHEREUM_NODE_URL);

        try {
            Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
            log.message(LOG_DEBUG, "Web3 client version: " + web3ClientVersion);

            EthBlockNumber result = web3.ethBlockNumber().send();
            long latestBlockNumber = 0;
            latestBlockNumber = result.getBlockNumber().longValue();
            log.message(LOG_DEBUG, "last block #: " + latestBlockNumber);
            Assert.assertNotEquals("Latest blockNumber is 0", 0, latestBlockNumber);
            Assert.assertEquals(true, true);

        } catch (Exception e) {
            log.message(LOG_ERROR, "Error while testing node " + ETHEREUM_NODE_URL, e);
            Assert.assertEquals("Web3 connection test failed: " + e.getMessage(), true, false);
        } finally {
            web3.shutdown();
            httpClient.connectionPool().evictAll();
        }
    }

    @After
    public void tearDown() {
        closeAllPurposeInterface();
        log.writeToStdoutAndClear();
    }
}

The above code requires the dependencies shown in this screenshot to run. You can download these dependencies from https://mvnrepository.com/ external link or from here .

Upload the dependencies to the Java Resource Library in your RealLoad portal account, so that you can select them when you attempt to compile the above Java code:

Then, configure a monitoring job to execute the JUnit test at a specific interval from suitable locations. This screenshot shows the results after approx. a day of monitoring, at hourly intervals:

Finally configure your alerting channels, to make sure you’re made aware that something’s not working as expected.

Need help?

The above article requires you to be somewhat familiar with the RealLoad platform.

If you’d like us to configure a tailored Web3 / Ethereum Synthetic Monitoring test, plz do let us know. What we’ve illustrated in this article is just a basic use case, we can implement a test suiting your specific needs and configure it for you, so that you don’t have to worry about the technicalities…

Thank you for reading and do not hesitate to reach out should you have any Qs by emailing us at support@realload.com .