Monitoring REST requests with Stagemonitor and Elasticsearch
Tags: monitoring, stagemonitor
Published: 2019-10-31 15:41:07

---
title: Monitoring REST requests with Stagemonitor and Elasticsearch
published: true
description: 
tags: [monitoring, stagemonitor]
---

[Stagemonitor](https://www.stagemonitor.org/) is a solution to let you monitor your java application in a very simple way. And it is free. While you may not have all the information some of the paid tools will give you, it still gives you a lot of useful information.
The application I am going to use as an example, is very simple, all it has is this:

```
import java.time.LocalDateTime;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.stagemonitor.tracing.Traced;

@Path("/user")
public class UserResource {

    @GET
    @Path("/hi")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Traced
    public Response sayHi() {
        String result = "Hi world @ " + LocalDateTime.now();

        return Response.status(Response.Status.OK).entity(result).build();
    }
}
```

It will be compiled with jdk 11, and will be deployed to wildfly 15.
To initialize the application monitoring, you need to startup stagemonitor yourself:

```
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import org.stagemonitor.core.Stagemonitor;

@Singleton
@Startup
public class StageMonitorHandler {
    @PostConstruct
    public void init() {
        Stagemonitor.init();
    }

    @PreDestroy
    public void shutDownStagemonitor() {
        Stagemonitor.shutDown();
    }
}
```

Since the application will run on a application server, you have to add the bytebuddy agent library to the classpath. To do this, you can override your $JAVA_OPTS, with something like this:

```
JAVA_OPTS="$JAVA_OPTS -javaagent:/opt/bytebuddy/byte-buddy-agent-1.8.11.jar -Dstagemonitor.property.overrides=stagemonitor.properties
```

The version of the bytebuddy agent has to be the same as the one used by stagemonitor. In this example, the stagemonitor version is 0.88.9.
The properties file that was added above, is packaged with the application and contains some stagemonitor configurations.

```
stagemonitor.applicationName=stagemonitor
stagemonitor.instrument.include=com.github
stagemonitor.instanceName=stagemonitor-dev
stagemonitor.reporting.elasticsearch.url=http://elasticsearch:9200
stagemonitor.tracing.reporting.log=true
```

In the project pom.xml file, you need to add at least this dependencies:

```
<properties>
  <stagemonitor.version>0.88.9</stagemonitor.version>
</properties>
...
<dependency>
  <groupId>org.stagemonitor</groupId>
  <artifactId>stagemonitor-tracing-elasticsearch</artifactId>
  <version>${stagemonitor.version}</version>
</dependency>
<dependency>
  <groupId>org.stagemonitor</groupId>
  <artifactId>stagemonitor-web-servlet</artifactId>
  <version>${stagemonitor.version}</version>
</dependency>
<dependency>
  <groupId>org.stagemonitor</groupId>
  <artifactId>stagemonitor-tracing</artifactId>
  <version>${stagemonitor.version}</version>
</dependency>
<dependency>
  <groupId>org.stagemonitor</groupId>
  <artifactId>stagemonitor-logging</artifactId>
  <version>${stagemonitor.version}</version>
</dependency>
<dependency>
  <groupId>org.stagemonitor</groupId>
  <artifactId>stagemonitor-jvm</artifactId>
  <version>${stagemonitor.version}</version>
</dependency>
<dependency>
  <groupId>org.stagemonitor</groupId>
  <artifactId>stagemonitor-os</artifactId>
  <version>${stagemonitor.version}</version>
</dependency>
<dependency>
  <groupId>org.stagemonitor</groupId>
  <artifactId>stagemonitor-dispatcher</artifactId>
  <version>${stagemonitor.version}</version>
</dependency>
```

And of course, you will need to have elasticsearch running on (http://elasticsearch:9200)[http://elasticsearch:9200] and also kibana.
The versions used for this test were 6.5.4.
After all this is setup, you can start making requests and start to see some cool charts in kibana.