prometheus-book
  • Introduction
  • 全书组织
  • Part I - Prometheus基础
    • 第1章 天降奇兵
      • Prometheus简介
      • 初识Prometheus
        • 安装Prometheus Server
        • 使用Node Exporter采集主机数据
        • 使用PromQL查询监控数据
        • 监控数据可视化
      • 任务和实例
      • Prometheus核心组件
      • 小结
    • 第2章 探索PromQL
      • 理解时间序列
      • Metrics类型
      • 初识PromQL
      • PromQL操作符
      • PromQL聚合操作
      • PromQL内置函数
      • 在HTTP API中使用PromQL
      • 最佳实践:4个黄金指标和USE方法
      • 小结
    • 第3章 Prometheus告警处理
      • Prometheus告警简介
      • 自定义Prometheus告警规则
      • 部署AlertManager
      • Alertmanager配置概述
      • 基于标签的告警处理路由
      • 使用Receiver接收告警信息
        • 集成邮件系统
        • 集成Slack
        • 集成企业微信
        • 集成钉钉:基于Webhook的扩展
      • 告警模板详解
      • 屏蔽告警通知
      • 使用Recoding Rules优化性能
      • 小结
  • Part II - Prometheus进阶
    • 第4章 Exporter详解
      • Exporter是什么
      • 常用Exporter
        • 容器监控:cAdvisor
        • 监控MySQL运行状态:MySQLD Exporter
        • 网络探测:Blackbox Exporter
      • 使用Java自定义Exporter
        • 使用Client Java构建Exporter程序
        • 在应用中内置Prometheus支持
      • 小结
    • 第5章 数据与可视化
      • 使用Console Template
      • Grafana的基本概念
      • Grafana与数据可视化
        • 变化趋势:Graph面板
        • 分布统计:Heatmap面板
        • 当前状态:SingleStat面板
      • 模板化Dashboard
      • 小结
    • 第6章 集群与高可用
      • 本地存储
      • 远程存储
      • 联邦集群
      • Prometheus高可用
      • Alertmanager高可用
      • 小结
    • 第7章 Prometheus服务发现
      • Prometheus与服务发现
      • 基于文件的服务发现
      • 基于Consul的服务发现
      • 服务发现与Relabel
      • 小结
  • Part III - Prometheus实战
    • 第8章 监控Kubernetes
      • 初识Kubernetes
      • 部署Prometheus
      • Kubernetes下的服务发现
      • 监控Kubernetes集群
      • 基于Prometheus的弹性伸缩
      • 小结
    • 第9章 Prometheus Operator
      • 什么是Prometheus Operator
      • 使用Operator管理Prometheus
      • 使用Operator管理监控配置
      • 在Prometheus Operator中使用自定义配置
      • 小结
    • 参考资料
Powered by GitBook
On this page
  • API响应格式
  • 在HTTP API中使用PromQL
  • 瞬时数据查询
  • 响应数据类型
  • 区间数据查询
  1. Part I - Prometheus基础
  2. 第2章 探索PromQL

在HTTP API中使用PromQL

Prometheus当前稳定的HTTP API可以通过/api/v1访问。

API响应格式

Prometheus API使用了JSON格式的响应内容。 当API调用成功后将会返回2xx的HTTP状态码。

反之,当API调用失败时可能返回以下几种不同的HTTP状态码:

  • 404 Bad Request:当参数错误或者缺失时。

  • 422 Unprocessable Entity 当表达式无法执行时。

  • 503 Service Unavailiable 当请求超时或者被中断时。

所有的API请求均使用以下的JSON格式:

{
  "status": "success" | "error",
  "data": <data>,

  // Only set if status is "error". The data field may still hold
  // additional data.
  "errorType": "<string>",
  "error": "<string>"
}

在HTTP API中使用PromQL

通过HTTP API我们可以分别通过/api/v1/query和/api/v1/query_range查询PromQL表达式当前或者一定时间范围内的计算结果。

瞬时数据查询

通过使用QUERY API我们可以查询PromQL在特定时间点下的计算结果。

GET /api/v1/query

URL请求参数:

  • query=:PromQL表达式。

  • time=:用于指定用于计算PromQL的时间戳。可选参数,默认情况下使用当前系统时间。

  • timeout=:超时设置。可选参数,默认情况下使用-query,timeout的全局设置。

例如使用以下表达式查询表达式up在时间点2015-07-01T20:10:51.781Z的计算结果:

$ curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
{
   "status" : "success",
   "data" : {
      "resultType" : "vector",
      "result" : [
         {
            "metric" : {
               "__name__" : "up",
               "job" : "prometheus",
               "instance" : "localhost:9090"
            },
            "value": [ 1435781451.781, "1" ]
         },
         {
            "metric" : {
               "__name__" : "up",
               "job" : "node",
               "instance" : "localhost:9100"
            },
            "value" : [ 1435781451.781, "0" ]
         }
      ]
   }
}

响应数据类型

当API调用成功后,Prometheus会返回JSON格式的响应内容,格式如上小节所示。并且在data节点中返回查询结果。data节点格式如下:

{
  "resultType": "matrix" | "vector" | "scalar" | "string",
  "result": <value>
}

PromQL表达式可能返回多种数据类型,在响应内容中使用resultType表示当前返回的数据类型,包括:

  • 瞬时向量:vector

当返回数据类型resultType为vector时,result响应格式如下:

[
  {
    "metric": { "<label_name>": "<label_value>", ... },
    "value": [ <unix_time>, "<sample_value>" ]
  },
  ...
]

其中metrics表示当前时间序列的特征维度,value只包含一个唯一的样本。

  • 区间向量:matrix

当返回数据类型resultType为matrix时,result响应格式如下:

[
  {
    "metric": { "<label_name>": "<label_value>", ... },
    "values": [ [ <unix_time>, "<sample_value>" ], ... ]
  },
  ...
]

其中metrics表示当前时间序列的特征维度,values包含当前事件序列的一组样本。

  • 标量:scalar

当返回数据类型resultType为scalar时,result响应格式如下:

[ <unix_time>, "<scalar_value>" ]

由于标量不存在时间序列一说,因此result表示为当前系统时间一个标量的值。

  • 字符串:string

当返回数据类型resultType为string时,result响应格式如下:

[ <unix_time>, "<string_value>" ]

字符串类型的响应内容格式和标量相同。

区间数据查询

使用QUERY_RANGE API我们则可以直接查询PromQL表达式在一段时间返回内的计算结果。

GET /api/v1/query_range

URL请求参数:

  • query=: PromQL表达式。

  • start=: 起始时间。

  • end=: 结束时间。

  • step=: 查询步长。

  • timeout=: 超时设置。可选参数,默认情况下使用-query,timeout的全局设置。

当使用QUERY_RANGE API查询PromQL表达式时,返回结果一定是一个区间向量:

{
  "resultType": "matrix",
  "result": <value>
}

需要注意的是,在QUERY_RANGE API中PromQL只能使用瞬时向量选择器类型的表达式。

例如使用以下表达式查询表达式up在30秒范围内以15秒为间隔计算PromQL表达式的结果。

$ curl 'http://localhost:9090/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" ]
            ]
         },
         {
            "metric" : {
               "__name__" : "up",
               "job" : "node",
               "instance" : "localhost:9091"
            },
            "values" : [
               [ 1435781430.781, "0" ],
               [ 1435781445.781, "0" ],
               [ 1435781460.781, "1" ]
            ]
         }
      ]
   }
}
PreviousPromQL内置函数Next最佳实践:4个黄金指标和USE方法

Last updated 7 years ago