springboot-微服务监控管理-Prometheus工具!

B站影视 韩国电影 2025-06-04 13:18 1

摘要:Prometheus是一个开源的系统监控和警报工具包,最初是在SoundCloud上构建的。Prometheus自2012年面市至今,许多公司和组织都在使用,因此其拥有非常活跃的开发人员和用户社区。现在Prometheus是一个独立的开源项目,并在2016年加

Prometheus是一个开源的系统监控和警报工具包,最初是在SoundCloud上构建的。Prometheus自2012年面市至今,许多公司和组织都在使用,因此其拥有非常活跃的开发人员和用户社区。现在Prometheus是一个独立的开源项目,并在2016年加入了Cloud NativeComputing Foundation,成为继Kubernetes之后的第二个托管的项目。

Prometheus之所以流行,是因为其本身具有以下特性:

多维度的数据模型,可以通过指标名称和键值对定义时间序列数据。

支持PromQL查询语言,在多维数据模型中可以灵活地查询数据。

不依赖分布式的存储方式,单个服务器节点可以自主抓取数据。

通过HTTP PULL方式收集时间序列数据。

通过中间网关完成时间序列推送。

可以通过服务或静态配置发现监控目标。

支持多种图形和仪表板。

Prometheus框架如图8.1所示。

Prometheus是一个生态系统,里面包含了许多组件。其中,Prometheus Server用于抓取数据和存储时间序列数据,Pushgateway可以作为一个中转站运行短时间的任务,Alertmanager可以用来处理报警,Prometheus Web UI提供了Web接口,可用于简单的可视化、语句执行或者服务状态监控。

Prometheus可以针对纯数字的时间序列数据提供多维度的收集与查询功能,并且对外依赖特别少。如果要求查询的数据完全准确的话,则不建议使用Prometheus。

Prometheus的度量类型有以下4种:

Counter:某个指标的累计数据。

Gauge:某个指标的瞬时采集数据。

Histogra:一次抓取时返回的多个数据值,包括

_bucket{le=""}、

_sum和_count(等价于

_bucket{le="+Inf"})。

Summary:与Histogra类似,一次抓取也可以返回多个数据,包括

{quantile=""}、_sum和

_count。

Prometheus可以看作是一个监控平台,通过监控目标暴露出来的HTTP端点进行指标数据的抓取。本节将讲解如何安装和配置Prometheus,以及如何通过Prometheus监控应用程序。

(1)下载Prometheus包。下载地址为https://prometheus.io/download/,选择适合自己的包即可。下载之后,将安装包解压到本地。

(2)修改配置文件。解压Prometheus包后,路径下有一个名为prometheus.yml的配置文件,内容如下:

# 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).

# Alertmanager configuration

alerting:

alertmanagers:

-static_configs:

-targets:

# -alertmanager:9093

# 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=` to any

timeseries

scraped from this config.

-job_name: 'prometheus'

# metrics_path defaults to '/metrics'

# scheme defaults to 'http'.

static_configs:

-targets: ['localhost:9090']

配置文件中的global是全局配置,alerting.alertmagagers配置的是报警信息,rules_files是其他配置文件,scape_configs是一些定时抓取的URL。抓取配置可以发现应用程序暴露的端点信息,例如:

-job_name: 'user'

static_configs:

-targets: ['localhost:8081']

# HELP go_gc_duration_seconds A summary of the pause duration

of garbage

collection cycles.

# TYPE go_gc_duration_seconds summary

go_gc_duration_seconds{quantile="0"} 0

go_gc_duration_seconds{quantile="0.25"} 0

go_gc_duration_seconds{quantile="0.5"} 0

go_gc_duration_seconds{quantile="0.75"} 0.0010005

go_gc_duration_seconds{quantile="1"} 0.0089951

go_gc_duration_seconds_sum 0.0139949

go_gc_duration_seconds_count 13

# HELP go_goroutines Number of goroutines that currently

exist.

# TYPE go_goroutines gauge

go_goroutines 35

# HELP go_info Information about the Go environment.

# TYPE go_info gauge

go_info{version="go1.15.6"} 1

# HELP go_memstats_alloc_bytes Number of bytes allocated and

still in

use.

# TYPE go_memstats_alloc_bytes gauge

go_memstats_alloc_bytes 2.9598424e+07

# HELP go_memstats_alloc_bytes_total Total number of bytes

allocated,

even if freed.

# TYPE go_memstats_alloc_bytes_total counter

go_memstats_alloc_bytes_total 8.1223112e+07

# HELP go_memstats_buck_hash_sys_bytes Number of bytes used

by the

profiling bucket hash table.

# TYPE go_memstats_buck_hash_sys_bytes gauge

go_memstats_buck_hash_sys_bytes 1.470448e+06

图8.2 Prometheus管理界面

通过执行表达式,可以查询到想要的抓取数据。如图8.3所示为与查询时间序列http_server_requests_seconds_bucket相关的数据。

图8.3 Prometheus查询

可以选择以图表的方式展示查询结果,如图8.4所示。

图8.4 Prometheus指标统计

Prometheus存储的数据是以时间序列的方式展示的,通常定义metrics时会包括一个名称和多个键值对的标签,通用示例如下:

{=, ...}

例如,如果一个metrics名为api_http_requests_total,标签键值对是method="POST"和handler="/messages",则一个时间序列如下:

api_http_requests_total{method="POST", handler="/messages"}

更多的查询例子,可以参考Prometheus官方文档。

来源:大数据架构师

相关推荐