Documentation
¶
Overview ¶
Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 GORM 数据库操作的追踪插件
Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 HTTP 客户端的追踪传输层,支持跨服务追踪传播
Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 Redis 操作的追踪钩子
Package tracing 提供基于 OpenTelemetry 的分布式链路追踪功能 本文件实现了 OpenTelemetry SDK 的初始化和核心追踪功能
Index ¶
- Variables
- func AddSpanEvent(span trace.Span, name string, attrs ...attribute.KeyValue)
- func GetSpanID(ctx context.Context) string
- func GetTraceID(ctx context.Context) string
- func InitTracer(cfg *config.TracingConfig) (func(context.Context) error, error)
- func IsDBTracingEnabled() bool
- func IsEnabled() bool
- func IsHTTPClientTracingEnabled() bool
- func IsRedisTracingEnabled() bool
- func NewTracingHTTPClient() *http.Client
- func SetSpanAttributes(span trace.Span, attrs ...attribute.KeyValue)
- func SetSpanError(span trace.Span, err error)
- func SpanFromContext(ctx context.Context) trace.Span
- func StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
- func WrapHTTPClient(client *http.Client) *http.Client
- type GormTracingPlugin
- type RedisTracingHook
- type TracingTransport
Constants ¶
This section is empty.
Variables ¶
var Tracer trace.Tracer
Tracer 全局追踪器实例
Functions ¶
func InitTracer ¶
InitTracer 初始化 OpenTelemetry Tracer 该函数会: 1. 根据配置创建对应的导出器(OTLP、Jaeger、stdout) 2. 创建资源信息,标识服务名称和环境 3. 配置采样策略 4. 设置全局 TracerProvider 和 Propagator 参数:
- cfg: 链路追踪配置
返回:
- func(context.Context) error: 关闭函数,用于优雅关闭追踪器
- error: 初始化错误
func IsHTTPClientTracingEnabled ¶
func IsHTTPClientTracingEnabled() bool
IsHTTPClientTracingEnabled 返回 HTTP 客户端追踪是否已启用
func IsRedisTracingEnabled ¶
func IsRedisTracingEnabled() bool
IsRedisTracingEnabled 返回 Redis 追踪是否已启用
func NewTracingHTTPClient ¶
NewTracingHTTPClient 创建带追踪功能的 HTTP 客户端 返回:
- *http.Client: 配置了追踪 Transport 的 HTTP 客户端
func SpanFromContext ¶
SpanFromContext 从 context 获取当前 Span 参数:
- ctx: 上下文
返回:
- trace.Span: 当前 Span,如果不存在则返回 NoOp Span
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 ¶
func (h *RedisTracingHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.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 实例