Documentation
¶
Overview ¶
错误码定义:统一以 TRACEX_ 为前缀(errx 家族规范)。
Package tracex 提供 OpenTelemetry 链路追踪基座: TracerProvider 管理、HTTP 中间件、logx 日志字段与内存导出器。
Index ¶
- Constants
- func AddSpanEvent(ctx context.Context, name string, attrs ...attribute.KeyValue)
- func BaggageValue(ctx context.Context, key string) string
- func LogFields(ctx context.Context) logx.FieldGroup
- func RecordError(ctx context.Context, err error)
- func WithBaggage(ctx context.Context, key, value string) context.Context
- type Config
- type ExporterKind
- type LogHook
- type Manager
- func (m *Manager) Config() Config
- func (m *Manager) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context
- func (m *Manager) Inject(ctx context.Context, carrier propagation.TextMapCarrier)
- func (m *Manager) Middleware(next http.Handler) http.Handler
- func (m *Manager) Propagator() propagation.TextMapPropagator
- func (m *Manager) RoundTripper(next http.RoundTripper) http.RoundTripper
- func (m *Manager) Shutdown(ctx context.Context) error
- func (m *Manager) Spans() []SpanSnapshot
- func (m *Manager) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
- func (m *Manager) Tracer() trace.Tracer
- type MemoryExporter
- type SpanEvent
- type SpanSnapshot
- type TraceAttr
- type TraceHook
Constants ¶
const ( CodeInvalidConfig errx.Code = "TRACEX_INVALID_CONFIG" CodeExporterFailed errx.Code = "TRACEX_EXPORTER_FAILED" CodeShutdownFailed errx.Code = "TRACEX_SHUTDOWN_FAILED" )
const Version = "v1.3.0"
Version 是当前库版本,与 git tag 保持一致。
Variables ¶
This section is empty.
Functions ¶
func AddSpanEvent ¶
AddSpanEvent 向当前 span 记录事件;无活动 span 时忽略。
func BaggageValue ¶
BaggageValue 读取上下文中的 bag 成员值;不存在返回空串。
func LogFields ¶
func LogFields(ctx context.Context) logx.FieldGroup
LogFields 从上下文提取链路信息生成 logx 字段; 无有效链路时返回空字段组。
func RecordError ¶
RecordError 将错误记录到当前 span:记录异常事件并标记 Error 状态。
Types ¶
type Config ¶
type Config struct {
// ServiceName 服务名(必填)。
ServiceName string
// Version 服务版本(可选)。
Version string
// Environment 部署环境(可选)。
Environment string
// SampleRatio 采样率 0~1,0 使用默认 1(全采样)。
SampleRatio float64
// BatchTimeout 批量导出间隔,0 使用默认 5s。
BatchTimeout time.Duration
// Exporter 导出器类型,空使用 stdout。
Exporter ExporterKind
// Writer stdout 导出器输出目标,nil 使用 os.Stdout。
Writer io.Writer
// OTLPEndpoint OTLP/HTTP 端点(host:port)。
OTLPEndpoint string
// OTLPInsecure 是否使用明文 HTTP。
OTLPInsecure bool
// OTLPHeaders OTLP/HTTP 附加请求头。
OTLPHeaders map[string]string
// OTLPTimeout OTLP/HTTP 请求超时,0 使用默认 10s。
OTLPTimeout time.Duration
// SlowThreshold 慢请求阈值;超过时记录 slow 事件,0 关闭。
SlowThreshold time.Duration
// RouteNamer 可选:从请求中提取路由模板(如 /users/{id})用于
// span 命名;返回空串时保持默认命名。
RouteNamer func(r *http.Request) string
// Sampler 可选采样器;nil 使用 TraceIDRatioBased(SampleRatio)。
Sampler sdktrace.Sampler
// SetGlobal 是否注册为 OTel 全局 TracerProvider 与传播器。
SetGlobal bool
}
Config 追踪器配置。
type ExporterKind ¶
type ExporterKind string
ExporterKind 导出器类型。
const ( // ExporterMemory 内存导出器(测试/调试)。 ExporterMemory ExporterKind = "memory" // ExporterStdout 标准输出导出器(默认)。 ExporterStdout ExporterKind = "stdout" // ExporterOTLPHTTP OTLP/HTTP 导出器(生产采集)。 ExporterOTLPHTTP ExporterKind = "otlp-http" )
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 追踪管理器:持有 TracerProvider、导出器与传播器。
func (*Manager) Extract ¶
func (m *Manager) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context
Extract 从 carrier 提取链路上下文(入站请求)。
func (*Manager) Inject ¶
func (m *Manager) Inject(ctx context.Context, carrier propagation.TextMapCarrier)
Inject 将链路上下文写入 carrier(出站请求)。
func (*Manager) Middleware ¶
Middleware 返回标准 net/http 链路追踪中间件: 提取入站链路上下文、记录请求属性与状态码,5xx 标记错误。
func (*Manager) Propagator ¶
func (m *Manager) Propagator() propagation.TextMapPropagator
Propagator 返回组合传播器(TraceContext + Baggage)。
func (*Manager) RoundTripper ¶
func (m *Manager) RoundTripper(next http.RoundTripper) http.RoundTripper
RoundTripper 返回出站 HTTP 链路注入器:自动携带 traceparent、 记录客户端 span,并按状态码/错误标记结果。
func (*Manager) Spans ¶
func (m *Manager) Spans() []SpanSnapshot
Spans 返回内存导出器中的 Span 快照(非内存导出器返回 nil)。
type MemoryExporter ¶
type MemoryExporter struct {
// contains filtered or unexported fields
}
MemoryExporter 内存导出器:供测试与调试读取 Span。
func (*MemoryExporter) ExportSpans ¶
func (e *MemoryExporter) ExportSpans(_ context.Context, spans []sdktrace.ReadOnlySpan) error
ExportSpans 接收并快照 Span。
type SpanSnapshot ¶
type SpanSnapshot struct {
// Name Span 名称。
Name string
// TraceID 十六进制链路 ID。
TraceID string
// SpanID 十六进制 Span ID。
SpanID string
// ParentSpanID 父 Span ID(根 Span 为空)。
ParentSpanID string
// Attributes 属性快照。
Attributes map[string]string
// StatusCode 状态码(Unset/Ok/Error)。
StatusCode string
// StatusMessage 状态描述。
StatusMessage string
// Events 事件快照。
Events []SpanEvent
}
SpanSnapshot Span 快照(内存导出器返回的只读视图)。