trace

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	ServiceName:    "xlgo-service",
	ServiceVersion: "1.0.0",
	Environment:    "development",
	ExporterType:   "otlp-http",
	Endpoint:       "localhost:4318",
	SampleRatio:    1.0,
	Enabled:        false,
	Propagator:     "w3c",
}

DefaultConfig 默认配置

Functions

func AddAttributes

func AddAttributes(c *gin.Context, attrs ...attribute.KeyValue)

AddAttributes 添加属性到当前 Span

func Close

func Close(ctx context.Context) error

Close 关闭链路追踪。

H-14 修复:去掉 sync.Once。原实现 Close→Init→Close 第二次因 once 已消费而 no-op, 新 provider 的 exporter 后台 goroutine/连接泄漏。改为每次 Swap 出当前 provider 并 Shutdown,Store 回兜底 NeverSample provider(C13a:Close 后再用已关闭 provider)。 幂等:重复 Close 时 Swap 得到的是无 exporter 的兜底 provider,Shutdown 无害。 并发安全:Swap 原子返回唯一指针,不会 double-Shutdown 同一 provider。

func GetContext

func GetContext(c *gin.Context) context.Context

GetContext 从 Gin Context 获取 OpenTelemetry Context

C13:裸断言改 comma-ok,防 "otel_ctx" 被外部置为非 context 值时 panic。

func GetTraceID

func GetTraceID(c *gin.Context) string

GetTraceID 获取当前 TraceID

func GetTracer

func GetTracer() trace.Tracer

GetTracer 获取全局 Tracer

func Init

func Init(cfg Config) error

Init 初始化链路追踪

func Middleware

func Middleware(serviceName string) gin.HandlerFunc

Middleware Gin 中间件

func RecordError

func RecordError(c *gin.Context, err error)

RecordError 记录错误

func RecordErrorToSpan

func RecordErrorToSpan(span trace.Span, err error)

RecordErrorToSpan 记录错误到指定 Span

func SetAttribute

func SetAttribute(c *gin.Context, key string, value any)

SetAttribute 设置单个属性

func StartSpan

func StartSpan(c *gin.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)

StartSpan 创建子 Span

func StartSpanFromContext

func StartSpanFromContext(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)

StartSpanFromContext 从 Context 创建 Span

func TracerProvider added in v1.2.0

func TracerProvider() *sdktrace.TracerProvider

TracerProvider 全局 TracerProvider(导出供高级用法;返回当前快照)。

Types

type Config

type Config struct {
	// ServiceName 服务名称
	ServiceName string
	// ServiceVersion 服务版本
	ServiceVersion string
	// Environment 运行环境
	Environment string
	// ExporterType 导出器类型: "otlp-http", "otlp-grpc", "stdout"
	ExporterType string
	// Endpoint OTLP 导出器地址
	Endpoint string
	// Insecure 是否使用明文(无 TLS)连接 collector。
	// 默认 false(TLS);对 localhost:4318 等明文 collector 需显式置 true(C13c)。
	Insecure bool
	// SampleRatio 采样比例 (0.0-1.0)
	SampleRatio float64
	// Enabled 是否启用
	Enabled bool
	// Propagator 传播器类型: "w3c", "b3", "jaeger"
	Propagator string
}

Config 链路追踪配置

Jump to

Keyboard shortcuts

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