
* refactor: trace_log重命名为trace_middleware * refactor: 日志处理器重构为类式写法 * perf: 移除无用文件 * perf: 优化trace中间件部分写法 * style: 格式化代码 * Merge branch 'master' into develop * feature: 1.日志添加traceId链路追踪 2.response-header默认添加request-id与traceId对应
24 lines
427 B
Python
24 lines
427 B
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
@author: peng
|
|
@file: ctx.py
|
|
@time: 2025/1/17 16:57
|
|
"""
|
|
|
|
import contextvars
|
|
from uuid import uuid4
|
|
|
|
CTX_REQUEST_ID: contextvars.ContextVar[str] = contextvars.ContextVar('request-id', default='')
|
|
|
|
|
|
class TraceCtx:
|
|
@staticmethod
|
|
def set_id():
|
|
_id = uuid4().hex
|
|
CTX_REQUEST_ID.set(_id)
|
|
return _id
|
|
|
|
@staticmethod
|
|
def get_id():
|
|
return CTX_REQUEST_ID.get()
|