一、操作场景
当我们有数据查询需求时,可以通过查询 API 请求监控数据。
二、APPID/Token 获取方式
托管 Prometheus API 使用需要通过 APPID + Token 的方式进行鉴权访问。
·APPID 获取地址。
·Token 从对应 Prometheus 实例的基本信息中获取。
三、查询 API 接口
GET /api/v1/queryPOST /api/v1/query
四、查询参数
·query=<string>: Prometheus:查询表达式。
·time=<rfc3339 | unix_timestamp>:时间戳, 可选。
·timeout=<duration>:检测超时时间,可选。 默认由 -query.timeout 参数指定。
根据时间范围查询我们需要的数据是我们面临的最多的场景,这时我们需要用到 /api/v1/query_range 接口,示例如下:
$ curl -u "appid:token" 'http://IP:PORT/api/v1/query_range?query=up&start=2015-07-01T20:10:30.781Z&end=2015-07-01T20:11:00.781Z&step=15s'{ "status" : "success", "data" : { "resultType" : "matrix", "result" : [ { "metric" : { "__name__" : "up", "job" : "prometheus", "instance" : "localhost:9090" }, "values" : [ [ 1435781430.781, "1" ], [ 1435781445.781, "1" ], [ 1435781460.781, "1" ] ] } ] }}
五、自建 Grafana 添加数据源
我们可以通过自己部署的 Grafana 添加托管的 Prometheus 为数据源,方便我们在自己的 Grafana 中查看数据,前提是需要保证它们在同一 VPC 内,保证网络是可以互相访问的。
开启 BasicAuth 认证方法,并填写相应的认证信息即可,如下配置。