Prometheus Flashcards

1
Q

Links

A

https://www.tutorialworks.com/spring-boot-prometheus-micrometer/
Source Code https://github.com/tutorialworks/spring-boot-with-metrics

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Working

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Micrometer

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Micrometer and Spring Boot 2

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Adding Prometheus to Spring Boot

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Exposing the endpoint

A

management.endpoints.web.exposure.include=health,info,prometheus
Endpoint will be available at /actuator/prometheus

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Custom Metrics

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Timer Metric Example

A

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());
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Timer Metric Example , metrics exposed

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Getting metrics into Prometheus

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Observing metrics in Prometheus

A

Go to graphs section
search by greeting_time_seconds_max and create a graph out of it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly