我正在尝试使用prometheus监视应用程序的延迟。有一个装饰器函数可以计算函数执行所花费的时间。现在,当我用暴露的樱桃端点包装它时,它没有任何响应。
我也尝试过在装饰器上使用@ cherrypy.tools.register('before_handler'),然后将其附加为@ cherrypy.tools.monitor_request(),但由于装饰器接受函数,它将通过参数异常。
def monitor_request(func):
def inner1(*args, **kwargs):
begin = time.time()
func(*args, **kwargs)
end = time.time()
diff = end-begin
REQUEST_LATENCY.labels(func.__name__).observe(diff)
REQUEST_COUNT.labels(func.__name__).inc()
return inner1
@cherrypy.expose
@monitor_request
def health1(self):
"""Give back health status"""
return "is_healthy"
我没有从问题的小结点返回响应结果。正确的代码应该是。
def inner1(*args, **kwargs):
begin = time.time()
x = func(*args, **kwargs)
end = time.time()
diff = end-begin
REQUEST_LATENCY.labels(func.__name__).observe(diff)
REQUEST_COUNT.labels(func.__name__).inc()
return x
return inner1