trace

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package trace 提供请求链路追踪上下文信息的存取。

设计约定:

  • 本包只做「结构体 + context 存取」,不做任何 HTTP/gRPC/反射逻辑;
  • ctx 中的 *Info 由网关入口中间件通过 WithInfo 写入, 各服务通过 GetInfo 读取,日志/grpcclient 自动提取并注入。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TraceIDFromCtx

func TraceIDFromCtx(ctx context.Context) string

TraceIDFromCtx 从 ctx 读取 TraceID,未写入时返回空字符串。

func WithInfo

func WithInfo(ctx context.Context, info *Info) context.Context

WithInfo 将 info 写入 ctx 并返回新 ctx。ctx 为 nil 时回退 Background。

func WithTraceID

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

WithTraceID 将 traceID 写入 ctx(便捷方法,保留已有 Info 的其他字段)。 拷贝结构体而非原地修改,避免污染共享的 *Info。

Types

type Info

type Info struct {
	TraceID  string
	RPCID    string
	Sampled  string
	UserID   string
	UserName string
}

Info 链路追踪上下文信息。

func GetInfo

func GetInfo(ctx context.Context) *Info

GetInfo 从 ctx 读取 *Info;未写入或 ctx 为 nil 时返回 nil。

Jump to

Keyboard shortcuts

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