PushGateway推送组件


2021年6月17日 11:44     admin

下载主页

  1. https://prometheus.io/download/

安装PushGateway

  1. wget https://github.com/prometheus/pushgateway/releases/download/v1.4.1/pushgateway-1.4.1.linux-amd64.tar.gz
  2. tar -zxvf pushgateway-1.4.1.linux-amd64.tar.gz
  3. mv pushgateway-1.4.1.linux-amd64 /usr/local/pushgateway
  4. cd /usr/local/pushgateway
  5. ./pushgateway --version

启动PushGateway

  1. cd /usr/local/pushgateway
  2. ./pushgateway #前台启动
  3. nohup ./pushgateway & #后台启动
  4. -----------------------------------------------------------
  5. --web.listen-address=":9091"
  6. #pushgateway监听的端口,默认是9091,若需要修改则通过此参数。
  7. --web.telemetry-path="/metrics"
  8. #获取metric信息的url,默认是/metrics,若需要修改则通过此参数
  9. --log.level="info"
  10. #设置日志级别
  11. --log.format="logger:stderr"
  12. #设置打印日志的格式,若有自动化日志提取工具可以使用这个参数规范日志打印的格式

查看

  1. http://【PushGateway服务器IP】:9091/


在prometheus中添加pushgateway

修改prometheus的配置文件

  1. vim prometheus.yml
  2. #内容
  3. - job_name: 'push-metrics'
  4. static_configs:
  5. - targets: ['192.168.110.146:9091']
  6. honor_labels: true

注意:因为prometheus配置pushgateway 的时候,也会指定job和instance,但是它只表示pushgateway实例,不能真正表达收集数据的含义。所以配置pushgateway需要添加honor_labels:true,避免收集数据本身的job和instance被覆盖


添加数据

格式:http://IP:PORT/metrics/job/[jobname]/instance/[instancename]

  1. #单条数据
  2. echo "some_metric 3.14" | curl --data-binary @- http://192.168.110.146:9091/metrics/job/some_job
  3. -----------------------------------------------------------
  4. #多条数据
  5. cat <<EOF | curl --data-binary @- http://192.168.110.146:9091/metrics/job/some_job/instance/some_instance
  6. # TYPE some_metric counter
  7. some_metric{label="val1"} 42
  8. # TYPE another_metric gauge
  9. # HELP another_metric Just an example.
  10. another_metric 2398.283
  11. EOF
  12. -----------------------------------------------------------
  13. #文件方式导入
  14. vim data.txt
  15. #内容:
  16. http_request_total{code="200",domain="abc.cn"} 276683
  17. http_request_total{code="400",domain="abc.cn"} 0
  18. http_request_total{code="408",domain="abc.cn"} 7
  19. #发送:
  20. curl -X POST --data-binary @data.txt http://192.168.110.146:9091/metrics/job/some_job/instance/some_instance

删除数据

  1. curl -X DELETE http://192.168.110.146:9091/metrics/job/some_job