!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对应
This commit is contained in:
py1ren
2025-01-24 01:34:33 +00:00
committed by insistence
parent 1cfd85f9de
commit 00011f8419
6 changed files with 197 additions and 6 deletions

View File

@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""
@author: peng
@file: span.py
@time: 2025/1/17 16:57
"""
from contextlib import asynccontextmanager
from starlette.types import Scope, Message
from .ctx import TraceCtx
class Span:
"""
整个http生命周期
request(before) --> request(after) --> response(before) --> response(after)
"""
def __init__(self, scope: Scope):
self.scope = scope
async def request_before(self):
"""
request_before: 处理header信息等, 如记录请求体信息
"""
TraceCtx.set_id()
async def request_after(self, message: Message):
"""
request_after: 处理请求bytes 如记录请求参数
example:
message: {'type': 'http.request', 'body': b'{\r\n "name": "\xe8\x8b\x8f\xe8\x8b\x8f\xe8\x8b\x8f"\r\n}', 'more_body': False}
"""
return message
async def response(self, message: Message):
"""
if message['type'] == "http.response.start": -----> request-before
pass
if message['type'] == "http.response.body": -----> request-after
message.get('body', b'')
pass
"""
if message['type'] == 'http.response.start':
message['headers'].append((b'request-id', TraceCtx.get_id().encode()))
return message
@asynccontextmanager
async def get_current_span(scope: Scope):
yield Span(scope)