Monitoring a web application Prometheus
with Java Spring Boot.
Monitoring an application's health
and metrics helps us manage it better.
We can notice an optimized behavior
and get closer to its performance.
This works on any type of architecture,
but especially holds through when you're deploying
and developing a system that uses many micro services.
Monitoring each service can prove to be crucial
when it comes to maintaining your system.
Based on this information, you can actually draw
some scientific conclusions and decide
which microservices need some optimizations
or need to be scaled.
So the scaling scenario can be on a hardware level
that you see that you are running out of memory.
It can also be on some business metrics being
like the requests are not processing
as fast as it should be, so I'm gonna spin up
an extra instance but you need data for it.
And that's why application monitoring is so important.
So in this lecture, we basically are going to integrate
Prometheus monitoring with a web application
based on Spring Boot.
We'll use the following technologies.
So we'll be using Spring Boot, Spring Boot Actuator,
Spring Boot Actuator and Micrometer.
In the next lecture, I'll be showing you a demo
on how to set everything up
and how to actually develop some metrics,
but first I'm going to give you some theory around this.
So first up, you got the Spring Boot web app, as shown here.
It has two endpoints, api/demo,
and api/delayed demo.
So those are the two endpoints.
There's not actually a client running anymore here,
but it's just using Actuator
and Actuator in compared together
with Micrometer can expose an endpoint
that Prometheus can crawl directly.
So it scrapes the info directly
from the application.
This is just an endpoint of your API.
So as a client comes in, he queries your endpoint.
Prometheus also can just come in and query its endpoint.
We'll be setting it up in the next demo,
and everything will become clear,
but just to give you a high overview
of how everything actually works.
So Spring Boot Actuator, what is it exactly?
So it's a sub project of Spring Boot.
It delivers production ready endpoints,
like one of them is /actuator,
does everything it's prefixed with that,
and it's protected by default.
So you need to open it in the application.properties.
You can tune what metrics you want to expose
because some metrics you probably
don't want to expose to the world or we want to shield
or you don't mind that everything is exposed.
It all depends on the scenario
but in local development scenarios,
you can do something like that.
Like management.endpoints.map.exposure.include,
and then you will expose all the metrics
which are available.
In the beginning of this chapter,
I also mentioned Micrometer.
So what is Micrometer actually?
So Micrometer is a Vendor Neutral
application metrics facade.
So it doesn't actually create a metrics,
but it rather translates to sort of speak the metrics
that are used in Actuator,
which are ugly JSON files that Prometheus is unable
to read to actually files,
like the endpoints that Prometheus can understand,
but it also has support for other things.
So maybe it can come out handy in your scenario
depending on what use case you have,
or what tools that you have in your company.
If you're not really using Prometheus yet,
but you're using Datadog for example.
You can also use it, for example,
when your company's using Cloudwatch, Datadog,
InfluxDB or New Relic, and so on.
So Micrometer can also facade
all those other third party tools,
but in this case, we're going to use Prometheus, of course.
So what it actually does is transforms
the /actuator/metrics data
into data your monitoring system can understand.
So not those ugly JSON files,
but actually data in the Prometheus sense.
It's a beautiful endpoint that Prometheus can understand.
So it's very lightweight to install.
So it's basically just a Micrometer dependency
that, which is vendor specific,
and you have to just insert it into your application,
and you're all set.
So as you can see here, you got a pom.xml.
So first, we're gonna do the Spring Boot starter Actuator,
so everything is started up to use Actuator,
then we're going to insert that Micrometer core.
So that's basically the core of that project,
and then we're also going to insert
the vendor specific Micrometer dependency
which will be Micrometer registry Prometheus.
So this is all we need to set this actually up.
How we look in our code?
So as you can see, we're just gonna import
the io.micrometer.core.instrument.Metrics.
Then we're going to instantiate Counter
as you can see down beside GetMapping.
This is basically Spring Boot to get a mapping of that part.
And then we also gonna do @Timed.
What does this actually do?
So this creates a time series
named HTTP_server_request,
which by default, contains dimensions for the HTTP status
of the response HTTP method,
and the exception type if the request were to fail.
Another cool thing is that it also will provide
the actual pre variable substitution of the URI.
So you can see on what endpoint you're actually querying
on with this simple code snippet.
In the next lecture, I will do a demo
and put all those pieces together
so you can actually see how it will work.
Alright, let's move over to the demo.
And let's actually start instrumenting our application.
See you there.