contextx

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package contextx provides goroutine-local context for tracing and logging.

Index

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 EndSpan

func EndSpan()

EndSpan ends the current span and restores parent.

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

func ExtractTraceContext(ctx context.Context) (traceID string, sessionID string, turnID int64)

ExtractTraceContext extracts the full TS-03 three-tuple from a context.

func GetParentSpan

func GetParentSpan() string

GetParentSpan returns the parent span ID.

func GetSessionID

func GetSessionID() string

GetSessionID returns the session ID for current goroutine.

func GetSpanID

func GetSpanID() string

GetSpanID returns the current span ID.

func GetStage

func GetStage() string

GetStage returns the stage name for current goroutine.

func GetTID

func GetTID() string

GetTID returns the trace ID for current goroutine.

func GetTurnID

func GetTurnID() int64

GetTurnID returns the turn ID for current goroutine.

func Go

func Go(fn func())

Go starts a new goroutine with inherited context.

func GoroutineID

func GoroutineID() int64

GoroutineID returns the current goroutine's ID.

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

func ModelID(ctx context.Context) string

ModelID extracts the model ID from a context.Context. Returns empty string if not set.

func SessionID

func SessionID(ctx context.Context) string

SessionID extracts the session ID from a context.Context.

func SetSessionID

func SetSessionID(id string)

SetSessionID sets the session ID for current goroutine.

func SetStage

func SetStage(stg string)

SetStage sets the stage name 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 StartSpan

func StartSpan(name string) string

StartSpan starts a new span and returns the span ID.

func TraceID

func TraceID(ctx context.Context) string

TraceID extracts the trace ID from a context.Context.

func TurnIDFromCtx

func TurnIDFromCtx(ctx context.Context) int64

TurnID extracts the turn ID from a context.Context.

func WithModelID

func WithModelID(ctx context.Context, modelID string) context.Context

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

func WithSessionID(ctx context.Context, id string) context.Context

WithSessionID returns a new context.Context with the session ID set.

func WithSpan

func WithSpan(name string, fn func())

WithSpan runs a function within a span.

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

func WithTraceID(ctx context.Context, id string) context.Context

WithTraceID returns a new context.Context with the trace ID set.

func WithTurnID

func WithTurnID(ctx context.Context, id int64) context.Context

WithTurnID returns a new context.Context with the turn 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.

func Clone

func Clone() *Context

Clone returns a copy of current context for passing to child goroutine.

func Current

func Current() *Context

Current returns the current goroutine's context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL