Pushgateway(2)自定义数据推送到pushgateway及推送数据的注意事项

简单定义几条数据,并将其推送至pushgateway,方便我们验证自定义模板功能。

数据推送默认格式

们要 Push 数据到 PushGateway 中,可以通过其提供的 API 标准接口来添加,
默认 URL 地址为:
http://<ip>:9091/metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>}

  1. 是必填项,为 job 标签值,后边可以跟任意数量的标签对,
  2. 一般我们会添加一个 instance/<INSTANCE_NAME>实例名称标签,来方便区分各个指标。

一:简单数据的推送(命令行数据)

1.1 测试

测试:

[root@localhost ~]# echo "test_metric 123456" | curl --data-binary @- http://192.168.156.134:9091/metrics/job/test_job
[root@localhost ~]# 

1.2 查看测试结果

刷新Pushgateway界面,查看界面
在这里插入图片描述
上图中,除了 test_metric 外,同时还新增了 push_time_secondspush_failure_time_seconds 两个指标,这两个是 PushGateway 系统自动生成的相关指标

同时,在 Prometheus UI 页面上 Graph 页面可以查询的到该指标了。
在这里插入图片描述
test_metric 我们查询出来的结果为

test_metric{exported_job="test_job",instance="pushgateway",job="pushgateway"}

可以发现,交的指标所属 job 名称为 test_job ,显示的为 exported_job=“test_job” ,而 job 显示为 job=“pushgateway”
为了避免这种情况的发送,需要在Prometheus的配置文件中增加增加 honor_labels: true 参数配置

1.3 修改配置使我们得到想要的显示(honor_labels )

进入Prometheus的安装目录,修改其配置文件如下
其实就是增加一段KV就可以了

  - job_name: pushgateway
    honor_labels: true
    static_configs:
      - targets: ['192.168.156.134:9091']
        labels:
          instance: pushgateway

honor_labels 的作用:

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

实际操作

[root@localhost prometheus-2.6.1.linux-amd64]# vim prometheus.yml 
       - 192.168.156.134:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
  - "/opt/prometheus/prometheus-2.6.1.linux-amd64/rules/*.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: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'agent1'
    static_configs:
    - targets: ['192.168.156.134:9100']
  - job_name: pushgateway
    honor_labels: true
    static_configs:
      - targets: ['192.168.156.134:9091']
        labels:
          instance: pushgateway
[root@localhost prometheus-2.6.1.linux-amd64]# 

1.4 重启Prometheus

[root@localhost prometheus-2.6.1.linux-amd64]# pkill prometheus
[root@localhost prometheus-2.6.1.linux-amd64]# lsof -i:9090
[root@localhost prometheus-2.6.1.linux-amd64]# ./prometheus --config.file=prometheus.yml & 
[root@localhost prometheus-2.6.1.linux-amd64]# 

1.5 查看UI页面

在这里插入图片描述

2 较为复杂数据的推送(命令行数据)

2.1 执行命令

Push 一个复杂一些的,一次写入多个指标,而且每个指标添加 TYPE 及 HELP 说明。
推送命令:

cat <<EOF | curl --data-binary @- http://192.168.156.134:9091/metrics/job/test_job/instance/test_instance
# TYPE test_metrics counter
test_metrics{label="app1",name="demo"} 100.00
# TYPE another_test_metrics gauge
# HELP another_test_metrics Just an example.
another_test_metrics 123.45
EOF

指令说明:

/metrics/job/test_job 和 metrics/job/test_job/instance/test_instance ,它们都属于 test_job,但是它们属于两个指标值,因为 instance 对二者做了区分;具体的可查看数据推送的格式(本博客最上方的小节)

实际操作:

[root@localhost ~]# cat <<EOF | curl --data-binary @- http://192.168.156.134:9091/metrics/job/test_job/instance/test_instance
> # TYPE test_metrics counter
> test_metrics{label="app1",name="demo"} 100.00
> # TYPE another_test_metrics gauge
> # HELP another_test_metrics Just an example.
> another_test_metrics 123.45
> EOF
[root@localhost ~]# 


2.2 查看Pushgateway界面

在这里插入图片描述

2.3 进入Prometheus的界面查看

在这里插入图片描述

3 大量数据提交(通过写入文件将文件提交)

之前的两种方法,Push 指标数据是通过命令行追加方式,少量数据还凑合,如果需要 Push 的数据比较大时,就不太方便了,这里我们也可以通过将指标数据写入到文件,然后将文件内容提交

第一步:编写指标数据文件pgdata.txt

pgdata.txt文件内容:

# TYPE http_request_total counter
# HELP http_request_total get interface request count with different code.
http_request_total{code="200",interface="/v1/save"} 276
http_request_total{code="404",interface="/v1/delete"} 0
http_request_total{code="500",interface="/v1/save"} 1
# TYPE http_request_time gauge
# HELP http_request_time get core interface http request time.
http_request_time{code="200",interface="/v1/core"} 0.122

实际操作:

[root@localhost ~]# cd data/
[root@localhost data]# ls
com.txt  test.txt
[root@localhost data]# vim pgdata.txt
# TYPE http_request_total counter
# HELP http_request_total get interface request count with different code.
http_request_total{code="200",interface="/v1/save"} 276
http_request_total{code="404",interface="/v1/delete"} 0
http_request_total{code="500",interface="/v1/save"} 1
# TYPE http_request_time gauge
# HELP http_request_time get core interface http request time.
http_request_time{code="200",interface="/v1/core"} 0.122
~
~
"pgdata.txt" [] 8L, 426C 已写入                                                      
[root@localhost data]# 

