首页 > 其他分享 >Prometheus pushgateway(不发布)

Prometheus pushgateway(不发布)

时间:2022-12-26 12:44:19浏览次数:72  
标签:Prometheus spider job pushgateway 发布 total registry

一. 概述

   前面讲到Exporter章节时,讲到了Prometheus服务器运行作业以从目标中获取指标。而指标的获取均是基于pull,即拉取模式的架构。然而在某些情况下,会出现无法从中获取指标目标场景,原因如下:

    1)由于网络安全性和连接性,使得无法访问目标资源,也就是说Prometheus无法直接去拉取各个目标的监控数据。

    2)  目标资源的生命周期太短,例如 在容器启动、执行、和停止时,运行一个Prometheus作业,发现目标在很短的周期内完成执行,结果不能获取该目标的信息。

    3)目标资源没有可以抓取的端点,例如,批处理作业一般不可能具有可以被抓取的正在运行的HTTP服务。

  上述情况下,我们需要使用pushgateway将时间序列推送到Prometheus服务器。

  pushgateway是一个独立的服务器组件,可在HTTP REST API上接收Prometheus指标,pushgateway视为代理服务。

 

   在官方建议中,只有在某些情况下才使用pushgateway,通常推荐使用exporters的pull模式进行监控指标采集,而不盲目地用pushgateway来代替,原因有如下:

    1)当部署单个pushgateway监控多个实例时,pushgateway即可能会发生单点故障,又可能会出现性能瓶颈。

    2)会丧失Prometheus通地UP监控指标检查实例健康状况的功能,此时Prometheus对应的pull状态的UP指标只是针对pushgateway服务。

    3) pushgateway会对推送给它的所有监控数据进行持久化,即时被监控服务已经下线销毁了,Prometheus还是会获取到过期的历史监控数据。这样,就需要我们手动清理pushgateway上过期的历史数据。

二.安装

  下载页:https://prometheus.io/download/  找到pushgateway的pushgateway-1.4.3.linux-amd64.tar.gz。

   解压,启动命令如下:

nohup ./pushgateway --web.listen-address 0.0.0.0:9091 &

     默认端口是9091,也可以通过上面指定其它端口。默认指标数据在pushgateway中不持久化存储只保存在内存中,可指定 --persistence.file来持久化数据,默认情况下,持久化文件每5分钟写入一次,可以使用

  --persistence.interval= 来重新设置写入文件的时间间隔。

三.关联Prometheus

  找到Prometheus.yml文件,添加job配置,其中honor_labels:true是为了避免采集数据本身job和instance被覆盖。

  - job_name: 'pushgateway'
    honor_labels: true
    static_configs:
      - targets: ['47.107.88.98:9091']

  重启Prometheus,查看http://47.107.xx.xx:9090/targets?search= ,可以看到最新targets列中中已经存在Pushateway主机的UP状态,如下所示:

  

 

四.pushgateway数据管理

  当pushgateway正常运行后,我们可以向它发送监控指标。大多数客户端库都支持向pushgateway发送监控指标,并公开这些监控指标以便抓取。

  注意标签值和作业名称不能包含 "/" 即使转义为%2F也不能使用。 

  推送指标是按组进行管理,确定分组是由任意数量标签组成,其中必需有一个job标签。

  查看 http://47.107.88.98:9091/根目录 ,可以看到各分组(以job分组),如下所示:

 

 

 

五. python客户端推送

    安装包

pip3 install scrapy_prometheus

  指标标签:由job标签和job下面的instance标签组成,其它标签任意

       指标案例 http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance

{job="some_job",instance="some_instance"}

  下面是scrapyrt项目进行指标监听,贴出关键代码: 

  mypushgateway.py文件如下:

  

from prometheus_client import CollectorRegistry, Counter, push_to_gateway,pushadd_to_gateway
from realtime_python_crawler.utils.sys_utils import get_config
from realtime_python_crawler.utils.sys_utils import get_setting

dict=get_config()
obj=dict['pushgateway']
url=obj[0]['url']
enable=int(obj[0]['enable'])
hostip=str(get_setting('HOSTIP'))

#必需是全局的,则否counter不累加
registry = CollectorRegistry()
counter=Counter(name='realtime_python_crawler_req_total', 
                    documentation= 'The total number of requests',
                    labelnames=['instance','spider'], 
                    registry=registry)


registry_failure = CollectorRegistry()
counter_failure=Counter(name='realtime_python_crawler_failures_total', 
                    documentation= 'The failure total number of requests',
                    labelnames=['instance','spider'], 
                    registry=registry_failure)

