【问题标题】:How to push a gauge with multiple metrics to Prometheus? Problem with gateway如何将具有多个指标的仪表推送到 Prometheus?网关问题
【发布时间】:2019-09-27 12:26:23
【问题描述】:

通过 Pushgateway 公开具有多个指标的 prometheus 仪表会引发“仪表”对象没有属性“_value”的错误。 一旦注释了抛出错误的行, push_to_gateway() 就会抛出

urlopen error [WinError 10061] No connection could be made because the target machine actively refused it

我在尝试输入 http://localhost:9091/ 时没有收到任何响应。


我尝试通过 HTTP 客户端通过标准方法公开指标。但是,python 代码只会按需运行,并带有参数。它将运行几秒钟并退出。我决定尝试一个 Pushgateway。我尝试按照https://github.com/prometheus/client_python#exporting-to-a-pushgateway 中的指南进行操作。我在本地安装 prometheus 并在编辑 yaml 文件后运行客户端,方法是将 scrape config 替换为:

scrape_configs:
 - job_name: pushgateway
   honor_labels: true
   static_configs:
    - targets:
      - localhost:9091

实际代码:

# ( Class attribute )
iv_registry = CollectorRegistry()
# Gauge to be passed to the Prometheus     
iv_gauge = Gauge(ic_gauge_name,
                     ic_gauge_docu_labels, ic_labels_list, registry=iv_registry)


    def __create_gauge(self):
        """Fill gauge to be passed to the prometheus and graffana"""
        try:               
            # Set labels and assign a metric
            self.iv_gauge.labels( label1 = "AAA", label2 =  "BBB", label3 = "CCC"  ).set(4)                          
            self.iv_gauge.labels( label1 = "AAA", label2 =  "BBB", label3 = "DDD"  ).set(0)                          

            # expose in a batch mode
            self.iv_gauge.set_to_current_time() # Does it have to be here? My gauge itself does not have any _value, on matrics with labels store _values
            push_to_gateway('localhost:9091', job='batchA', registry=self.iv_registry)

我希望看到一个具有不同指标的仪表被推送到 Pushgateway,由 Prometheus 抓取并用于本地测试,以便能够在本地主机上的某个位置显示它,就像通过 HTTP 服务器暴露使用 http://localhost:8000/ 完成的一样。请在下面找到示例。

TYPE gauge_name gauge
gauge_name{label1="AAA",label2="BBB",label3="CCC"} 4.0
gauge_name{label1="AAA",label2="BBB",label3="DDD"} 0.0

【问题讨论】:

  • 如果我的“解决方案”有误,请纠正我。我必须在 prometheus 客户端中安装 pushgateway 并修改废弃配置。资料来源:prometheus.io/download/#pushgateway 一旦我这样做了,我的指标就会暴露在 localhost:9091>。你能告诉我这是否是我应该做的一切吗?或者是否需要为 prometheus 设置其他任何东西才能废弃这些指标?
  • 我可以在 localhost:9090> 看到结果。结果有额外的“工作”标签。请问这个标签是否必要?此设置是否允许我在每次执行生成值的 py 脚本时存储结果?
  • 有关设置的其他文档:blog.ruanbekker.com/blog/2019/05/17/… 将很快更新并关闭该主题。

标签: python push prometheus metrics gauge


【解决方案1】:

将以下内容粘贴到 Python 解释器中:

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
push_to_gateway('localhost:9091', job='batchA', registry=registry)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多