Prometheus Flashcards
Links
https://www.tutorialworks.com/spring-boot-prometheus-micrometer/
Source Code https://github.com/tutorialworks/spring-boot-with-metrics
What is
It is a time-series database, which stores a sequence of data points, across time.
Prometheus is used to store metrics and performance data from applications. And this allows you to perform time-series analysis of metrics.
A simple example of time-series analysis is looking at the number of requests your application has processed over time. You can see how many requests you processed in the last hour, or the last day, or the last week.
Working
Prometheus runs separately from your application. So you can run a single instance of Prometheus, and it can fetch and store metrics from dozens of your apps.
Prometheus uses a pull-based approach for getting metrics. It uses a set of instructions, to determine which applications to fetch metrics from, and how to do it.
Prometheus polls your application for its latest metrics data – this is known as scraping
Micrometer
Micrometer can help you take measurements from your application, and publish those metrics ready to be scraped by many different applications, including Prometheus.
Micrometer acts as a facade – an intermediate layer – between your application and some of the more popular monitoring tools. This makes it easier to publish metrics to Prometheus and other tools like Elastic, Datadog or Dynatrace.
Micrometer and Spring Boot 2
From Spring Boot 2, support for Micrometer is available right out of the box. So you need to know only how to add Prometheus support to Spring Boot.
Adding Prometheus to Spring Boot
The first dependency we need to add is the Spring Boot Actuator. This is the part of Spring Boot which exposes some APIs, for health-checking and monitoring of your apps
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
This will configure the Actuator, which already includes Micrometer.
Next, you will need to add the Micrometer registry dependency which specifically enables Prometheus support.
This allows the metrics collected by Micrometer to exposed in a format that will be understood by Prometheus:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
Exposing the endpoint
management.endpoints.web.exposure.include=health,info,prometheus
Endpoint will be available at /actuator/prometheus
Custom Metrics
There are 3 types of Micrometer metrics
Gauge Size of a collection, number of running threads, number of messages on a queue, memory usage
Counter Total number of orders processed, total tasks completed, etc.
Timer - Example Method execution time, request duration, time taken to boil an egg.
Timer Metric Example
To check the time taken to execute a method
Need to add an AOP dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
We also need to register the TimedAspect bean in our Spring context. This will allow Micrometer to add a timer to custom methods. Register the bean to your @SpringBootApplication or @Configuration class
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
Then, find the method that you want to time, and add the @Timed annotation to it. Use the value attribute to give the metric a name.
// io.micrometer.core.annotation.Timed
@Timed(value = “greeting.time”, description = “Time taken to return greeting”)
public Greeting getGreeting() {
return new Greeting());
}
Timer Metric Example , metrics exposed
You will see 3 metrics exposed
greeting_time_seconds_count (count of calls to this method in last 1 second)
greeting_time_seconds_max (max time to return a greeting) - total time taken in executing that method
greeting_time_seconds_sum
Getting metrics into Prometheus
prometheus.yml
scrape_configs:
- job_name: ‘spring boot scrape’
metrics_path: ‘/actuator/prometheus’
scrape_interval: 5s
static_configs:
- targets: [‘localhost:8080’]
The settings file configures Prometheus to scrape metrics from localhost:8080/actuator/prometheus every 5 seconds. This frequency can be changed, depending on how granular you want to get.
Observing metrics in Prometheus
Go to graphs section
search by greeting_time_seconds_max and create a graph out of it.