使用自定义python修饰符接受具有cherrypy公开端点的参数
我正在尝试使用prometheus监控应用程序的延迟。有一个decorator函数可以计算函数执行所需的时间。现在,当我用cherrypy暴露端点包装它时,它没有响应 我还尝试在我的decorator上使用@cherrypy.tools.register('before_handler'),然后将其附加为@cherrypy.tools.monitor_request(),但当decorator接受函数时,它会通过一个参数异常使用自定义python修饰符接受具有cherrypy公开端点的参数,python,function,python-decorators,cherrypy,Python,Function,Python Decorators,Cherrypy,我正在尝试使用prometheus监控应用程序的延迟。有一个decorator函数可以计算函数执行所需的时间。现在,当我用cherrypy暴露端点包装它时,它没有响应 我还尝试在我的decorator上使用@cherrypy.tools.register('before_handler'),然后将其附加为@cherrypy.tools.monitor_request(),但当decorator接受函数时,它会通过一个参数异常 def monitor_request(func): def
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"
我没有返回cherrypy end point的响应结果,这就是问题所在。正确的代码应该是
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
在
internal
中调用时,您忽略了func
的返回。func
有一些副作用,或者这就是你的问题。是的,正确的方法是存储func的返回值,然后在inner1函数的末尾返回。这解决了你的问题吗?是的,它起作用了。是的,当然,完成了。