prometheus monitoring springboot application simple introduction and detailed explanation

  • 2021-07-24 10:59:54
  • OfStack

For springboot applications, the following steps are required

The springboot application opens endpoint, adds actuator since and promethus dependencies


<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
 <groupId>io.micrometer</groupId>
 <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

In the yml file or properties file, add the following configuration:


management:
 endpoints:
  jmx:
   exposure:
    include: "*"
  web:
   exposure:
    include: "*"
 metrics:
  export:
   datadog:
    application-key: ${spring.application.name}

It should be noted here that the * sign needs double quotation marks.

After the above two steps are completed, the rest is to add registry:


@Bean MeterRegistryCustomizer<MeterRegistry> configurer(
  @Value("${spring.application.name}") String applicationName) {
  return (registry) -> registry.
    config().
    commonTags("application", applicationName);
}

For the application of springboot, this is basically completed. The next step is to start promethus.

Configuring prometheus

First of all, it should be placed in prometheus ": Prometheus-Monitoring system & time series database"

On the download page, select when to download the version. It is recommended to download the tar. gz package. After downloading, extract it. In the right path.

Here are the directories and files of prometheus:

1. prometheus adopts the configuration mode of yml file.
2. Under the root directory, there is an prometheus. yml configuration file. The initialization contents of the file are as follows:


global:
 scrape_interval:   15s  #  This is the frequency of each data phone 
 evaluation_interval: 15s  #  Evaluate the frequency of alarm rules. 

rule_files:
 # - "first.rules"
 # - "second.rules"

scrape_configs:        #  Through the configuration control here, prometheus Resources monitored 
 - job_name: prometheus   # prometheus Self-default 
  static_configs:
   - targets: ['localhost:9090'] #  The default exposure is 9090 Port service 

global is a global configuration. See the notes above for details.

3. Add our application to monitor springboot


- job_name: 'spring-sample'
  metrics_path: 'actuator/prometheus'  #  Here we springboot Exposed endpoint
  scrape_interval: 5s          #  Information collection time is interval 5 Seconds 
  static_configs:
  - targets: ['localhost:8778']     #  This is springboot Exposed addresses and ports 

4. After these configurations are completed, prometheus,./prometheus-config.file=prometheus. yml can be started, and the service can be started. Visit official website specifically.

Configuring grafana

Download grafana and start it directly.

1. See official website for startup command:./grafana-server web

2. Configure datasource and select prometheus. There is a very important point to pay attention to. I see many people on the Internet talking about how to monitor springboot application with prometheus. It is estimated that they didn't actually build it. At interval, the default is a number, such as 15, which means 15 seconds. When adding dashboard, you will find a red dot in the upper left corner of the monitoring icon, and report errors: Invalid interval string, expecting a number followed by one of "Mwdhmsy". The solution to this error is to add "s" after these time intervals. Problem solving.

3. Select dashboard, enter a template in import, and go to dashboards to find your corresponding template. We choose 4701 template of jvm here, and then we can see your monitoring information of springboot. At this point, the whole construction is completed.


Related articles: