Debugging Live Java Applications with Lightrun

Java is a wonderful/powerful/versatile language/platform. It’s very easy to debug under normal conditions but when it’s deployed in a remote environment this might be more challenging. Especially at scale. 

This is true for a simple Java application or for a server application running on tomcat, spring, JavaEE or pretty much anything else out there. We need a way to observe live applications and debug issues without interrupting user flows.

This is where Lightrun steps in to make this process trivial and secure without risking your uptime. Notice that this short tutorial uses a simple prime main calculation app for demonstration purposes. But you can use pretty much any application with Lightrun. The one constraint is that it’s “long running”, so a hello world application will end too quickly and we won’t have time to attach to it. 

Here is the code to the prime main application we’ll use in this tutorial: https://gist.github.com/shai-almog/e400134f01decc9639230a6a99d51eab

Step 1 – Build the Test Application

Download the file, open the directory where it resides and compile the project in intellij.

Step 2 – Install Lightrun & Run Prime Main

If you didn’t do this yet go to https://app.lightrun.com and follow the steps to create an account. Download the IDE plugin and set up the agent on your server. I won’t replicate the steps here as they are pretty clear on the website.

You can download the agent into the project directory then run the app using:

java -agentpath:PATH_TO_AGENT_DIRECTORY/lightrun_agent.so -classpath out/production/PrimeJava PrimeMainMR

Notice you need to replace PATH_TO_AGENT_DIRECTORY with the right path. Try to avoid shortcuts like ~ which might cause issues. Also you might need to fix the classpath to match the name you gave to the project.

Notice that if this works you should see no output, the process will just keep running and calculating.

You can now install the plugin and login via the IDE.

Step 3 – Inject a Log

In the IDE open the PrimeMainMR.java file. Go to line 84 and right click on it. Select Lightrun and Log:

In the Log dialog type the following into the format field: “Prime number {cnt} is {i}”

This injects a new log into the application. Notice the variables in the curly braces. We can use any valid read only Java expression, including method calls in those braces. Now if you look at the app you’ll notice printouts like this:

 

Oct 24, 2021 12:39:46 PM PrimeMainMR main

INFO: LOGPOINT: Prime number 22020 is 249721

Oct 24, 2021 12:39:46 PM PrimeMainMR main

INFO: LOGPOINT: Prime number 22021 is 249727

Oct 24, 2021 12:39:46 PM PrimeMainMR main

INFO: LOGPOINT: Prime number 22022 is 249737

Oct 24, 2021 12:39:46 PM PrimeMainMR main

INFO: LOGPOINT: Prime number 22023 is 249749

Oct 24, 2021 12:39:46 PM PrimeMainMR main

INFO: LOGPOINT: breakpointId: [c7b98d7e-2a2d-4c4b-89b3-3e730f9b93a5]: Logpoint is paused due to high call rate until log quota is restored

Oct 24, 2021 12:39:46 PM PrimeMainMR main

INFO: LOGPOINT: Prime number 22059 is 250169

 

Notice that printouts were removed to preserve low CPU usage. If you use complex expressions in the log or print out too much data Lightrun will throttle you. This keeps your servers stable.

 

Once you’re done with the log you can delete it from the right click menu or the tree on the right.

Step 4 – Add a Snapshot

Follow the same steps as the previous step, but select adding a snapshot instead of a log (notice a snapshot doesn’t have a format entry). I suggest doing it on line 23 so it will be more interesting:

The snapshot is like a debugging breakpoint, but it doesn’t stop the execution of the application. As such it can’t break your live application flow.

You can see the stack trace on the left which you can navigate through. You can see variable values on the right matching the current stack frame and you can use those to understand what went on in a particular phase.

Notice that all Lightrun actions (snapshots, logs and metrics) support conditional execution. That means you can use an expression to limit them to a specific case. E.g. userName.equalsIgnorCase(“Shai”) which can limit logs/snapshots only to the user Shai.

There’s So Much More

I barely scratched the surface here. I didn’t touch metrics, tags or complex usages. Installation and setup are also pretty elaborate in some cases, so if you run into trouble don’t hesitate to contact our support team via the chat widget on the website. 

Lightrun can give your application a level of observability that hasn’t been seen before.

Ready to get started ?