, 然后提取和运行它,so easy:

    在开始启动Prometheus之前,我们要配置它

    配置Prometheus监控自身

    Prometheus从目标机上通过http方式拉取采样点数据, 它也可以拉取自身服务数据并监控自身的健康状况

    当然Prometheus服务拉取自身服务采样数据,并没有多大的用处,但是它是一个好的DEMO。保存下面的Prometheus配置,并命名为::

    1. global:
    2. scrape_interval: 15s # 默认情况下,每15s拉取一次目标采样点数据。
    3. # 我们可以附加一些指定标签到采样点度量标签列表中, 用于和第三方系统进行通信, 包括:federation, remote storage, Alertmanager
    4. external_labels:
    5. monitor: 'codelab-monitor'
    6. # 下面就是拉取自身服务采样点数据配置
    7. scrape_configs:
    8. # job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上
    9. - job_name: 'prometheus'
    10. # 覆盖global的采样点,拉取时间间隔5s
    11. scrape_interval: 5s
    12. static_configs:
    13. - targets: ['localhost:9090']

    对于一个完整的配置选项,请见

    指定启动Prometheus的配置文件,然后运行

      这样Prometheus服务应该起来了。你可以在浏览器上输入:http://localhost:9090, 就可以看到Prometheus的监控界面

      使用expression browser(暂翻译:浏览器上输入表达式)

      为了使用Prometheus内置浏览器表达式,导航到http://localhost:9090/graph,并选择带有”Graph”的”Console”.

      在拉取到的度量采样点数据中, 有一个metric叫prometheus_target_interval_length_seconds, 两次拉取实际的时间间隔,在表达式的console中输入:

      1. prometheus_target_interval_length_seconds

      这个应该会返回很多不同的倒排时间序列数据,这些度量名称都是prometheus_target_interval_length_seconds, 但是带有不同的标签列表值,这些标签列表值指定了不同的延迟百分比和目标组间隔

      如果我们仅仅对99%的延迟感兴趣,则我们可以使用下面的查询去清洗信息:

      为了统计返回时间序列数据个数,你可以写:

      1. count(prometheus_target_interval_length_seconds)

      有关更多的表达式语言,请见

      见图表表达式,导航到http://localhost:9090/graph, 然后使用”Graph” tab

      1. rate(prometheus_tsdb_head_chunks_created_total[1m])

      启动其他一些采样目标

      Go客户端包括了一个例子,三个服务只见的RPC调用延迟

      首先你必须有Go的开发环境,然后才能跑下面的DEMO, 下载Prometheus的Go客户端,运行三个服务:

      1. git clone https://github.com/prometheus/client_golang.git
      2. cd client_golang/examples/random
      3. go get -d
      4. ## 启动三个服务
      5. ./random -listen-address=:8080
      6. ./random -listen-address=:8081
      7. ./random -listen-address=:8082

      现在你在浏览器输入:http://localhost:8080/metrics, http://localhost:8081/metrics, http://localhost:8082/metrics, 能看到所有采集到的采样点数据

      现在我们将会配置Prometheus,拉取三个目标服务的采样点。我们把这三个目标服务组成一个job, 叫example-radom. 然而,想象成,前两个服务是生产环境服务,后者是测试环境服务。我们可以通过group标签分组,在这个例子中,我们通过group="production"标签和group="test"来区分生产和测试

      进入浏览器,输入rpc_duration_seconds, 验证Prometheus所拉取到的采样点中每个点都有group标签,且这个标签只有两个值production, test

      聚集到的采样点数据配置规则

      上面的例子没有什么问题, 但是当采样点海量时,计算成了瓶颈。查询、聚合成千上万的采样点变得越来越慢。为了提高性能,Prometheus允许你通过配置文件设置规则,对表达式预先记录为全新的持续时间序列。让我们继续看RPCs的延迟速率(rpc_durations_seconds_count), 如果存在很多实例,我们只需要对特定的jobservice进行时间窗口为5分钟的速率计算,我们可以写成这样:

      1. avg(rate(rpc_durations_seconds_count[5m])) by (job, service)

      为了记录这个计算结果,我们命名一个新的度量:job_service:rpc_durations_seconds_count:avg_rate5m, 创建一个记录规则文件,并保存为prometheus.rules.yml:

      1. groups:
      2. - name: example
      3. rules:
      4. - record: job_service:rpc_durations_seconds_count:avg_rate5m
      5. expr: avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
      1. global:
      2. scrape_interval: 15s # By default, scrape targets every 15 seconds.
      3. evaluation_interval: 15s # Evaluate rules every 15 seconds.
      4. # Attach these extra labels to all timeseries collected by this Prometheus instance.
      5. external_labels:
      6. monitor: 'codelab-monitor'
      7. rule_files:
      8. - 'prometheus.rules.yml'
      9. scrape_configs:
      10. - job_name: 'prometheus'
      11. # Override the global default and scrape targets from this job every 5 seconds.
      12. scrape_interval: 5s
      13. static_configs:
      14. - targets: ['localhost:9090']
      15. - job_name: 'example-random'
      16. # Override the global default and scrape targets from this job every 5 seconds.
      17. scrape_interval: 5s
      18. static_configs:
      19. - targets: ['localhost:8080', 'localhost:8081']
      20. labels:
      21. group: 'production'
      22. - targets: ['localhost:8082']
      23. labels:
      24. group: 'test'

      然后重启Prometheus服务,并指定最新的配置文件,查询并验证job_service:rpc_durations_seconds_count:avg_rate5m度量指标