Spring Boot Metrics monitoring of Prometheus & Grafana (rpm)

Welcome to the second part of the Spring Boot Actuator tutorial series. In the first section , you learned a spring-boot-actuatormodule to do what, how to configure the spring boot applications and how to interact with a variety of actuator endpoints.

In this article, you will learn how to integrate external monitoring systems sprint boot Prometheus and graphics solutions Grafana .

At the end of this article, you will build a Prometheus and Grafana dashboard on your local computer for visual monitoring all metrics Spring Boot generated by the application.

Prometheus

Prometheus is an open source monitoring system, originated in SoundCloud . It consists of the following core components:

  • Data reptiles: periodically arrested metrics data according to the configuration time via HTTP.
  • time-series Database: stores all the metrics data.
  • Simple user interactive interface: visualization, query and monitor all of the metrics.

Grafana

Grafana so you can put data from different sources such as Elasticsearch, Prometheus, Graphite, influxDB and other data in a variety of colorful icons displayed.

It can also send alerts based on your metrics data. When an alarm state changes, it can notify you by email, slack or other means.

It is noteworthy that, Prometheus dashboard also has a simple icon. But a better graphical representation of the Grafana. This is why, in this article, we will integrate Grafana and Pormetheus to visualize metrics data.

Micrometer Prometheus Registry to increase your Spring Boot application

Spring Boot use Micrometer , an application component metrics, the metrics Actuator integrate to an external monitoring system.

It supports a variety of monitoring systems, such as Netflix Atalas, AWS Cloudwatch, Datadog, InfluxData, SignalFx, Graphite, Wavefront and Prometheus and so on.

In order to integrate Prometheus, you need to increase micrometer-registry-prometheusdependence:

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

Once you add the above-described dependency, Spring Boot automatically a configuration PrometheusMeterRegistryand CollectorRegistryto collect metrics and outputs the formatted data, so that the server can crawl Prometheus.

Metrics data for all applications is based on a called /prometheusendpoint to set is available. Prometheus server may periodically crawling the endpoint to acquire metrics data.

The analytical Spring Boot Actuator / prometheus Endpoint

First, you can page through the actuator endpoint-discovery ( HTTP: // localhost: 8080 / Actuator ) look at the prometheusendpoint.

"prometheus": {
"href": "http://127.0.0.1:8080/actuator/prometheus",
"templated": false } 

prometheusendpoint exposed Prometheus formatted data to the metrics server. You can prometheusEndpoint ( HTTP: 8080 / Actuator / Prometheus: // localhost see metrics data is exposed):

# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for  the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="nonheap",id="Code Cache",} 9830400.0 jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 4.3032576E7 jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 6070272.0 jvm_memory_committed_bytes{area="heap",id="PS Eden Space",} 2.63192576E8 jvm_memory_committed_bytes{area="heap",id="PS Survivor Space",} 1.2058624E7 jvm_memory_committed_bytes{area="heap",id="PS Old Gen",} 1.96608E8 # HELP logback_events_total Number of error level events that made it to the logs # TYPE logback_events_total counter logback_events_total{level="error",} 0.0 logback_events_total{level="warn",} 0.0 logback_events_total{level="info",} 42.0 logback_events_total{level="debug",} 0.0 logback_events_total{level="trace",} 0.0 ... 

Use Docker download and run Prometheus

Download Prometheus

You can use the docker pullcommand to download Prometheus docker image.

$ docker pull prom/prometheus

Once the image is downloaded, you can use the docker image lscommand to view a list of local image:

$ docker image ls
REPOSITORY                                   TAG                 IMAGE ID            CREATED             SIZE
prom/prometheus                              latest              b82ef1f3aa07        5 days ago          119MB

Prometheus 配置 (prometheus.yml)

Next, we need to configure Spring Boot Actuator Prometheus to capture the /prometheusmetrics in the data endpoint.

Create a prometheus.ymlfile, fill in the following:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.  - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'.  static_configs:  - targets: ['127.0.0.1:9090']  - job_name: 'spring-actuator'  metrics_path: '/actuator/prometheus'  scrape_interval: 5s  static_configs:  - targets: ['HOST_IP:8080'] 

In Prometheus document, the above profile is basic configuration file extension.

The above configuration items are more important spring-actuatorjob of scrape_configsoptions.

metrics_pathActuator is in prometheusthe path of the endpoint. targesIt includes Spring Boot application HOSTand PORT.

Be sure to replace HOST_IPyour Spring Boot application running computer's IP address. It is noteworthy that, localhostwill not work, because we are connected to the HOST machine from the docker container. You have to set the network IP address.

Use Docker run Prometheus

Finally, let's run in Docker in Prometheus. Use the following command to start a Prometheus server.

$ docker run -d --name=prometheus -p 9090:9090 -v <PATH_TO_prometheus.yml_FILE>:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

Be sure to replace <PATH_TO_prometheus.yml_FILE> save the configuration file path for Prometheus you created above.

After running the above command, docker will start in a container in a Prometheus server. You can see all of the container with the following command:

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
e036eb20b8ad        prom/prometheus     "/bin/prometheus --c…"   4 minutes ago       Up 4 minutes        0.0.0.0:9090->9090/tcp prometheus 

In Prometheus dashboard visualization Spring Boot Metrics

You can visit http: // localhost: 9090 Access Prometheus dashboard. You can query metrics by Prometheus query expression.

Here are some examples:

  • System CPU usage
system-cpu-usage.png
  • Delayed response to the API
response-latency.jpg

You can learn more from Prometheus official document Prometheus Query Expressions.

Use Docker download and run Grafana

Use the following commands to download and run the Docker Grafana:

$ docker run -d --name=grafana -p 3000:3000 grafana/grafana 

The above command will open in a Grafana Docker Container, and to provide services using port 3000 on the host.

You can use docker container lsto view a list of Docker container:

$ docker container ls
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
939dd22a7179        quay.io/prometheus/prometheus   "/bin/prometheus --c…"   14 minutes ago      Up 14 minutes       0.0.0.0:9090->9090/tcp   vigilant_neumann
1f94c46bcf5c        grafana/grafana                 "/run.sh"                22 hours ago        Up 22 hours         0.0.0.0:3000->3000/tcp   grafana

You can access the HTTP: // localhost: 3000 , and to log Grafana use the default account name (admin) password (admin).

Grafana metrics data import configuration of Prometheus

And visualized on the data metrics Grafana by Prometheus introduced in the following steps.

Prometheus increase data source on Grafana

data-source.png

The establishment of a dashboard chart

![add-cpu-usage.png](https://upload-images.jianshu.io/upload_images/793918-2e46ebd23646f1f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Add a query Prometheus

The default visualization

dashboard.png

You can Github to see the full Actutator demo applications.

Read the first part: the Spring the Boot Actuator: health inspections, audits, statistics and monitoring .

Read More Resources

Translation source



Author: alvin_wang
link: https: //www.jianshu.com/p/afc3759e75b9
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/sandea/p/11202749.html