Your starter project will have some pre-built classes. You can delete all of them, except for a class named Main:
- Now create a class named MpMetricsResource. Set up its header like this:
@Path("/ch08mpmetrics")
@RequestScoped
public class MpMetricsResource {
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
}
- Then, let's create a method that will perform some action so we can monitor its metrics. Inside the MpMetricsResource class, create this:
private String getResource() {
Client client = null;
String response;
try {
client = ClientBuilder.newClient();
WebTarget target =
client.target("https://eldermoraes.com/book");
response = target.request()
.header("Content-Type", "application/json")
.get(String.class);
} finally {
if (client != null) {
client.close();
}
}
return response;
}
- Now, we will create two methods that will generate metrics based on this preceding one. Remember, keep adding these methods to the MpMetricsResource class.
The first one will generate the Timed metrics, which are metrics based on the time that it takes the method to execute its tasks:
@Timed(name = "getResourceTimed")
@Path("/timed")
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getResourceTimed() {
String response = getResource();
return JSON.createObjectBuilder()
.add("message", response)
.build();
}
- The second one will generate the Metered metrics, which are metrics based on the frequency at which the method is called:
@Metered(name = "getResourceMetered")
@Path("/metered")
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getResourceMetered() {
String response = getResource();
return JSON.createObjectBuilder()
.add("message", response)
.build();
}
Your MpMetricsResource is ready! You only need an Application class that will handle it for you. Just as a reminder, the Application class is a class specified by the JAX-RS specification to handle the REST endpoints inside your project.
- Create a class named MpMetricsApplication and code it like this:
@ApplicationScoped
@ApplicationPath("/")
public class MpMetricsApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return CollectionsHelper.setOf(MpMetricsResource.class);
}
}
So, you are ready to build and run your project to collect its metrics:
- First, let's build it. Inside the project folder, execute this:
mvn package
- It will build your project and package it. Once it is done and if everything finished without any errors, execute this:
java -jar target/ch08-mpmetrics.jar
You should see something like this on your Terminal:
- Now your microservice is up and running, so you need to call the methods you've just created to be able to generate and check its metrics. Their URLs are as follows:
http://localhost:8080/ch08mpmetrics/metered
http://localhost:8080/ch08mpmetrics/timed
Call each one of them a few times, so the metrics can be generated and checked. Fortunately, you don't have to do anything more for both cases!
- These two metrics you've just created are automatically generated by the framework each time these methods are called. And for checking them, there is a default REST endpoint already available in your microservice (no, you didn't create it!). Just open this URL:
http://localhost:8080/metrics/application
If so, congratulations! You are collecting these metrics from your microservice.