As the latest infrastructure demands changed, the existing monitoring solutions started to become obsolete and the need for a monitoring solution crafted to meet the new changes was required. And once again to save the day and to ensure mankind’s progress appears Prometheus.
Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. (source)
Through Prometheus we can monitor our code in either of the two ways • The Whitebox way: Using the standard library for the supported languages • The Blackbox way: Using an agent(called “Exporters”)
Following is a simple Hello World! Spring Boot application to which we will be adding Prometheus instrumentation.
Now that we have our code ready let’s create a pom.xml file so that we can build it using Maven.
Once that’s ready we will build the application jar using the command mvn clean package. The jar file can be found at target/prometheus_example-0.0.1-SNAPSHOT.jar when executed it will print Hello World! at http://localhost:8080
Now that we have our sample application working it’s time to add some Prometheus instrumentation to it. We will be adding support for two metrics types Counter and Histogram to our sample application. First off all we need to include the standard Prometheus libraries for Spring Boot to the dependency list and for that we will be adding the following lines in the pom.xml file
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.4.0</version>
</dependency>
As we have our pom.xml file ready now, let’s go ahead and add the support for the Prometheus metrics in our code.
To enable the support for Counter and Histogram metrics we have to first import the required libraries
import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
import io.prometheus.client.Counter;
import io.prometheus.client.Histogram;
and then create both the metric types
static final Counter requests = Counter.build()
.name("requests_total").help("Total number of requests.").register();
static final Histogram requestLatency = Histogram.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
Once created these metric types will be shared through all instances of the object. This will provide the metric a name and will add some additional HELP and TYPE text with those metrics. Now to start using the Counter metric type all we need to do is call the inc() method whenever we want to increment the metric’s count by one. Since the Histogram metric sample observations and is often used to monitor things like request latency or request sizes therefore it is used around a try…finally block as below
// Start the histogram timer
Histogram.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
// Stop the histogram timer
requestTimer.observeDuration();
}
Now that the setup is done let’s build the jar using Maven through mvn clean package and execute the newly built jar. If we visit http://localhost:8080/ you’ll see Hello World!, and on http://localhost:8080/prometheus you can see the metrics that are currently being published by our application.
Note: As Prometheus takes advantage of Spring Boot actuator to gather and publish the metrics. Therefore, we will have to disable the default Spring Boot security and you will notice that apart from the metrics we explicitly defined in our application additional JVM metrics are also being published.
Continued in part 2 of the series.