Monitor Tomcat Java application with OpenTelemetry and SigNoz
Tags: java, monitoring, devops, opentelemetry
Published: 2021-09-20 13:14:33

OpenTelemetry is a vendor-agnostic instrumentation library. In this article, let's explore how to auto-instrument a Tomcat Java application using OpenTelemetry Java JAR agent.

After capturing telemetry data with OpenTelemetry, we will use SigNoz, an open-source full-stack observability tool, to visualize the data.

[![SigNoz GitHub repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yroh39ngk6sz6tv3z9y.png)](https://bit.ly/2WkkmL4)

Steps to get started with OpenTelemetry for Tomcat Java application:

- Installing SigNoz
- Installing sample Tomcat Java application
- Auto Instrumentation with OpenTelemetry Java agent
- Getting metrics and traces for Tomcat application in SigNoz

## Installing SigNoz

You can get started with SigNoz using just three commands at your terminal if you have Docker installed. You can read about other deployment options from [SigNoz documentation](https://signoz.io/docs/deployment/requirement/).

```
git clone https://github.com/SigNoz/signoz.git
cd signoz/deploy/
./install.sh
```

You will have an option to choose between ClickHouse or Kafka + Druid as a storage option. Trying out SigNoz with ClickHouse database takes less than 1.5GB of memory, and for this tutorial, we will use that option.

We have installed SigNoz on a Ubuntu VM in Azure cloud. You can access SigNoz UI at `http://IP_of_SigNoz:3000`. You can access SigNoz UI at port: 3000 of any host that you choose. In case of local host just use: `http://localhost:3000`

The application list shown in the dashboard is from a sample app called HOT R.O.D that comes bundled with the SigNoz installation package.

![SigNoz dashboard showing application list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/df7gclrc9c5dvsm9lgsh.png)<figcaption>SigNoz Dashboard</figcaption>



## Installing sample Tomcat Java application

**Prerequisites:**<br></br>
Make sure you have Tomcat installed on your system. If not, then you can download it from <a href="https://tomcat.apache.org/index.html" rel="noopener noreferrer nofollow" target="_blank">Apache Tomcat website</a>. For this tutorial I have used Tomcat 10.0.8.

### Steps to install sample Tomcat Java application:

- Download an example app packaged as a war file provided at Apache Tomcat official website - <a href="https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/" rel="noopener noreferrer nofollow" target="_blank">Sample Tomcat app</a>
![Sample applicationlink](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brrw3xoml3gn6urri0rl.png)


- The easiest way to run the sample app is to move it to webapps folder inside the Tomcat directory.

  ```
  cd Tomcat/webapps
  cp ~/Downloads/sample.war .
  ```

- Once you have copied the file in the webapps folder, get back to the Tomcat folder and run the app using the following command.

  ```
  cd ..
  bin/startup.sh
  ```

- Check if the sample app is running at: http://localhost:8080/sample/ . The sample should open up like below.

![Sample app running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asxnkp8d284l08kopt2m.png)<figcaption>Sample Tomcat app</figcaption>


  By clicking on the links shown on the page, you can see that it makes dummy calls to a page as part of the Hello World application.

## Auto Instrumentation with OpenTelemetry Java agent

OpenTelemetry has a very handy Java JAR agent that can be attached to any Java 8+ application for instrumenting Java applications.. The JAR agent can detect a number of popular libraries and frameworks and instrument it right out of the box. You don't need to add any code for that.

1. Download the [latest Java JAR agent](https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent-all.jar).
2. For the Tomcat application, you need to setup a few environment variables. You need to create and add a new file `setenv.sh` in your Tomcat bin folder. The `./startup.sh` command which is used to run the Java app will check for `setenv.sh` file and run the commands accordingly.
3. In `setenv.sh` file, paste the following environment variables using a code editor:

   ```
   export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/path/to/opentelemetry-javaagent-all.jar"
   export OTEL_METRICS_EXPORTER=none
   export OTEL_EXPORTER_OTLP_ENDPOINT=<IP of SigNoz Backend>:4317
   export OTEL_RESOURCE_ATTRIBUTES=service.name=<app_name>
   ```

   In the `CATALINA_OPTS` environment variable, you need to replace `path` with the path of the folder location where you have saved the OpenTelemetry Java agent downloaded in step 1.

   The `OTEL_EXPORTER_OTLP_ENDPOINT` specifies the endpoint for SigNoz's backend. In place of IP of SigNoz backend, you need to put the IP of host machine where SigNoz is installed. Also, remember to allow incoming requests to port 4317 of the machine where SigNoz backend is hosted.

   `OTEL_RESOURCE_ATTRIBUTES` is used to specify the service name of the service being monitored. So the final environment variables will look like below. Note that:

   ```
   export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/Users/cruxaki/Downloads/opentelemetry-javaagent-all.jar"
   export OTEL_METRICS_EXPORTER=none
   export OTEL_EXPORTER_OTLP_ENDPOINT=http://40.76.59.122:4317
   export OTEL_RESOURCE_ATTRIBUTES=service.name=Tomcat-SigNoz
   ```

   Make sure that you have saved this file as `setenv.sh` and in your Tomcat bin folder, because when starting up, Catalina checks this file for environment variables.

4. Now we need to restart our Tomcat Java app with the OpenTelemetry Java agent attached to it. Make sure you're at your Tomcat home folder and then restart the Tomcat server using following commands:

   ```
   bin/shutdown.sh
   bin/startup.sh
   ```

   Check out the sample Tomcat app again at  [http://localhost:8080/sample/](http://localhost:8080/sample/) and play around with it to generate some load. It might take 1-2 minutes before it starts showing up in the SigNoz dashboard.

   Below you can find your `Tomcat-SigNoz` app in the list of applications being monitored.

![Tomcat shows up in the list of applications monitored by SigNoz](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3rqjgill13ijql6gtlp.png)<figcaption>Tomcat-SigNoz shows up in the list of applications monitored by SigNoz</figcaption>


## Metrics and Traces of the Tomcat Java Application

SigNoz makes it easy to visualize metrics and traces captured through OpenTelemetry instrumentation.

SigNoz comes with out of box RED metrics charts and visualization. RED metrics stands for:

- Rate of requests
- Error rate of requests
- Duration taken by requests

![SigNoz UI showing charts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9m75541cp8pg1lfgq7ph.png)<figcaption>SigNoz UI showing popular RED metrics of application performance</figcaption>


You can then choose a particular timestamp where latency is high to drill down to traces around that timestamp.

![View of traces at a particular timestamp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twho6nxaun1ucll8xacb.png)<figcaption>View of traces at a particular timestamp</figcaption>


You can use flamegraphs to identify the issue causing the latency.

![Flamegraphs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjs5gg9v6pvovuh97b0g.png)<figcaption>Flamegraphs used for distributed tracing in SigNoz dashboard</figcaption>


## Conclusion

OpenTelemetry makes it very convenient to instrument your Spring Boot application. You can then use an open-source APM tool like SigNoz to analyze the performance of your app. As SigNoz offers a full-stack observability tool, you don't have to use multiple tools for your monitoring needs.

You can try out SigNoz by visiting its GitHub repo 👇<br></br>

[![SigNoz GitHub repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yroh39ngk6sz6tv3z9y.png)](https://bit.ly/2WkkmL4)

If you are someone who understands more from video, then you can watch the tutorial on how to monitor your Tomcat Java Application with OpenTelemetry and SigNoz below👇<br></br>

{% youtube 4obQilMqU4E %}

If you face any issues while trying out SigNoz, feel free to write to us at: support@signoz.io

<br></br>If you want to read more about SigNoz 👇
<br></br>

[Golang Application Performance Monitoring with SigNoz](https://signoz.io/blog/monitoring-your-go-application-with-signoz/)

[Monitor your Spring Boot application with OpenTelemetry and SigNoz](https://signoz.io/blog/opentelemetry-spring-boot/)