Documentation
¶
Index ¶
- Variables
- func Example()
- func ExampleConcurrentAgents()
- func ExampleWithError()
- func IncrementCounter(name string, value int64, labels map[string]string)
- func RecordHistogram(name string, value float64, labels map[string]string)
- func SetGauge(name string, value float64, labels map[string]string)
- func SetGlobalMetrics(metrics Metrics)
- func SetGlobalTracer(tracer Tracer)
- type AgentMetrics
- func (m *AgentMetrics) RecordError(agentID, errorType string)
- func (m *AgentMetrics) RecordRequest(agentID string, duration time.Duration)
- func (m *AgentMetrics) RecordTokens(agentID string, inputTokens, outputTokens int64)
- func (m *AgentMetrics) RecordToolCall(agentID, toolName string, duration time.Duration, success bool)
- func (m *AgentMetrics) SetActiveAgents(count int)
- func (m *AgentMetrics) SetQueueDepth(agentID string, depth int)
- type Attribute
- type CounterSnapshot
- type GaugeSnapshot
- type HistogramSnapshot
- type Metrics
- type MetricsSnapshot
- type NoopSpan
- type NoopSpanContext
- type NoopTracer
- type OTelOption
- func WithOTelAttributes(attrs ...attribute.KeyValue) OTelOption
- func WithOTelExporter(exporter sdktrace.SpanExporter) OTelOption
- func WithOTelSampler(sampler sdktrace.Sampler) OTelOption
- func WithOTelServiceVersion(version string) OTelOption
- func WithOTelSpanProcessor(processor sdktrace.SpanProcessor) OTelOption
- type OTelSpan
- type OTelSpanContext
- type OTelTracer
- func (t *OTelTracer) Extract(ctx context.Context, carrier any) context.Context
- func (t *OTelTracer) ForceFlush(ctx context.Context) error
- func (t *OTelTracer) Inject(ctx context.Context, carrier any) error
- func (t *OTelTracer) Shutdown(ctx context.Context) error
- func (t *OTelTracer) StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
- type SimpleMetrics
- func (m *SimpleMetrics) IncrementCounter(name string, value int64, labels map[string]string)
- func (m *SimpleMetrics) RecordHistogram(name string, value float64, labels map[string]string)
- func (m *SimpleMetrics) SetGauge(name string, value float64, labels map[string]string)
- func (m *SimpleMetrics) Snapshot() MetricsSnapshot
- type SimpleSpan
- func (s *SimpleSpan) AddEvent(name string, attrs ...Attribute)
- func (s *SimpleSpan) Attributes() []Attribute
- func (s *SimpleSpan) Duration() time.Duration
- func (s *SimpleSpan) End()
- func (s *SimpleSpan) EndTime() time.Time
- func (s *SimpleSpan) Error() error
- func (s *SimpleSpan) Events() []SpanEvent
- func (s *SimpleSpan) Name() string
- func (s *SimpleSpan) RecordError(err error)
- func (s *SimpleSpan) SetAttributes(attrs ...Attribute)
- func (s *SimpleSpan) SetStatus(code StatusCode, description string)
- func (s *SimpleSpan) SpanContext() SpanContext
- func (s *SimpleSpan) StartTime() time.Time
- func (s *SimpleSpan) Status() StatusCode
- func (s *SimpleSpan) StatusDescription() string
- type SimpleSpanContext
- type SimpleTracer
- func (t *SimpleTracer) Extract(ctx context.Context, carrier any) context.Context
- func (t *SimpleTracer) GetSpans() []*SimpleSpan
- func (t *SimpleTracer) Inject(ctx context.Context, carrier any) error
- func (t *SimpleTracer) StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
- type Span
- type SpanConfig
- type SpanContext
- type SpanEvent
- type SpanKind
- type SpanOption
- type StatusCode
- type Tracer
Constants ¶
This section is empty.
Variables ¶
var ( // Agent 相关 AttrAgentID = "agent.id" AttrAgentName = "agent.name" AttrAgentTemplateID = "agent.template_id" // LLM 相关 AttrLLMProvider = "llm.provider" AttrLLMModel = "llm.model" AttrLLMMaxTokens = "llm.max_tokens" AttrLLMInputTokens = "llm.usage.input_tokens" AttrLLMOutputTokens = "llm.usage.output_tokens" AttrLLMTotalTokens = "llm.usage.total_tokens" // Tool 相关 AttrToolName = "tool.name" AttrToolCallID = "tool.call_id" AttrToolDuration = "tool.duration_ms" AttrToolSuccess = "tool.success" // Session 相关 AttrSessionID = "session.id" AttrUserID = "user.id" AttrInvocationID = "invocation.id" AttrBranch = "agent.branch" )
常用的预定义属性键(基于 OpenTelemetry 语义约定)
Functions ¶
func ExampleConcurrentAgents ¶
func ExampleConcurrentAgents()
ExampleConcurrentAgents 演示多 Agent 并发场景
func IncrementCounter ¶
便捷函数
Types ¶
type AgentMetrics ¶
type AgentMetrics struct {
// contains filtered or unexported fields
}
AgentMetrics Agent 相关的指标收集器
func NewAgentMetrics ¶
func NewAgentMetrics(metrics Metrics) *AgentMetrics
NewAgentMetrics 创建 Agent metrics
func (*AgentMetrics) RecordError ¶
func (m *AgentMetrics) RecordError(agentID, errorType string)
RecordError 记录错误
func (*AgentMetrics) RecordRequest ¶
func (m *AgentMetrics) RecordRequest(agentID string, duration time.Duration)
RecordRequest 记录请求
func (*AgentMetrics) RecordTokens ¶
func (m *AgentMetrics) RecordTokens(agentID string, inputTokens, outputTokens int64)
RecordTokens 记录 token 使用
func (*AgentMetrics) RecordToolCall ¶
func (m *AgentMetrics) RecordToolCall(agentID, toolName string, duration time.Duration, success bool)
RecordToolCall 记录工具调用
func (*AgentMetrics) SetActiveAgents ¶
func (m *AgentMetrics) SetActiveAgents(count int)
SetActiveAgents 设置活跃 Agent 数量
func (*AgentMetrics) SetQueueDepth ¶
func (m *AgentMetrics) SetQueueDepth(agentID string, depth int)
SetQueueDepth 设置队列深度
type CounterSnapshot ¶
CounterSnapshot 计数器快照
type GaugeSnapshot ¶
GaugeSnapshot 仪表盘快照
type HistogramSnapshot ¶
type HistogramSnapshot struct {
Name string
Count int64
Sum float64
Min float64
Max float64
Mean float64
Labels map[string]string
}
HistogramSnapshot 直方图快照
type Metrics ¶
type Metrics interface {
// Counter 操作
IncrementCounter(name string, value int64, labels map[string]string)
// Gauge 操作
SetGauge(name string, value float64, labels map[string]string)
// Histogram 操作
RecordHistogram(name string, value float64, labels map[string]string)
// 获取指标快照
Snapshot() MetricsSnapshot
}
Metrics 提供指标收集能力 参考 Google ADK-Go 的 metrics 设计
type MetricsSnapshot ¶
type MetricsSnapshot struct {
Counters map[string]*CounterSnapshot
Gauges map[string]*GaugeSnapshot
Histograms map[string]*HistogramSnapshot
Timestamp time.Time
}
MetricsSnapshot 指标快照
type NoopSpan ¶
type NoopSpan struct{}
NoopSpan 空实现的 span
func (*NoopSpan) RecordError ¶
func (*NoopSpan) SetAttributes ¶
func (*NoopSpan) SetStatus ¶
func (s *NoopSpan) SetStatus(code StatusCode, description string)
func (*NoopSpan) SpanContext ¶
func (s *NoopSpan) SpanContext() SpanContext
type NoopSpanContext ¶
type NoopSpanContext struct{}
NoopSpanContext 空实现的 span context
func (*NoopSpanContext) IsSampled ¶
func (c *NoopSpanContext) IsSampled() bool
func (*NoopSpanContext) SpanID ¶
func (c *NoopSpanContext) SpanID() string
func (*NoopSpanContext) TraceID ¶
func (c *NoopSpanContext) TraceID() string
type NoopTracer ¶
type NoopTracer struct{}
NoopTracer 空实现的 tracer,用于禁用追踪
func (*NoopTracer) StartSpan ¶
func (t *NoopTracer) StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
type OTelOption ¶
type OTelOption func(*otelConfig)
OTelOption 配置 OpenTelemetry tracer 的选项
func WithOTelAttributes ¶
func WithOTelAttributes(attrs ...attribute.KeyValue) OTelOption
WithOTelAttributes 设置额外的 resource 属性
func WithOTelExporter ¶
func WithOTelExporter(exporter sdktrace.SpanExporter) OTelOption
WithOTelExporter 设置 trace exporter
func WithOTelSampler ¶
func WithOTelSampler(sampler sdktrace.Sampler) OTelOption
WithOTelSampler 设置采样器
func WithOTelServiceVersion ¶
func WithOTelServiceVersion(version string) OTelOption
WithOTelServiceVersion 设置服务版本
func WithOTelSpanProcessor ¶
func WithOTelSpanProcessor(processor sdktrace.SpanProcessor) OTelOption
WithOTelSpanProcessor 添加自定义 span processor
type OTelSpan ¶
type OTelSpan struct {
// contains filtered or unexported fields
}
OTelSpan 适配 Span 接口
func (*OTelSpan) SetAttributes ¶
SetAttributes 实现 Span 接口
func (*OTelSpan) SetStatus ¶
func (s *OTelSpan) SetStatus(code StatusCode, description string)
SetStatus 实现 Span 接口
type OTelSpanContext ¶
type OTelSpanContext struct {
// contains filtered or unexported fields
}
OTelSpanContext 适配 SpanContext 接口
func (*OTelSpanContext) IsSampled ¶
func (c *OTelSpanContext) IsSampled() bool
IsSampled 实现 SpanContext 接口
func (*OTelSpanContext) TraceID ¶
func (c *OTelSpanContext) TraceID() string
TraceID 实现 SpanContext 接口
type OTelTracer ¶
type OTelTracer struct {
// contains filtered or unexported fields
}
OTelTracer 实现 Tracer 接口,适配 OpenTelemetry 参考 Google ADK-Go 的 telemetry/telemetry.go 实现
使用示例:
// 1. 创建 OTel tracer
tracer, err := telemetry.NewOTelTracer("my-agent-service",
telemetry.WithOTelExporter(exporter),
)
// 2. 设置为全局 tracer
telemetry.SetGlobalTracer(tracer)
// 3. 在代码中使用
ctx, span := tracer.StartSpan(ctx, "agent.chat")
defer span.End()
func NewOTelTracer ¶
func NewOTelTracer(serviceName string, opts ...OTelOption) (*OTelTracer, error)
NewOTelTracer 创建 OpenTelemetry 追踪器
func (*OTelTracer) ForceFlush ¶
func (t *OTelTracer) ForceFlush(ctx context.Context) error
ForceFlush 强制刷新所有待处理的 spans
func (*OTelTracer) Inject ¶
func (t *OTelTracer) Inject(ctx context.Context, carrier any) error
Inject 将 trace context 注入到 carrier
func (*OTelTracer) Shutdown ¶
func (t *OTelTracer) Shutdown(ctx context.Context) error
Shutdown 关闭 tracer,刷新所有待处理的 spans
func (*OTelTracer) StartSpan ¶
func (t *OTelTracer) StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
StartSpan 实现 Tracer 接口
type SimpleMetrics ¶
type SimpleMetrics struct {
// contains filtered or unexported fields
}
SimpleMetrics 简单的内存 metrics 实现
func (*SimpleMetrics) IncrementCounter ¶
func (m *SimpleMetrics) IncrementCounter(name string, value int64, labels map[string]string)
func (*SimpleMetrics) RecordHistogram ¶
func (m *SimpleMetrics) RecordHistogram(name string, value float64, labels map[string]string)
func (*SimpleMetrics) SetGauge ¶
func (m *SimpleMetrics) SetGauge(name string, value float64, labels map[string]string)
func (*SimpleMetrics) Snapshot ¶
func (m *SimpleMetrics) Snapshot() MetricsSnapshot
type SimpleSpan ¶
type SimpleSpan struct {
// contains filtered or unexported fields
}
SimpleSpan 简单的 span 实现
func (*SimpleSpan) AddEvent ¶
func (s *SimpleSpan) AddEvent(name string, attrs ...Attribute)
func (*SimpleSpan) Attributes ¶
func (s *SimpleSpan) Attributes() []Attribute
func (*SimpleSpan) Duration ¶
func (s *SimpleSpan) Duration() time.Duration
func (*SimpleSpan) End ¶
func (s *SimpleSpan) End()
func (*SimpleSpan) EndTime ¶
func (s *SimpleSpan) EndTime() time.Time
func (*SimpleSpan) Error ¶
func (s *SimpleSpan) Error() error
func (*SimpleSpan) Events ¶
func (s *SimpleSpan) Events() []SpanEvent
func (*SimpleSpan) RecordError ¶
func (s *SimpleSpan) RecordError(err error)
func (*SimpleSpan) SetAttributes ¶
func (s *SimpleSpan) SetAttributes(attrs ...Attribute)
func (*SimpleSpan) SetStatus ¶
func (s *SimpleSpan) SetStatus(code StatusCode, description string)
func (*SimpleSpan) SpanContext ¶
func (s *SimpleSpan) SpanContext() SpanContext
func (*SimpleSpan) StartTime ¶
func (s *SimpleSpan) StartTime() time.Time
func (*SimpleSpan) Status ¶
func (s *SimpleSpan) Status() StatusCode
func (*SimpleSpan) StatusDescription ¶
func (s *SimpleSpan) StatusDescription() string
type SimpleSpanContext ¶
type SimpleSpanContext struct {
// contains filtered or unexported fields
}
SimpleSpanContext 简单的 span context 实现
func (*SimpleSpanContext) IsSampled ¶
func (c *SimpleSpanContext) IsSampled() bool
func (*SimpleSpanContext) SpanID ¶
func (c *SimpleSpanContext) SpanID() string
func (*SimpleSpanContext) TraceID ¶
func (c *SimpleSpanContext) TraceID() string
type SimpleTracer ¶
type SimpleTracer struct {
// contains filtered or unexported fields
}
SimpleTracer 简单的内存 tracer 实现 用于开发和测试环境
func NewSimpleTracer ¶
func NewSimpleTracer() *SimpleTracer
func (*SimpleTracer) GetSpans ¶
func (t *SimpleTracer) GetSpans() []*SimpleSpan
GetSpans 获取所有 span (用于测试)
func (*SimpleTracer) StartSpan ¶
func (t *SimpleTracer) StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
type Span ¶
type Span interface {
// End 结束 span
End()
// SetAttributes 设置属性
SetAttributes(attrs ...Attribute)
// SetStatus 设置状态
SetStatus(code StatusCode, description string)
// AddEvent 添加事件
AddEvent(name string, attrs ...Attribute)
// RecordError 记录错误
RecordError(err error)
// SpanContext 返回 span context
SpanContext() SpanContext
}
Span 表示一个追踪片段
type SpanConfig ¶
SpanConfig span 配置
type SpanContext ¶
SpanContext 表示 span 的上下文信息
type StatusCode ¶
type StatusCode int
StatusCode 表示 span 的状态码
const ( StatusCodeUnset StatusCode = iota StatusCodeOK StatusCodeError )
type Tracer ¶
type Tracer interface {
// StartSpan 开始一个新的 span
StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
// Extract 从 carrier 中提取 trace context
Extract(ctx context.Context, carrier any) context.Context
// Inject 将 trace context 注入到 carrier
Inject(ctx context.Context, carrier any) error
}
Tracer 提供分布式追踪能力 参考 Google ADK-Go 的 OpenTelemetry 集成