Combining Production and Remote Debugging in Java Applications

Kalizi <Andrea>
Dev Genius
Published in
5 min readAug 10, 2021

--

Nowadays, systems are becoming more and more complex. A big application may be composed of microservices, depend on different APIs, be a monolith that’s hard to control because it contains old legacy code, or have lots of developers working together.

While web applications grow, your software architect (if you have one) may introduce standard practices like IOC (inversion of control) and TDD (test-driven development) to minimize production bugs or crashes. Alternatively, you might be using a framework that already implements these techniques, like Spring. However, even with these tools, bugs and crashes still occur and you have to deal with them!

So what can you do? You need to be able to see and manage bugs as they come up and, for this, I have found Lightrun Cloud to be most effective.

Lightrun homepage

Lightrun Cloud: What and Why

Lightrun Cloud is a free observability platform that gives you the tools you need to perform real-time debugging and logging on a staging or production environment using CLI or your favourite IDE (in my case, IntelliJ IDEA).

Installation is fast with IntelliJ IDEA: you open it, get to the settings page, look for the “Lightrun” plugin, and press “Install”.

Setup process

Once installed, restart IntelliJ IDEA, click “Login”, and it will open the browser and synchronize your IDE with your account.

Intellij IDEA after installing Lightrun

Once your IDE is ready, the platform will know automatically. What’s missing is the environment you want to debug. That’s the second step of the installation wizard.

Agent installation instructions

The agent should be installed on the environment you want to debug or test. It consists of a library that will be installed on your system and will be loaded with the java runtime environment, acting as a middleware between Java and your application. This way, you can practically debug it.

Note that the installation refers to using the agent with Java and a JAR file. But if you read the documentation carefully, you will notice that Lightrun Cloud provides support to most major frameworks and servers like Tomcat, Glassfish, Wildfly, Jetty, and Weblogic. This means that if your application is not a JAR file and you’re not testing the service and want to go straight into using the service, you can simply use a “Hello World” application to skip this step and correctly configure the agent with your environment.

Command to start an application using Lighrun Agent as Middleware

Windows and WSL are a great way to emulate a development and staging environment because many developers usually run their IDEs on Windows and deploy on a Linux machine that can be represented from WSL.

Building a Test Web App

Now that we have set up Lightrun, it’s time to get our hands dirty with a faulty fake web application. For this tutorial, I chose a fake Spring Boot Application packed in a JAR file to test on Lightrun. I picked this to demonstrate the use of Lightrun Cloud with a web app that depends on some faulty API that we consider amazingly stable, so stable that we don’t have a fallback error handling strategy.

Let’s set the stage. We have a good web application with feature and unit tests that mock the API we’re connecting to and a continuous delivery system that will trigger a build and deploy it directly to our environment.

To generate random data that will mock our fake API, we can use Java Faker, a simple library to generate fake data. You can add it to your application via Maven or Gradle.

Fake TestUser model code

The TestUser will be what our fake API will return. Suppose that the fake API has a failure rate of 30%. This means we can extract a number between 0 and 100 and, if this number is greater than 70, we can consider the API call failed. If this happens, we’ll use a RuntimeException when our system throws an unhandled exception.

Fake controller that will throw Exception randomly

To build our API as a JAR, we can easily tweak our pom.xml to use the spring-boot-maven-plugin.

Lightrun Cloud in Action

After starting your application with Lightrun Cloud connected, it will pop up on your IDE, spawning the connected environment.

Panel on IntelliJ IDEA

This means that it is currently connected to your processes and waiting for you to set the logging or the remote debugging. All you have to do is right-click on the line you want to log, open the “Lightrun” submenu, and click “Log”.

Log enabling

This will spawn a popup where you can set what you want to see on your logs:

Logging conditions and format

For testing purposes, I won’t set up any particular condition, just log the message. After you’ve added these logging conditions, the console will appear and the log will populate fast.

Logs

This is just the tip of the iceberg. Imagine using Lightrun Cloud in a real production environment where you can log everything coming from APIs, DBs, if conditions or AJAX calls for every user and for every instance of your application. Having multiple servers or instances of an application or multiple microservices will never be a problem with a logger like this. You can use Virtual Snapshot to log all local variables and expressions, like setting a breakpoint on your local environment. And you can do even more by measuring metrics about your application execution.

No More Edge Conditions

Lightrun Cloud is a powerful system that can improve your web applications and all your Java applications in general. You might wonder: If I am developing a web app from scratch, respecting all the well-known patterns and common auditing techniques to stem all bugs, will I need Lightrun Cloud then?

There’s no right answer to this question, but having a powerful tool like this will significantly improve your testing experience, especially for special cases like edge conditions or unknown states, catching and fixing all possible bugs in production!

--

--

IT Engineering Bachelor and still student 🎓 Mobile&Backend Dev 💻 Growth hacking Enthusiast ☕ Startupper 🚀 Metalhead 🤘🏻 With love, from Palermo ❤️