otel

package
v3.1.34 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package otel 提供 OpenTelemetry Trace 上下文传播中间件。

使用方式:

app.Use(otel.New())

功能:

  • 从请求中提取 traceparent/tracestate header,注入 context
  • 在响应中回写 trace-id header
  • 可选自动创建 span(需要完整 OTel SDK)

零开销 — 仅 header 读写,不创建 span 时不产生任何分配。 若启用自动 span 创建,需引入 go.opentelemetry.io/otel SDK。

Index

Constants

View Source
const (
	HeaderTraceParent = "traceparent"
	HeaderTraceState  = "tracestate"
	HeaderResponseID  = "X-Trace-ID"
)

Variables

This section is empty.

Functions

func New

func New(conf ...Config) core.HandlerFunc

New 返回 OTel trace 传播中间件(零依赖版本)。 不创建 span,仅传播 traceparent/tracestate header。

func TraceID

func TraceID(ctx context.Context) string

TraceID 从 context 中提取 trace-id(供 logger 使用)。

Types

type Config

type Config struct {
	// WriteOnResponse 是否在响应头中写入 trace-id
	WriteOnResponse bool
	// TraceIDHeader 响应中 trace-id 的 header 名,默认 X-Trace-ID
	TraceIDHeader string
	// SpanName 可选:自动创建 span 时的名称前缀。空字符串表示不自动创建 span。
	SpanName string
	// TracerProvider 可选:自定义 TracerProvider(需要完整 SDK)。
	TracerProvider any // interface{} 避免强制引入 OTel SDK 依赖
}

Config OTel 配置

Jump to

Keyboard shortcuts

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