Python Prometheus计数器内部异步调用
我正在使用普罗米修斯直方图、计数器和openAPI。我所看到的不知何故使计数器工作:创建一个具有确切状态的记录,并对其进行计数。当我得到200时,一切正常,但当我得到任何错误代码时,我不会把任何东西输入状态计数器。我认为这是由状态提升引起的,但即使在记录之后提升,我也无法做到。我做错了什么?我听说我可以用钩子做,但我没有任何经验。如果你知道,请告诉我!非常感谢:) 尝试使用if并稍后调用raise_以获取_状态:Python Prometheus计数器内部异步调用,python,asynchronous,python-asyncio,counter,prometheus,Python,Asynchronous,Python Asyncio,Counter,Prometheus,我正在使用普罗米修斯直方图、计数器和openAPI。我所看到的不知何故使计数器工作:创建一个具有确切状态的记录,并对其进行计数。当我得到200时,一切正常,但当我得到任何错误代码时,我不会把任何东西输入状态计数器。我认为这是由状态提升引起的,但即使在记录之后提升,我也无法做到。我做错了什么?我听说我可以用钩子做,但我没有任何经验。如果你知道,请告诉我!非常感谢:) 尝试使用if并稍后调用raise_以获取_状态: from prometheus_client import Histogram,
from prometheus_client import Histogram, Counter
with HIST.labels(model_id, version_id).time():
async with client_session.post(
settings.url,
json=data,
allow_redirects=False,
) as response:
print(response.status)
STATUS_COUNTER.labels(response.status).inc(1)
if not response.status == 200:
response.raise_for_status()
return await response.json()
原始状态:
from prometheus_client import Histogram, Counter
with HIST.labels(model_id, version_id).time():
async with client_session.post(
settings.URL,
json=data,
allow_redirects=False,
raise_for_status=True,
) as response:
STATUS_COUNTER.labels(response.status).inc(1)
return await response.json()
我不熟悉Python异步,但这是一个有趣的问题 我想你可能错过了一个
试试看。。。在async client_会话中,预期会出现…
。post(…)…
,以便捕获get
的故障
以下是我的作品:
导入异步IO
进口aiohttp
导入json
导入时间
从prometheus_客户端导入启动_http_服务器,计数器
c=计数器(“请求总数”、“请求总数”[“代码”])
异步def get_url(客户端,url):
尝试:
与client.get(url)异步作为响应:
c、 标签(代码=响应.status).inc(1)
resp=wait response.read()
返回响应
#你想要比例外更具体的东西
#你想处理它吗
例外情况除外,如e:
打印(“例外:{e}”。格式(e=e))
异步def get(代码):
将aiohttp.ClientSession(loop=loop)作为客户端进行异步:
url=”https://httpstat.us/{code}.格式(code=code)
等待获取url(客户端,url)
打印(“完成:{code}”。格式(code=code))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
启动http服务器(8080)
loop=asyncio.get\u event\u loop()
虽然(正确):
期货=[
获取中代码的(代码){
200: 1,
202: 2,
204: 3,
301: 4,
400: 5,
401: 6,
410: 7,
500: 8,
}
]
结果=循环。运行直到完成(asyncio.gather(*futures))
时间。睡眠(10)
和卷曲http://localhost:8080/metrics
收益率:
# HELP requests_total Total count of requests
# TYPE requests_total counter
requests_total{code="202"} 2.0
requests_total{code="500"} 2.0
requests_total{code="400"} 2.0
requests_total{code="200"} 4.0
requests_total{code="410"} 2.0
requests_total{code="204"} 2.0
requests_total{code="401"} 2.0
# HELP requests_created Total count of requests
# TYPE requests_created gauge
requests_created{code="202"} 1.6221402488908508e+09
requests_created{code="500"} 1.6221402488980236e+09
requests_created{code="400"} 1.6221402488994935e+09
requests_created{code="200"} 1.6221402489686863e+09
requests_created{code="410"} 1.6221402490579622e+09
requests_created{code="204"} 1.6221402493150167e+09
requests_created{code="401"} 1.622140249339906e+09
注意我不确定客户为什么会生成创建的请求