Tracing and performance monitoring is a crucial practice that allows us to identify problems quickly and provides a good starting point for investigation and remediation.
To start tracing Java applications using LogSense, one needs to download the Java agent and provide it to the JVM. The agent will instrument the code and automatically detect several types of operations, such as servlet requests, database access, calls to message queues, NoSQL access and more. The agent provided by LogSense leverages the OpenTracing initiative, which is aimed at creating vendor-neutral APIs and instrumentation for distributed tracing.
In this post, I will show how easy it is to use the LogSense tracing agent and how quickly you can begin to monitor application performance.
For the sake of the example, we'll use At Sea Sample Shop App, a simple 3rd-party project based on Spring Boot, which is run using Docker.
Running the Project
Let's start with cloning the repository:
$ git clone https://github.com/dockersamples/atsea-sample-shop-app $ cd atsea-sample-shop-app
In the project directory, we can find the Dockerfile responsible for running the Java Web server in
app/Dockerfile. After looking at its content, we can see the piece responsible for running the virtual machine.
ENTRYPOINT ["java", "-jar", "/app/AtSea-0.0.1-SNAPSHOT.jar"]
It essentially runs the app using the jar that was previously copied into the /app directory as specified in the Dockerfile.
Setting up the agent
To modify it a bit, we will make following changes:
- download the LogSense agent JAR (and since the Dockerfile is based on Alpine - a minimal distribution - we need to add a dependency that will allow us to handle https requests using wget)
- set the environment variable with the token, which can be found in user portal
-javaagentoption that will tell Java to use the instrumentation
After these changes, the same segment of the file should look like following:
RUN apk add --update openssl RUN wget https://github.com/collectivesense/logsense-opentracing-agent/releases/download/v1.0.0/logsense-opentracing-agent-1.0.0.jar ENV LOGSENSE_TOKEN 555aaaa-dbbb-4777-8888-4444eeaeeeee ENTRYPOINT ["java", "-javaagent:logsense-opentracing-agent-1.0.0.jar", "-jar", "/app/AtSea-0.0.1-SNAPSHOT.jar"]
Running the project
Now, we can run the app normally following the instructions in the AtSea repository.
(create secrets as specified in README.md)
$ docker-compose up --build
The following screenshot presents two example records representing servlet actions together with their path, duration and other properties.
Monitoring the Performance
Since we have the data available, let's build some custom charts that will allow us to track performance of the application and understand what's going on there.
Building overall execution duration chart with percentiles
There are several ways to build execution duration charts. One is to right-click on
logs.duration_us and select the AVG option. This action will create a chart showing the average over time. Next, a few more instances of the value can be dropped on the chart. Let's select 5-th and 95-th percentile for them.
Grouping the duration by resource
We see there were some fluctuations of the duration, but we still need to know which operation and resource caused the fluctuations. To find out, we can create another chart that is very similar to the previous one but grouped by the
logs.http.url field. After sorting, click on the resource to add it to the filter.
Traces and spans
Each trace might contain one or more spans, which represent the actions performed within the scope of given request. They are identified by
logs.trace_id (for being a member of a given trace) and
logs.parent_span_id (for identifying the span that triggered given operation). To filter for a given trace, it is enough to click on
logs.trace_id to add it to the search bar.
LogSense offers a fast and intuitive way to use tracing and performance monitoring to identify problems, and quickly work to investigate and remediate any issues. If you want to learn how to use LogSense for tracing java apps in your production environment, give us a shout.