Documentation
¶
Overview ¶
Package contextx provides goroutine-local context for tracing and logging.
Index ¶
- func Clear()
- func EndSpan()
- func EnterStage(sub string) func()
- func ExtractTraceContext(ctx context.Context) (traceID string, sessionID string, turnID int64)
- func GetParentSpan() string
- func GetSessionID() string
- func GetSpanID() string
- func GetStage() string
- func GetTID() string
- func GetTurnID() int64
- func Go(fn func())
- func GoroutineID() int64
- func Inherit(parent *Context)
- func InjectTraceContext(ctx context.Context, traceID string, sessionID string, turnID int64) context.Context
- func ModelID(ctx context.Context) string
- func SessionID(ctx context.Context) string
- func SetSessionID(id string)
- func SetStage(stg string)
- func SetTID(tid string)
- func SetTurnID(id int64)
- func StartSpan(name string) string
- func TraceID(ctx context.Context) string
- func TurnIDFromCtx(ctx context.Context) int64
- func WithModelID(ctx context.Context, modelID string) context.Context
- func WithSessionID(ctx context.Context, id string) context.Context
- func WithSpan(name string, fn func())
- func WithStage(stg string) func()
- func WithTID(tid string) func()
- func WithTraceID(ctx context.Context, id string) context.Context
- func WithTurnID(ctx context.Context, id int64) context.Context
- type Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clear ¶
func Clear()
Clear removes context for current goroutine. Should be called when goroutine finishes to avoid memory leak.
func EnterStage ¶
func EnterStage(sub string) func()
EnterStage sets stage and returns a cleanup function that restores previous. Usage: defer contextx.EnterStage("intent")()
func ExtractTraceContext ¶
ExtractTraceContext extracts the full TS-03 three-tuple from a context.
func GetSessionID ¶
func GetSessionID() string
GetSessionID returns the session ID for current goroutine.
func Inherit ¶
func Inherit(parent *Context)
Inherit sets current goroutine's context from a parent context.
func InjectTraceContext ¶
func InjectTraceContext(ctx context.Context, traceID string, sessionID string, turnID int64) context.Context
InjectTraceContext sets the full TS-03 three-tuple (traceId + sessionId + turnId) into a context.
func ModelID ¶
ModelID extracts the model ID from a context.Context. Returns empty string if not set.
func SetSessionID ¶
func SetSessionID(id string)
SetSessionID sets the session ID for current goroutine.
func SetTID ¶
func SetTID(tid string)
SetTID sets the trace ID for current goroutine. Format: "场景/唯一标识", e.g., "chat/req-001", "upgrade/task-xyz"
func SetTurnID ¶
func SetTurnID(id int64)
SetTurnID sets the turn ID for current goroutine (TS-03 three-tuple).
func TurnIDFromCtx ¶
TurnID extracts the turn ID from a context.Context.
func WithModelID ¶
WithModelID returns a new context.Context with the model ID set. Used to propagate user-selected model from HTTP layer to LLM executor.
func WithSessionID ¶
WithSessionID returns a new context.Context with the session ID set.
func WithStage ¶
func WithStage(stg string) func()
WithStage returns a function that sets stage and restores previous on cleanup. Usage: defer contextx.WithStage("http/handler")()
func WithTID ¶
func WithTID(tid string) func()
WithTID returns a function that sets TID and returns a cleanup function. Usage: defer contextx.WithTID("chat/req-001")()
func WithTraceID ¶
WithTraceID returns a new context.Context with the trace ID set.
Types ¶
type Context ¶
type Context struct {
TID string // 业务流跟踪ID (entry_type/unique_id)
Stage string // 阶段名称 (业务流优先: chat/intent, not http/handler/chat/intent)
SessionID string // 会话ID (用于 Memory 路由和 LanePool 分发)
TurnID int64 // Turn 序号 (TS-03 三元组: traceId + sessionId + turnId)
SpanID string // 当前 Span ID
ParentSpan string // 父 Span ID
SpanStack []string // Span 栈 (用于嵌套)
}
Context holds trace context for the current goroutine.