# context **Repository Path**: xgo-core/context ## Basic Information - **Project Name**: context - **Description**: context - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-22 - **Last Updated**: 2026-07-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # XGo Context `gitee.com/xgo-core/context` 提供一个轻量的上下文封装,适用于需要在普通 Go 代码、gin 和 gRPC 之间统一上下文类型的服务。 [English](README.md) ## 功能 - 支持从 `context.Background()` 创建 `XGoContext`。 - 支持包装已有的 `context.Context`。 - 支持包装 gin `*gin.Context`,并使用其 request context 作为标准 context。 - 支持包装 gRPC `context.Context`。 - 支持获取原始来源 context。 - `*XGoContext` 实现标准 `context.Context` 接口。 - 支持从包装的 context 中读取 OpenTelemetry span 信息。 - 支持基于包装的 context 创建 OpenTelemetry 子 span。 ## 安装 ```sh go get gitee.com/xgo-core/context ``` ## 使用方式 ### 从 Background 创建 ```go package main import xcontext "gitee.com/xgo-core/context" func main() { ctx := xcontext.New() _ = ctx.Context() _ = ctx.RawContext() } ``` ### 从标准 Context 创建 ```go package main import ( "context" "time" xcontext "gitee.com/xgo-core/context" ) func main() { base, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() ctx, err := xcontext.NewFromContext(base) if err != nil { panic(err) } baseCtx, err := ctx.BaseContext() if err != nil { panic(err) } _ = baseCtx } ``` ### 从 Gin 创建 ```go package main import ( xcontext "gitee.com/xgo-core/context" "github.com/gin-gonic/gin" ) func handler(c *gin.Context) { ctx, err := xcontext.NewFromGin(c) if err != nil { c.Error(err) return } ginCtx, err := ctx.GinContext() if err != nil { c.Error(err) return } _ = ginCtx } ``` ### 从 gRPC 创建 ```go package main import ( "context" xcontext "gitee.com/xgo-core/context" ) func handler(grpcCtx context.Context) error { ctx, err := xcontext.NewFromGRPC(grpcCtx) if err != nil { return err } rawGRPCCtx, err := ctx.GRPCContext() if err != nil { return err } _ = rawGRPCCtx return nil } ``` ## 标准 Context 兼容 `*XGoContext` 实现了 `context.Context`,因此可以直接传给任何接收标准 context 的 API。 ```go func query(ctx context.Context) error { return nil } func run(ctx *xcontext.XGoContext) error { return query(ctx) } ``` 以下方法会委托给内部包装的标准 context: - `Deadline()` - `Done()` - `Err()` - `Value(key any)` ## OpenTelemetry 链路追踪 本包会从包装的 `context.Context` 中读取链路追踪信息,但不会安装 gin 或 gRPC 中间件,也不会自行解析 HTTP header 或 gRPC metadata。请先使用标准 OpenTelemetry middleware 或 interceptor 将 span 信息写入请求 context,再构造 `XGoContext`。 ```go ctx, err := xcontext.NewFromContext(parent) if err != nil { return err } traceID := ctx.TraceID() spanID := ctx.SpanID() child, span, err := ctx.StartSpan("operation") if err != nil { return err } defer span.End() _ = child _ = traceID _ = spanID ``` 可以配置 `StartSpan` 使用的 tracer: ```go tracer := xcontext.NewTracer("my-service") xcontext.SetTracer(tracer) ``` 如果没有配置包级 tracer,`StartSpan` 会使用 OpenTelemetry 全局 tracer provider,并使用默认 tracer 名称 `gitee.com/xgo-core/context`。 ## API ### 构造函数 | API | 说明 | | --- | --- | | `New() *XGoContext` | 基于 `context.Background()` 创建 `XGoContext`。 | | `NewFromContext(context.Context) (*XGoContext, error)` | 从标准 context 创建 `XGoContext`。 | | `NewFromGin(*gin.Context) (*XGoContext, error)` | 从 gin 创建 `XGoContext`。 | | `NewFromGRPC(context.Context) (*XGoContext, error)` | 从 gRPC 创建 `XGoContext`。 | ### Context 获取 | API | 说明 | | --- | --- | | `Context() context.Context` | 返回包装的标准 context。 | | `RawContext() any` | 返回原始来源 context。 | | `Source() XGoContextSource` | 返回来源类型。 | | `BaseContext() (context.Context, error)` | 当来源是 `SourceContext` 时,返回原始标准 context。 | | `GinContext() (*gin.Context, error)` | 当来源是 `SourceGin` 时,返回原始 gin context。 | | `GRPCContext() (context.Context, error)` | 当来源是 `SourceGrpc` 时,返回原始 gRPC context。 | ### 链路追踪 | API | 说明 | | --- | --- | | `TraceContext() context.Context` | 返回用于链路追踪的 context。 | | `Span() trace.Span` | 从包装的 context 返回当前 span。 | | `SpanContext() trace.SpanContext` | 返回当前 span context。 | | `TraceID() string` | 返回当前 trace ID;不存在时返回空字符串。 | | `SpanID() string` | 返回当前 span ID;不存在时返回空字符串。 | | `StartSpan(string, ...trace.SpanStartOption) (*XGoContext, trace.Span, error)` | 创建子 span,并返回新的 `XGoContext`。 | | `SetTracer(trace.Tracer)` | 设置包级 tracer。 | | `GetTracer() trace.Tracer` | 返回包级 tracer;未设置时返回默认全局 tracer。 | | `NewTracer(string, ...trace.TracerOption) trace.Tracer` | 创建并保存包级 tracer。 | ## 错误 本包会针对非法输入和来源不匹配返回类型化的 `XGoContextError`。 ```go ctx, err := xcontext.NewFromGin(nil) if err == xcontext.GinContextNilError { // 处理 nil gin context } _ = ctx ``` 可用来源常量: - `SourceContext` - `SourceGin` - `SourceGrpc`