zoukankan      html  css  js  c++  java
  • 【转】Prometheus查询表达式

    原文:https://www.jianshu.com/p/d187ac561eb8

    ----------------------

    Prometheus Querying

    查询

    prometheus提供了功能性表达式语言,可让用户对于时间序列的数据进行选择和聚合。通过表达式查询的结果可以绘制为曲线图,也可以在prometheus提供的表达式浏览器中显示为表格,也可以通过外部系统以HTTP API来调用使用。

    expression language data types

    prometheus 表达式语言中,有四种类型:

    • 即时向量(instant vector) 包含每个时间序列的单个样本的一组时间序列,共享相同的时间戳。
    • 范围向量(Range vector) 包含每个时间序列随时间变化的数据点的一组时间序列。
    • 标量(Scalar) 一个简单的数字浮点值
    • 字符串(String) 一个简单的字符串值(目前未被使用)

    根据使用情况(例如绘图或者显示表达式的输出),这些类型中只有一些是由用户指定的表达式产生的结果而有效的,例如,即时向量表达式是可以绘图的唯一类型。

    时间序列选择器

    • 即时向量选择器

      即时向量选择器允许选择一组时间序列,或者某个给定的时间戳的样本数据。下面这个例子选择了具有http_requests_total的时间序列:

        http_requests_total
      

      你可以通过附加一组标签,并用{}括起来,来进一步筛选这些时间序列。下面这个例子只选择有http_requests_total名称的、有prometheus工作标签的、有canary组标签的时间序列:

        http_requests_total{job="prometheus",group="canary"}
      

      另外,也可以也可以将标签值反向匹配,或者对正则表达式匹配标签值。下面列举匹配操作符:

        =:选择正好相等的字符串标签
        !=:选择不相等的字符串标签
        =~:选择匹配正则表达式的标签(或子标签)
        !=:选择不匹配正则表达式的标签(或子标签)
      

      例如,选择staging、testing、development环境下的,GET之外的HTTP方法的http_requests_total的时间序列:

        http_requests_total{environment=~"staging|testing|development",method!="GET"}
      
    • 范围向量选择器

      范围向量表达式正如即时向量表达式一样运行,前者返回从当前时刻的时间序列回来。语法是,在一个向量表达式之后添加[]来表示时间范围,持续时间用数字表示,后接下面单元之一:

      • s:seconds
      • m:minutes
      • h:hours
      • d:days
      • w:weeks
      • y:years

      在下面这个例子中,我们选择此刻开始5分钟内的所有记录,metric名称为http_requests_total、作业标签为prometheus的时间序列的所有值:

        http_requests_total{job="prometheus"}[5m]
      
    • 偏移修饰符(offset modifier)
      偏移修饰符允许更改查询中单个即时向量和范围向量的时间偏移量,例如,以下表达式返回相对于当前查询时间5分钟前的http_requests_total值:

        http_requests_total offset 5m
      

      Note:请注意,偏移量修饰符始终需要跟随选择器,即以下是正确的:

        sum(http_requests_total{method="GET"} offset 5m) // GOOD.
      

      下面是错误的:

        sum(http_requests_total{method="GET"}) offset 5m // INVALID.
      

      如下是范围向量的相同样本。这返回http_requests_total在一周前5分钟内的速率:

        rate(http_requests_total[5m] offset 1w)
      
    • 操作符

      Prometheus支持多种二元和聚合的操作符请查看这里

    • 函数
      Prometheus支持多种函数,来对数据进行操作请查看这里

    怎么使用prometheus监控容器?

    prometheus监控不同的目标服务需要实现不同的exporter插件,早期的时候,官方出了container-exporter项目,但是现在项目已经停止。推荐使用谷歌的cAdvisor项目作为prometheus的exporter。cAdvisor作为一个监控单机容器的项目,数据较为全面,但是也有很大的问题,例如io等数据没有等等。结合prometheus后就能在整个集群监控查询容器。举个例子,你有一个项目有3个容器分布在三台机器,你怎么监控整个项目的流量,内存量,负载量的实时数据。这就是prometheus的多维度查询解决的问题,数据从3台机器的cadvisor得到每个容器的数据,它的多维度查询语法就能让你得到你想要的数据。

    这里假设你有10台机器部署了容器需要监控,你在10台机器上分别部署cAdvisor容器

    sudo docker run 
      --volume=/:/rootfs:ro 
      --volume=/var/run:/var/run:rw 
      --volume=/sys:/sys:ro 
      --volume=/var/lib/docker/:/var/lib/docker:ro 
      --publish=8080:8080 
      --detach=true 
      --name=cadvisor 
      google/cadvisor:latest
    

    找一台机器部署prometheus服务,这里依然使用容器部署:

    docker run 
        -p 9090:9090 
        --log-driver none 
        -v /hdd1/prometheus/etc/:/etc/prometheus/ 
        -v /hdd1/prometheus/data/:/prometheus/ 
        -v /etc/localtime:/etc/localtime 
        --name prometheus 
        prom/prometheus
    

    创建/hdd1/prometheus/etc/prometheus.yml配置文件

        my global config
        global:
          scrape_interval:     15s # By default, scrape targets every 15 seconds.
          evaluation_interval: 15s # By default, scrape targets every 15 seconds.
          # scrape_timeout is set to the global default (10s).
          # Attach these labels to any time series or alerts when communicating with
          # external systems (federation, remote storage, Alertmanager).
          external_labels:
              monitor: 'container-monitor'
        # Load and evaluate rules in this file every 'evaluation_interval' seconds.
        rule_files:
           - "/etc/prometheus/rules/common.rules"
        # 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: 'container'
            static_configs:
            - targets: ['10.12.1.129:8080','10.12.1.130:8080','10.50.1.92:8080','10.50.1.93:8080','10.50.1.119:8080']
    

    配置文件中 -targets中的端点填写你的实际cadvisor所在的ip和暴露的端口.正确启动后访问ip:9090就能查询数据了哦



    作者:YichenWong
    链接:https://www.jianshu.com/p/d187ac561eb8
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    redux dispatch、action、reduce 执行流程
    react中使用react-redux
    npm 全局安装默认地址
    react 组件外js文件路由跳转
    withRouter的作用和适用场景
    react 自定义高阶组件,实现路由拦截,子路由渲染
    移动端原生js使用touch事件监听滑动方向
    Vue.js中this.$nextTick()的使用与理解
    域名等级划分介绍
    nodejs 实现一个账号只能一台设备登录
  • 原文地址:https://www.cnblogs.com/oxspirt/p/15018552.html
Copyright © 2011-2022 走看看