Documentation
¶
Index ¶
- Variables
- func AddAttributes(c *gin.Context, attrs ...attribute.KeyValue)
- func Close(ctx context.Context) error
- func GetContext(c *gin.Context) context.Context
- func GetTraceID(c *gin.Context) string
- func GetTracer() trace.Tracer
- func Init(cfg Config) error
- func Middleware(serviceName string) gin.HandlerFunc
- func RecordError(c *gin.Context, err error)
- func RecordErrorToSpan(span trace.Span, err error)
- func SetAttribute(c *gin.Context, key string, value any)
- func StartSpan(c *gin.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- func StartSpanFromContext(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- func TracerProvider() *sdktrace.TracerProvider
- type Config
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 ¶
AddAttributes 添加属性到当前 Span
func Close ¶
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 ¶
GetContext 从 Gin Context 获取 OpenTelemetry Context
C13:裸断言改 comma-ok,防 "otel_ctx" 被外部置为非 context 值时 panic。
func RecordErrorToSpan ¶
RecordErrorToSpan 记录错误到指定 Span
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 链路追踪配置
Click to show internal directories.
Click to hide internal directories.