【发布时间】:2020-02-14 18:08:15
【问题描述】:
我正在尝试使用 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"
【问题讨论】:
-
在
inner中调用时,您忽略了func的返回。func有一些副作用,或者那是你的问题。 -
是的,正确的做法是将func的返回值存储起来,然后在inner1函数的末尾返回。
-
这能解决您的问题吗?
-
是的,它成功了。
-
是的,完成。
标签: python function python-decorators cherrypy