第二步:将文件数据push上去

命令:

curl -XPOST --data-binary @pgdata.txt http://192.168.156.134:9091/metrics/job/app/instance/app-172.30.0.0

实际操作:

[root@localhost data]# curl -XPOST --data-binary @pgdata.txt http://192.168.156.134:9091/metrics/job/app/instance/app-172.30.0.0
[root@localhost data]# 

第三步:在PushGateway UI 页面查看

在这里插入图片描述

4 删除某一个指标

4.1 ui界面删除

可以通过ui界面进行删除,如下:
在这里插入图片描述
删除后的效果:
在这里插入图片描述

4.2 通过命令删除

如删除 job=“test_job” 组下的所有指标值,可以执行如下命令:

curl -X DELETE http://192.168.156.134:9091/metrics/job/test_job

实际操作:

[root@localhost data]# curl -X DELETE http://192.168.156.134:9091/metrics/job/test_job
[root@localhost data]# 

最后的页面效果:
在这里插入图片描述

4.3 删除命令的注意事项

删除 job="test_job" 组下的所有指标值,不包括 {job="test_job", instance="test_instance"} 中的指标值,虽然它们的 job 名称都为 test_job

如果想删除该指标值,那么需要执行如下命令:

curl -X DELETE http://192.168.156.134:9091/metrics/job/test_job/instance/test_instance

实际操作:

[root@localhost data]# curl -X DELETE http://192.168.156.134:9091/metrics/job/test_job/instance/test_instance
[root@localhost data]# 

页面效果:
在这里插入图片描述

5 PushGateway使用的注意事项

1:指标值只能是数字类型,非数字类型报错
测试:

[root@localhost data]# echo "test_metric 12.34.56ff" | curl --data-binary @- http://192.168.156.134:9091/metrics/job/test_job_1
text format parsing error in line 1: expected float as value, got "12.34.56ff"
[root@localhost data]# 

2:指标值支持最大长度为 16 位,超过16 位后默认置为 0

[root@localhost data]# echo "test_metric 1234567898765432123456789" | curl --data-binary @- http://192.168.156.134:9091/metrics/job/test_job_2
[root@localhost data]# 

实际上:实际获取值 test_metric{job=“test_job_2”} 1234567898765432200000000

在这里插入图片描述

3:PushGateway 数据持久化操作: 默认 PushGateway 不做数据持久化操作,当 PushGateway 重启或者异常挂掉,导致数据的丢失,可以通过启动时添加 -persistence.file 和 -persistence.interval 参数来持久化数据

  1. -persistence.file 表示本地持久化的文件,将 Push 的指标数据持久化保存到指定文件
  2. -persistence.interval 表示本地持久化的指标数据保留时间,若设置为 5m,则表示 5 分钟后将删除存储的指标数据
docker run -d -p 9091:9091 prom/pushgateway "-persistence.file=pg_file –persistence.interval=5m"

4:PushGateway 推送Prometheus 拉取时间:设置 Prometheus 每次从 PushGateway 拉取的数据,并不是拉取周期内用户推送上来的所有数据,而是最后一次 Push 到 PushGateway 上的数据所以推荐设置推送时间小于或等于 Prometheus 拉取的时间,这样保证每次拉取的数据是最新 Push 上来的

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
一、prometheus简介 Prometheus是一个开源的系统监控和告警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF维护管理的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控,prometheus支持多种exporter采集数据,还支持通过pushgateway进行数据上报,Prometheus再性能上可支撑上万台规模的集群。 二、prometheus架构图 三、prometheus组件介绍 1.Prometheus Server: 用于收集和存储时间序列数据。 2.Client Library: 客户端库,检测应用程序代码,当Prometheus抓取实例的HTTP端点时,客户端库会将所有跟踪的metrics指标的当前状态发送到prometheus server端。 3.Exporters: prometheus支持多种exporter,通过exporter可以采集metrics数据,然后发送到prometheus server端 4.Alertmanager: 从 Prometheus server 端接收到 alerts 后,会进行去重,分组,并路由到相应的接收方,发出报警,常见的接收方式有:电子邮件,微信,钉钉, slack等。 5.Grafana:监控仪表盘 6.pushgateway: 各个目标主机可上报数据pushgatewy,然后prometheus server统一从pushgateway拉取数据。 四、课程亮点 五、效果图展示 六、讲师简介 先超(lucky):高级运维工程师、资深DevOps工程师,在互联网上市公司拥有多年一线运维经验,主导过亿级pv项目的架构设计和运维工作 主要研究方向: 1.云计算方向:容器 (kubernetes、docker),虚拟化(kvm、Vmware vSphere),微服务(istio),PaaS(openshift),IaaS(openstack)等2.系统/运维方向:linux系统下的常用组件(nginx,tomcat,elasticsearch,zookeeper,kafka等),DevOps(Jenkins+gitlab+sonarqube+nexus+k8s),CI/CD,监控(zabbix、prometheus、falcon)等 七、课程大纲
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

?abc!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值