gpt4 book ai didi

python - 如何更新计数器 : set new value after avery request, 不将新值增加到以前的值?

转载 作者:行者123 更新时间:2023-12-05 07:38:24 25 4
gpt4 key购买 nike

想用python创建Prometheus客户端

Tried to use this module

获取此代码

from prometheus_client import Counter
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
c.labels(method='get', endpoint='/').inc()
c.labels(method='post', endpoint='/submit').inc()

很好,但我想在每次请求后设置新值,而不是将新值增加到以前的值。

怎么做到的?

最佳答案

您必须使用仪表而不是计数器。

自述文件中的示例:

from prometheus_client import Gauge
g = Gauge('my_inprogress_requests', 'Description of gauge')
g.inc() # Increment by 1
g.dec(10) # Decrement by given value
g.set(4.2) # Set to a given value

另请参阅 Prometheus 指标类型:https://prometheus.io/docs/concepts/metric_types/

Counter

A counter is a cumulative metric that represents a single numerical value that only ever goes up. A counter is typically used to count requests served, tasks completed, errors occurred, etc. Counters should not be used to expose current counts of items whose number can also go down, e.g. the number of currently running goroutines. Use gauges for this use case.

Gauge

A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.

Gauges are typically used for measured values like temperatures or current memory usage, but also "counts" that can go up and down, like the number of running goroutines.

关于python - 如何更新计数器 : set new value after avery request, 不将新值增加到以前的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47929310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com