observability

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 13 Imported by: 0

README

Observability (可观测性:监控与链路追踪)

pkg/observability 为 GoRAG 提供了工业级的监控指标(Metrics)和分布式链路追踪(Tracing)支持。

为什么可观测性对 RAG 至关重要?

RAG 系统通常涉及多个外部调用(如向量数据库、LLM API)。当用户反馈“回答太慢”或“回答不对”时,如果没有可观测性,排查将极其困难。

  • Metrics 告诉你系统哪里慢、调用了多少次 Token。
  • Tracing 告诉你具体的请求在哪个组件发生了延迟或错误。

1. 监控指标 (Metrics)

核心接口:core.Metrics

我们提供了一个符合 Cloud Native 标准的 Prometheus 实现。

导出指标清单
指标名称 类型 描述 标签 (Labels)
gorag_search_duration_seconds Histogram 向量搜索耗时分布 engine
gorag_indexing_duration_seconds Histogram 文档解析与入库耗时 parser
gorag_embedding_count Counter 累计生成的向量/Token 数量 -
gorag_search_error_count Counter 搜索失败次数 engine
如何使用
// 启动一个指标服务器在 :8080/metrics 供 Prometheus 抓取
metrics := observability.DefaultPrometheusMetrics(":8080")

2. 链路追踪 (Tracing)

核心接口:Tracer & Span

我们提供了一个符合工业标准 OpenTelemetry (OTel) 的实现。

功能特点
  • 分布式上下文传递:支持将 Trace ID 从业务上层传递到最底层的向量数据库调用。
  • 导出器支持:支持导出到 Jaeger, Zipkin, Honeycomb 或任何 OTel 兼容的后端。
  • 自动语义映射:将 RAG 内部的“分块”、“召回”、“重排序”自动映射为 Trace 中的不同阶段。
如何使用
// 初始化 OTel 追踪并导出到本地 Jaeger 代理
tracer, _ := observability.DefaultOpenTelemetryTracer(ctx, "localhost:4317", "MyRAGApp")

3. 在 GoRAG 中一站式集成

在工业实践中,建议通过 indexer 的 Functional Options 一键开启这些能力:

idx, _ := indexer.NewBuilder().
    // 开启 Prometheus 监控
    WithPrometheusMetrics(":8080").
    // 开启分布式追踪 (Jaeger/Zipkin)
    WithOpenTelemetryTracer(ctx, "jaeger-collector:4317", "gorag-service").
    Build()

注意:如果不进行任何配置,系统将默认使用 Noop 实现,确保在不需要观测的场景下保持零性能开销。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

type Collector interface {
	// RecordDuration records the duration of an operation.
	RecordDuration(operation string, duration time.Duration, labels map[string]string)

	// RecordCount records the count of an operation (success/failure).
	RecordCount(operation string, status string, labels map[string]string)

	// RecordValue records a custom metric value.
	RecordValue(metricName string, value float64, labels map[string]string)
}

Collector defines the interface for collecting metrics.

func DefaultNoopCollector added in v1.1.3

func DefaultNoopCollector() Collector

DefaultNoopCollector creates a no-op collector that discards all metrics.

type PrometheusMetrics added in v1.1.3

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

PrometheusMetrics implements core.Metrics using Prometheus (duck typed)

func DefaultPrometheusMetrics added in v1.1.3

func DefaultPrometheusMetrics(addr string) *PrometheusMetrics

DefaultPrometheusMetrics creates a new prometheus-based metrics collector and optionally starts an HTTP server for scraping at the given address if addr != "".

func (*PrometheusMetrics) RecordEmbeddingCount added in v1.1.3

func (m *PrometheusMetrics) RecordEmbeddingCount(count int)

func (*PrometheusMetrics) RecordIndexingDuration added in v1.1.3

func (m *PrometheusMetrics) RecordIndexingDuration(parser string, duration any)

func (*PrometheusMetrics) RecordIndexingResult added in v1.1.3

func (m *PrometheusMetrics) RecordIndexingResult(parser string, count int)

func (*PrometheusMetrics) RecordSearchDuration added in v1.1.3

func (m *PrometheusMetrics) RecordSearchDuration(engine string, duration any)

func (*PrometheusMetrics) RecordSearchError added in v1.1.3

func (m *PrometheusMetrics) RecordSearchError(engine string, err error)

func (*PrometheusMetrics) RecordSearchResult added in v1.1.3

func (m *PrometheusMetrics) RecordSearchResult(engine string, count int)

func (*PrometheusMetrics) RecordVectorStoreOperations added in v1.1.3

func (m *PrometheusMetrics) RecordVectorStoreOperations(op string, count int)

type Span

type Span interface {
	// SetTag sets a tag on the span.
	SetTag(key string, value interface{})

	// LogEvent logs an event within the span.
	LogEvent(eventName string, fields map[string]interface{})

	// End ends the span.
	End()
}

Span represents a single operation in a trace.

type Tracer

type Tracer interface {
	// StartSpan starts a new span/tracing context.
	StartSpan(ctx context.Context, operationName string) (context.Context, Span)

	// GetSpan retrieves the current span from context.
	GetSpan(ctx context.Context) Span
}

Tracer defines the interface for distributed tracing.

func DefaultNoopTracer added in v1.1.3

func DefaultNoopTracer() Tracer

DefaultNoopTracer creates a no-op tracer.

func DefaultOpenTelemetryTracer added in v1.1.3

func DefaultOpenTelemetryTracer(ctx context.Context, endpoint string, serviceName string) (Tracer, error)

DefaultOpenTelemetryTracer creates a new OTel tracer.

Jump to

Keyboard shortcuts

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