prometheus API使用

3,786 阅读3分钟

一 API 概览

HTTP API

Prometheus 所有稳定的 HTTP API 都在 /api/v1 路径下。当我们有数据查询需求时,可以通过查询 API 请求监控数据,提交数据可以使用 remote write 协议或者 Pushgateway 的方式。

支持的 API

image.png

认证方法

默认开启认证,因此所有的接口都需要认证,且所有的认证方式都支持 Bearer Token和 Basic Auth。

Bearer Token

Bearer Token 随着实例产生而生成,可以通过控制台进行查询。了解 Bearer Token 更多信息,请参见 Bearer Authentication。

Basic Auth

Basic Auth 兼容原生 Prometheus Query 的认证方式,用户名为用户的 APPID,密码为 bearer token(实例产生时生成),可以通过控制台进行查询。了解 Basic Auth 更多信息,请参见 Basic Authentication。

数据返回格式

所有 API 的响应数据格式都为 JSON。每一次成功的请求会返回 2xx 状态码。

无效的请求会返回一个包含错误对象的 JSON 格式数据,同时也将包含一个如下表格的状态码:

所有 API 的响应数据格式都为 JSON。每一次成功的请求会返回 2xx 状态码。

无效的请求会返回一个包含错误对象的 JSON 格式数据,同时也将包含一个如下表格的状态码:

image.png

二 数据写入

操作场景 类似 flink job 上报数据的场景,我们需要通过 API 直接将数据写入 Prometheus,因为这些 job 的生命周期可能会很短,来不及等待 Prometheus 来拉取数据。写入数据可以直接使用 Remote Write 协议或者 Pushgateway 的方式。

Remote Write

POST /api/v1/prom/write

Remote Write 是 Prometheus 标准的协议,更多介绍可以参见 Prometheus-remote write。使用 remote write 我们可以把 VPC 内其他 Prometheus 的数据写入到 Prometheus 监控服务中,对于数据的稳定性提升和迁移,都不失为一种不错的方案。

Pushgateway和NodeExporter

在Prometheus的架构设计中,Prometheus Server并不直接服务监控特定的目标,其主要任务负责数据的收集,存储并且对外提供数据查询支持。因此为了能够能够监控到某些东西,如主机的CPU使用率,我们需要使用到Exporter。Prometheus周期性的从Exporter暴露的HTTP服务地址(通常是/metrics)拉取监控样本数据。 可以是一个独立运行的程序独立于监控目标以外,也可以是直接内置在监控目标中。只要能够向Prometheus提供标准格式的监控样本数据即可。

http://localhost:9091/metrics

直接点metric这个tab是空的,因为是路由到#,还是直接访问这个url

go_gc_duration_seconds 表示 垃圾回收周期暂停持续时间的summary go_gc_duration_seconds{quantile="0"} 3.05e-05 go_gc_duration_seconds{quantile="0.25"} 4.18e-05 go_gc_duration_seconds{quantile="0.5"} 4.65e-05 go_gc_duration_seconds{quantile="0.75"} 8.34e-05 go_gc_duration_seconds{quantile="1"} 0.0020116 go_gc_duration_seconds_sum 0.0114824 go_gc_duration_seconds_count 94 比如 表示 50% 的 go gc duration seconds 是小于等于 4.65e-05 秒的

pushgateway_http_push_duration_seconds 推送到 Pushgateway 的 HTTP 请求持续时间。 pushgateway_http_push_duration_seconds{method="put",quantile="0.1"} 0.0013008 pushgateway_http_push_duration_seconds{method="put",quantile="0.5"} 0.0027658 pushgateway_http_push_duration_seconds{method="put",quantile="0.9"} 0.0092754 pushgateway_http_push_duration_seconds_sum{method="put"} 0.013342 pushgateway_http_push_duration_seconds_count{method="put"} 3

pushgateway_http_push_size_bytes 推送到 Pushgateway 的 HTTP 请求大小 pushgateway_http_push_size_bytes{method="put",quantile="0.1"} 407 pushgateway_http_push_size_bytes{method="put",quantile="0.5"} 408 pushgateway_http_push_size_bytes{method="put",quantile="0.9"} 408 pushgateway_http_push_size_bytes_sum{method="put"} 1223 pushgateway_http_push_size_bytes_count{method="put"} 3

pushgateway_http_requests_total Pushgateway 处理的 HTTP 请求总数,不包括爬虫。 pushgateway_http_requests_total{code="200",handler="push",method="put"} 3 pushgateway_http_requests_total{code="200",handler="status",method="get"} 1


from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from prometheus_client.exposition import basic_auth_handler

def my_auth_handler(url, method, timeout, headers, data):
    username = 'admin'
    password = 'admin'
    return basic_auth_handler(url, method, timeout, headers, data, username, password)
registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime_xuliang', 'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
push_to_gateway('localhost:9091', job='batchA', registry=registry, handler=my_auth_handler)

参数抓到了 image.png

除了这些以外,在当前页面中根据物理主机系统的不同,你还可能看到如下监控指标:

node_boot_time:系统启动时间
node_cpu:系统CPU使用量
nodedisk*:磁盘IO
nodefilesystem*:文件系统用量
node_load1:系统负载
nodememeory*:内存使用量
nodenetwork*:网络带宽
node_time:当前系统时间
go_*:node exporter中go相关指标
process_*:node exporter自身进程相关运行指标