tracing

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 GORM 数据库操作的追踪插件

Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 HTTP 客户端的追踪传输层,支持跨服务追踪传播

Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 Redis 操作的追踪钩子

Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 OpenTelemetry SDK 的初始化和核心追踪功能

Index

Constants

This section is empty.

Variables

View Source
var Tracer trace.Tracer

Tracer 全局追踪器实例

Functions

func AddSpanEvent

func AddSpanEvent(span trace.Span, name string, attrs ...attribute.KeyValue)

AddSpanEvent 添加 Span 事件 参数:

  • span: 目标 Span
  • name: 事件名称
  • attrs: 事件属性

func GetSpanID

func GetSpanID(ctx context.Context) string

GetSpanID 从 context 获取 SpanID 参数:

  • ctx: 上下文

返回:

  • string: SpanID 字符串,如果无效则返回空字符串

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID 从 context 获取 TraceID 参数:

  • ctx: 上下文

返回:

  • string: TraceID 字符串,如果无效则返回空字符串

func InitTracer

func InitTracer(cfg *config.TracingConfig) (func(context.Context) error, error)

InitTracer 初始化 OpenTelemetry Tracer 该函数会: 1. 根据配置创建对应的导出器(OTLP、Jaeger、stdout) 2. 创建资源信息,标识服务名称和环境 3. 配置采样策略 4. 设置全局 TracerProvider 和 Propagator 参数:

  • cfg: 链路追踪配置

返回:

  • func(context.Context) error: 关闭函数,用于优雅关闭追踪器
  • error: 初始化错误

func IsDBTracingEnabled

func IsDBTracingEnabled() bool

IsDBTracingEnabled 返回数据库追踪是否已启用

func IsEnabled

func IsEnabled() bool

IsEnabled 返回链路追踪是否已启用

func IsHTTPClientTracingEnabled

func IsHTTPClientTracingEnabled() bool

IsHTTPClientTracingEnabled 返回 HTTP 客户端追踪是否已启用

func IsRedisTracingEnabled

func IsRedisTracingEnabled() bool

IsRedisTracingEnabled 返回 Redis 追踪是否已启用

func NewTracingHTTPClient

func NewTracingHTTPClient() *http.Client

NewTracingHTTPClient 创建带追踪功能的 HTTP 客户端 返回:

  • *http.Client: 配置了追踪 Transport 的 HTTP 客户端

func SetSpanAttributes

func SetSpanAttributes(span trace.Span, attrs ...attribute.KeyValue)

SetSpanAttributes 设置 Span 属性 参数:

  • span: 目标 Span
  • attrs: 属性键值对

func SetSpanError

func SetSpanError(span trace.Span, err error)

SetSpanError 设置 Span 错误状态 参数:

  • span: 目标 Span
  • err: 错误信息

func SpanFromContext

func SpanFromContext(ctx context.Context) trace.Span

SpanFromContext 从 context 获取当前 Span 参数:

  • ctx: 上下文

返回:

  • trace.Span: 当前 Span,如果不存在则返回 NoOp Span

func StartSpan

func StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

StartSpan 开始一个新的 Span 参数:

  • ctx: 父上下文
  • name: Span 名称
  • opts: Span 选项

返回:

  • context.Context: 包含新 Span 的上下文
  • trace.Span: 新创建的 Span

func WrapHTTPClient

func WrapHTTPClient(client *http.Client) *http.Client

WrapHTTPClient 为现有的 HTTP 客户端添加追踪功能 参数:

  • client: 现有的 HTTP 客户端

返回:

  • *http.Client: 添加了追踪功能的 HTTP 客户端

Types

type GormTracingPlugin

type GormTracingPlugin struct {
	// contains filtered or unexported fields
}

GormTracingPlugin GORM 链路追踪插件 实现了 gorm.Plugin 接口,用于追踪数据库操作

func NewGormTracingPlugin

func NewGormTracingPlugin(dbName ...string) *GormTracingPlugin

NewGormTracingPlugin 创建新的 GORM 追踪插件实例 参数:

  • dbName: 数据库名称,可选,用于在追踪中标识数据库

返回:

  • *GormTracingPlugin: 追踪插件实例

func (*GormTracingPlugin) Initialize

func (p *GormTracingPlugin) Initialize(db *gorm.DB) error

Initialize 初始化插件,注册 GORM 回调函数 实现 gorm.Plugin 接口 该函数会: 1. 注册创建、查询、更新、删除等操作的 Before 回调(创建 Span) 2. 注册对应操作的 After 回调(结束 Span 并记录信息)

func (*GormTracingPlugin) Name

func (p *GormTracingPlugin) Name() string

Name 返回插件名称 实现 gorm.Plugin 接口

type RedisTracingHook

type RedisTracingHook struct {
	// contains filtered or unexported fields
}

RedisTracingHook Redis 链路追踪钩子 实现了 redis.Hook 接口,用于追踪 Redis 操作

func NewRedisTracingHook

func NewRedisTracingHook(addr string, aliasName string, db int) *RedisTracingHook

NewRedisTracingHook 创建新的 Redis 追踪钩子实例 参数:

  • addr: Redis 服务器地址
  • aliasName: Redis 实例别名,可选
  • db: Redis 数据库编号

返回:

  • *RedisTracingHook: 追踪钩子实例

func (*RedisTracingHook) DialHook

func (h *RedisTracingHook) DialHook(next redis.DialHook) redis.DialHook

DialHook 连接建立钩子 实现 redis.Hook 接口

func (*RedisTracingHook) ProcessHook

func (h *RedisTracingHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook

ProcessHook 单个命令处理钩子 实现 redis.Hook 接口 该函数会: 1. 在命令执行前创建 Span 2. 记录命令名称和参数 3. 在命令执行后记录错误(如果有) 4. 结束 Span

func (*RedisTracingHook) ProcessPipelineHook

ProcessPipelineHook 管道命令处理钩子 实现 redis.Hook 接口 该函数会追踪 Pipeline 中的批量命令执行

type TracingTransport

type TracingTransport struct {
	// Base 底层的 HTTP Transport
	Base http.RoundTripper
}

TracingTransport 带链路追踪的 HTTP Transport 实现了 http.RoundTripper 接口,用于追踪出站 HTTP 请求 并将追踪上下文传播到下游服务

func NewTracingTransport

func NewTracingTransport(base http.RoundTripper) *TracingTransport

NewTracingTransport 创建新的追踪 Transport 参数:

  • base: 底层的 http.RoundTripper,如果为 nil 则使用 http.DefaultTransport

返回:

  • *TracingTransport: 追踪 Transport 实例

func (*TracingTransport) RoundTrip

func (t *TracingTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip 执行 HTTP 请求并追踪 实现 http.RoundTripper 接口 该函数会: 1. 创建出站 HTTP 请求的 Span 2. 将追踪上下文注入到请求头中(用于跨服务传播) 3. 执行请求并记录响应状态 4. 如果发生错误,记录错误信息

Jump to

Keyboard shortcuts

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