registry_es = CollectorRegistry()
counter_es=Counter(name='realtime_python_crawler_es_total', 
                    documentation= 'The  total number of elasticsearch',
                    labelnames=['instance','spider'], 
                    registry=registry_es)

req_total_job='req_total_'+hostip.split('.')[-1]
req_failure_job='failures_total_'+hostip.split('.')[-1]
item_total_job='item_total_'+hostip.split('.')[-1]

class MyPushGateWay:

    #请求总数
    def req_total(self,spider):
        if enable==0:
            return
        counter.labels(instance=hostip,spider=spider).inc()
        pushadd_to_gateway(url, job=req_total_job, timeout=5, registry=registry)

    #失败数
    def req_failure_total(self,spider):
        if enable==0:
            return
        counter_failure.labels(instance=hostip,spider=spider).inc()
        pushadd_to_gateway(url, job=req_failure_job,timeout=5, registry=registry_failure)

    # item的总数
    def item_total(self,inc,spider):
        if enable==0:
            return
        if inc>0:
            counter_es.labels(instance=hostip,spider=spider).inc(inc)
            push_to_gateway(url, job=item_total_job,timeout=5, registry=registry_es)

 

  关键信息调用如下所示:

     push=MyPushGateWay()
        push.req_total()

        errors = result.get("errors")
        if errors:
            response["errors"] = errors
            push.req_failure_total()

  es入库指标如下所示:

   def saveobj(self, vendor_quote_platform):
        push=MyPushGateWay()
        push.save_es_total()
        vendor_quote_platform.save()

  最后效果,见上图以job分组

github地址:https://github.com/prometheus/pushgateway

 

标签:Prometheus,spider,job,pushgateway,发布,total,registry
From: https://www.cnblogs.com/MrHSR/p/16733525.html

相关文章

  • Prometheus监控之检查工具Promtool TSDB
    一、概述Promtool在TSDB方面一个有6个子命令,分别用来进行写性能测试、TSDB分析、列出TSDB数据块、dump、从OpenMetric导入数据块、为新的记录规则创建数据块二、TSDB1、写性......
  • kali新版本2022.4发布
    KaliLinux2022.4发布(Azure、Social&KaliNetHunterPro)。在这一年结束之前,我们认为最好是把2022年的最后一个版本发布出来。今天我们发布了KaliLinux2022.4。这可......
  • 【踩坑】Debian编译安装Podman和Prometheus-podman-exporter
    这个月一直在折腾Grafana,中途遇到了各种各样的问题这两天折腾得差不多了,才有空把遇到的问题写出来一方面做个记录,另一方面也当给想要折腾的小伙伴们踩踩坑估计最近......
  • postmarketOS 22.12发布
    postmarketOS是一个基于阿尔卑斯山的移动设备Linux发行版。该项目最新的版本是postmarketOS22.12,其中包括对移动操作系统的三个用户界面的升级。Sxmo、Phosh和Pla......
  • Jeecgboot-Vue3 v1.2.0 版本正式发布,企业级低代码平台
    项目介绍Jeecgboot-Vue3采用Vue3.0、Vite、Ant-Design-Vue、TypeScript等新技术方案,包括二次封装组件、utils、hooks、动态菜单、权限校验、按钮级别权限控制等功能。Je......
  • pg_graphql 1.0 发布了
    pg_graphql是supabse团队使用pgx扩展开发的pggraphql扩展,实际上官方的graphl支持是演变了好几个版本的,学习下官方博客的演变还是很值得的看看如何进行设计参考资料h......
  • 高可用kube-prometheus 5分钟快速搭建
    项目地址​​prometheus-operator/kube-prometheus:UsePrometheustomonitorKubernetesandapplicationsrunningonKubernetes(github.com)​​1.初识prometheus1.......
  • WebService简单教学??SpringBoot整合CXF的快速入门??CXF发布Rest服务
    目录​##springboot整合CXF的快速入门##​​一,服务端提供webservice服务​​​1,实体类User​​​​2,webservice接口​​​​3,webservice接口的实现类​​​​4,CXF配置类​​......
  • 新闻发布及管理系统的设计与实现(论文+PPT+源码)
     本科毕业设计(论文)题目:新闻发布及管理系统的设计与实现专题题目:本科毕业设计(论文)任务书题目:新闻发布及管理系统的设计与实现专题题目(若无专题则不填):原始......
  • 文章发布系统的设计与实现(论文+PPT+源码)
    摘要随着计算机技术的迅速发展,网络正以一种前所未有的冲击力影响着人类的生产和生活。网络的快速发展,颠覆了传统的信息传播方式,冲破了传统的时间,空间的局限性,继而引发了人......