程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Adding event listeners to an api endpoint and listening to every request sent to it like a heartbeat signal

发布于2024-10-31 20:34     阅读(621)     评论(0)     点赞(16)     收藏(1)


I have a FastAPI endpoint as follows

#Endpoints 
@app.post("/random_endpoint")
def random_endpoint(input_item: str) -> str: 
    # doing some work 
    # ... 
    return input_item 

if __name__ == '__main__': 
    # add event listeners here 
    
    # launch server 
    uvicorn.run(port=4000)
    

I want to make it so that once this endpoint is hit by the user, the logs as a response status code or if an error was encountered from it it is going to run a certain logger function that will send it to a database listening to it.

def logger(response: Dict[str, str] | Exception):
    # posts this response or exception to a database that is meant to store logging 
    pass 

I have two queries. One How do I do this? and two, what is a good database I can use that will allow me to view these logs in real time.

One thing I think I can try is use a try-catch approach.

Every time a response is generated successfully I can post a client-server interaction. If it doesn't work then I can log the error encountered which seems like a fine idea to me. It will work fine.

I also viewed this link at Stack Overflow: How do I make an event listener with decorators in Python?

Which didn't really make much sense to me, since the endpoint already has a decorator. I might be understanding it wrong.


解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2040407/3cc0f876d930b5b35f3b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

16 0
收藏该文
已收藏

评论内容:(最多支持255个字符)