无法从 Prometheus HTTP Server 获取查询或 JSON 密钥

Posted

技术标签:

【中文标题】无法从 Prometheus HTTP Server 获取查询或 JSON 密钥【英文标题】:Unable to get query or JSON keys from Prometheus HTTP Server 【发布时间】:2021-10-03 14:49:04 【问题描述】:

我想从 Prometheus HTTP 服务器获取某个指标:zcash_difficulty_gauge,但我遇到了以下错误:i) JSON 不可用 ii) 请求 URL 不返回指标,而是返回整个指标页面。

我的代码:

query = "http://localhost:8094/api/v1/query\?query\=zcash_difficulty_gauge"
response = requests.get(query)

然后我遇到以下命令的 JSON 错误:

In [39]: response.json()
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-39-34c975fa6377> in <module>
----> 1 response.json()

~/workspace/source/graphstore/k8s/gcp/neo4j/venv/lib/python3.8/site-packages/requests/models.py in json(self, **kwargs)
    908                     # used.
    909                     pass
--> 910         return complexjson.loads(self.text, **kwargs)
    911
    912     @property

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/decoder.py in decode(self, s, _w)
    335
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

即使我尝试curl 命令:

curl http://localhost:8094/api/v1/query\?query\=zcash_difficulty_gauge

作为回报,我得到了整个页面,而不仅仅是我想要的指标。

网址:localhost:8094是整个页面,如下:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_totalgeneration="0" 314.0
python_gc_objects_collected_totalgeneration="1" 58.0
python_gc_objects_collected_totalgeneration="2" 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_totalgeneration="0" 0.0
python_gc_objects_uncollectable_totalgeneration="1" 0.0
python_gc_objects_uncollectable_totalgeneration="2" 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_totalgeneration="0" 36.0
python_gc_collections_totalgeneration="1" 3.0
python_gc_collections_totalgeneration="2" 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_infoimplementation="CPython",major="3",minor="8",patchlevel="2",version="3.8.2" 1.0
# HELP zcash_difficulty_gauge zcash_difficulty_gauge gauge
# TYPE zcash_difficulty_gauge gauge
zcash_difficulty_gauge 1237.0

我的服务器脚本是:

from typing import Counter
from prometheus_client import start_http_server, Gauge
from time import sleep


start_http_server(8094)

ZCASH_DIFFICULTY_GAUGE = Gauge('zcash_difficulty_gauge', 'zcash_difficulty_gauge gauge')
counter = 0
while True:
    sleep(10)
    ZCASH_DIFFICULTY_GAUGE.set(1004 + counter)
    counter += 1

我已尝试阅读这些文档,但似乎我在这些文档的基础上做了正确的事情。我也试图理解another similar post 所说的内容,但解决方案似乎是我现在正在做的事情。

如何使用 Python 获取特定指标?

【问题讨论】:

【参考方案1】:

这里似乎有些关于端点的混淆。

http://localhost:8094 不是普罗米修斯——那是你的脚本启动的 HTTP 服务器(在start_http_server(8094) 行中)。它没有“API”;它只是发布一个指标列表。

Prometheus 将使用抓取作业收集这些指标。

Prometheus 通常侦听端口 9090,尽管可以更改。如果您配置了适当的抓取作业,并且 prometheus 暴露在 localhost:9090,则以下 curl 命令将查询 prometheus 以获取您的目标指标:

curl 'http://localhost:9090/api/v1/query?query=zcash_difficulty_gauge'

【讨论】:

【参考方案2】:

有一个text formatter provided by Prometheus client 可以解决这个问题。它加载所有文本指标,您可以从中创建字典、json。

【讨论】:

以上是关于无法从 Prometheus HTTP Server 获取查询或 JSON 密钥的主要内容,如果未能解决你的问题,请参考以下文章

无法从 prometheus-adapter 检索自定义指标

Prometheus无法获取Windows的CPU数据解决方法

Prometheus 无法从自定义 Rest Endpoint 读取指标

win11安装wondow+serve2008显示无法更新安装文件?

网站无法响应HTTP/1.1

升级到 Spring Boot 版本 2 后 Prometheus 无法正常工作