Files
RuoYi-Vue3-FastAPI/ruoyi-fastapi-backend/middlewares/trace_middleware/middle.py
py1ren 00011f8419 !27 feat: 新增trace中间件强化日志链路追踪和响应头
* refactor: trace_log重命名为trace_middleware
* refactor: 日志处理器重构为类式写法
* perf: 移除无用文件
* perf: 优化trace中间件部分写法
* style: 格式化代码
* Merge branch 'master' into develop
* feature: 1.日志添加traceId链路追踪 2.response-header默认添加request-id与traceId对应
2025-01-24 01:34:33 +00:00

48 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
"""
@author: peng
@file: middle.py
@time: 2025/1/17 16:57
"""
from functools import wraps
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from .span import get_current_span, Span
class TraceASGIMiddleware:
"""
fastapi-example:
app = FastAPI()
app.add_middleware(TraceASGIMiddleware)
"""
def __init__(self, app: ASGIApp) -> None:
self.app = app
@staticmethod
async def my_receive(receive: Receive, span: Span):
await span.request_before()
@wraps(receive)
async def my_receive():
message = await receive()
await span.request_after(message)
return message
return my_receive
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope['type'] != 'http':
await self.app(scope, receive, send)
return
async with get_current_span(scope) as span:
handle_outgoing_receive = await self.my_receive(receive, span)
async def handle_outgoing_request(message: 'Message') -> None:
await span.response(message)
await send(message)
await self.app(scope, handle_outgoing_receive, handle_outgoing_request)