observability

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package observability provides logging, metrics, and tracing functionality

This package implements observability features for GoRAG, including: - Structured logging (JSON format) - Metrics collection - Distributed tracing

Example:

// Create a JSON logger
logger := observability.NewJSONLogger()

// Log info message
logger.Info(ctx, "Document indexed", map[string]interface{}{
    "document": "example.pdf",
    "chunks": 42,
})

// Log error message
logger.Error(ctx, "Failed to index document", err, map[string]interface{}{
    "document": "example.pdf",
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONLogger

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

JSONLogger implements structured logging in JSON format

func NewJSONLogger

func NewJSONLogger() *JSONLogger

NewJSONLogger creates a new JSON logger

func (*JSONLogger) Debug

func (l *JSONLogger) Debug(ctx context.Context, message string, fields map[string]interface{})

Debug logs a debug message

func (*JSONLogger) Error

func (l *JSONLogger) Error(ctx context.Context, message string, err error, fields map[string]interface{})

Error logs an error message

func (*JSONLogger) Info

func (l *JSONLogger) Info(ctx context.Context, message string, fields map[string]interface{})

Info logs an info message

func (*JSONLogger) Warn

func (l *JSONLogger) Warn(ctx context.Context, message string, fields map[string]interface{})

Warn logs a warning message

type Logger

type Logger interface {
	// Info logs an info message
	Info(ctx context.Context, message string, fields map[string]interface{})
	// Error logs an error message
	Error(ctx context.Context, message string, err error, fields map[string]interface{})
	// Debug logs a debug message
	Debug(ctx context.Context, message string, fields map[string]interface{})
	// Warn logs a warning message
	Warn(ctx context.Context, message string, fields map[string]interface{})
}

Logger defines the interface for structured logging

type Metrics

type Metrics interface {
	// RecordQueryLatency records the latency of a query
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - duration: Duration of the query
	RecordQueryLatency(ctx context.Context, duration time.Duration)

	// RecordIndexLatency records the latency of an index operation
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - duration: Duration of the index operation
	RecordIndexLatency(ctx context.Context, duration time.Duration)

	// RecordQueryCount records the number of queries
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - status: Status of the query (e.g., "success", "error")
	RecordQueryCount(ctx context.Context, status string)

	// RecordIndexCount records the number of index operations
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - status: Status of the index operation (e.g., "success", "error")
	RecordIndexCount(ctx context.Context, status string)

	// RecordErrorCount records the number of errors
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - errorType: Type of error
	RecordErrorCount(ctx context.Context, errorType string)

	// RecordIndexedDocuments records the number of indexed documents
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - count: Number of indexed documents
	RecordIndexedDocuments(ctx context.Context, count int)

	// RecordIndexingDocuments records the number of documents being indexed
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - count: Number of documents being indexed
	RecordIndexingDocuments(ctx context.Context, count int)

	// RecordMonitoredDocuments records the number of monitored documents
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - count: Number of monitored documents
	RecordMonitoredDocuments(ctx context.Context, count int)

	// RecordSystemMetrics records system metrics (CPU, memory)
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - cpuUsage: CPU usage percentage
	// - memoryUsage: Memory usage in MB
	RecordSystemMetrics(ctx context.Context, cpuUsage float64, memoryUsage float64)

	// Register registers the metrics with the underlying system
	//
	// Returns:
	// - error: Error if registration fails
	Register() error
}

Metrics defines the interface for metrics collection

This interface defines methods for recording various metrics related to the RAG engine, including latencies, counts, and system metrics.

Example:

type CustomMetrics struct {
    // Custom metrics implementation
}

func (m *CustomMetrics) RecordQueryLatency(ctx context.Context, duration time.Duration) {
    // Record query latency
}

// Implement other methods...

type NoopSpan

type NoopSpan struct{}

NoopSpan implements a no-op span

This implementation provides a no-operation span that does nothing. It's used by the NoopTracer.

func (*NoopSpan) End

func (s *NoopSpan) End()

End ends the span

func (*NoopSpan) SetAttribute

func (s *NoopSpan) SetAttribute(key string, value interface{})

SetAttribute sets an attribute on the span

Parameters: - key: Attribute key - value: Attribute value

func (*NoopSpan) SetError

func (s *NoopSpan) SetError(err error)

SetError sets an error on the span

Parameters: - err: Error to set

type NoopTracer

type NoopTracer struct{}

func NewNoopTracer

func NewNoopTracer() *NoopTracer

NewNoopTracer creates a new no-op tracer

Returns: - *NoopTracer: New no-op tracer instance

func (*NoopTracer) Extract

func (t *NoopTracer) Extract(ctx context.Context) (Span, bool)

Extract extracts a span from a context

Parameters: - ctx: Context containing the span

Returns: - Span: No-op span - bool: False (no span found)

func (*NoopTracer) StartSpan

func (t *NoopTracer) StartSpan(ctx context.Context, name string) (context.Context, Span)

StartSpan starts a new span

Parameters: - ctx: Context for cancellation - name: Name of the span

Returns: - context.Context: Original context - Span: No-op span

type PrometheusMetrics

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

PrometheusMetrics implements metrics collection using Prometheus

This implementation uses Prometheus to collect and expose metrics about the RAG engine's performance and health.

Example:

metrics := NewPrometheusMetrics()
if err := metrics.Register(); err != nil {
    log.Fatal(err)
}

// Record a query latency
start := time.Now()
// Perform query...
metrics.RecordQueryLatency(ctx, time.Since(start))
metrics.RecordQueryCount(ctx, "success")

func NewPrometheusMetrics

func NewPrometheusMetrics() *PrometheusMetrics

NewPrometheusMetrics creates a new Prometheus metrics collector

Returns: - *PrometheusMetrics: New Prometheus metrics collector instance

func (*PrometheusMetrics) RecordErrorCount

func (m *PrometheusMetrics) RecordErrorCount(ctx context.Context, errorType string)

RecordErrorCount records the number of errors

Parameters: - ctx: Context for cancellation - errorType: Type of error

func (*PrometheusMetrics) RecordIndexCount

func (m *PrometheusMetrics) RecordIndexCount(ctx context.Context, status string)

RecordIndexCount records the number of index operations

Parameters: - ctx: Context for cancellation - status: Status of the index operation (e.g., "success", "error")

func (*PrometheusMetrics) RecordIndexLatency

func (m *PrometheusMetrics) RecordIndexLatency(ctx context.Context, duration time.Duration)

RecordIndexLatency records the latency of an index operation

Parameters: - ctx: Context for cancellation - duration: Duration of the index operation

func (*PrometheusMetrics) RecordIndexedDocuments added in v1.0.1

func (m *PrometheusMetrics) RecordIndexedDocuments(ctx context.Context, count int)

RecordIndexedDocuments records the number of indexed documents

Parameters: - ctx: Context for cancellation - count: Number of indexed documents

func (*PrometheusMetrics) RecordIndexingDocuments added in v1.0.1

func (m *PrometheusMetrics) RecordIndexingDocuments(ctx context.Context, count int)

RecordIndexingDocuments records the number of documents being indexed

Parameters: - ctx: Context for cancellation - count: Number of documents being indexed

func (*PrometheusMetrics) RecordMonitoredDocuments added in v1.0.1

func (m *PrometheusMetrics) RecordMonitoredDocuments(ctx context.Context, count int)

RecordMonitoredDocuments records the number of monitored documents

Parameters: - ctx: Context for cancellation - count: Number of monitored documents

func (*PrometheusMetrics) RecordQueryCount

func (m *PrometheusMetrics) RecordQueryCount(ctx context.Context, status string)

RecordQueryCount records the number of queries

Parameters: - ctx: Context for cancellation - status: Status of the query (e.g., "success", "error")

func (*PrometheusMetrics) RecordQueryLatency

func (m *PrometheusMetrics) RecordQueryLatency(ctx context.Context, duration time.Duration)

RecordQueryLatency records the latency of a query

Parameters: - ctx: Context for cancellation - duration: Duration of the query

func (*PrometheusMetrics) RecordSystemMetrics added in v1.0.1

func (m *PrometheusMetrics) RecordSystemMetrics(ctx context.Context, cpuUsage float64, memoryUsage float64)

RecordSystemMetrics records system metrics (CPU, memory)

Parameters: - ctx: Context for cancellation - cpuUsage: CPU usage percentage - memoryUsage: Memory usage in MB

func (*PrometheusMetrics) Register

func (m *PrometheusMetrics) Register() error

Register registers the metrics with Prometheus

Returns: - error: Error if registration fails

type Span

type Span interface {
	// End ends the span
	End()

	// SetAttribute sets an attribute on the span
	//
	// Parameters:
	// - key: Attribute key
	// - value: Attribute value
	SetAttribute(key string, value interface{})

	// SetError sets an error on the span
	//
	// Parameters:
	// - err: Error to set
	SetError(err error)
}

Span defines the interface for a tracing span

A span represents a single operation within a trace. It has a name, start time, end time, and attributes.

Example:

ctx, span := tracer.StartSpan(ctx, "query")
defer span.End()

span.SetAttribute("query", "What is GoRAG?")

// Perform operation...

if err != nil {
    span.SetError(err)
}

type SpanContext

type SpanContext struct {
	TraceID string // Trace ID
	SpanID  string // Span ID
}

SpanContext represents the context of a span

This struct contains the trace and span IDs for a span.

Example:

spanCtx := SpanContext{
    TraceID: "trace-123",
    SpanID:  "span-456",
}

type Tracer

type Tracer interface {
	// StartSpan starts a new span
	//
	// Parameters:
	// - ctx: Context for cancellation
	// - name: Name of the span
	//
	// Returns:
	// - context.Context: New context with the span
	// - Span: New span instance
	StartSpan(ctx context.Context, name string) (context.Context, Span)

	// Extract extracts a span from a context
	//
	// Parameters:
	// - ctx: Context containing the span
	//
	// Returns:
	// - Span: Extracted span
	// - bool: True if a span was found
	Extract(ctx context.Context) (Span, bool)
}

Tracer defines the interface for distributed tracing

This interface defines methods for creating and managing distributed traces. Tracing allows you to track the flow of requests through the system and identify performance bottlenecks.

Example:

type CustomTracer struct {
    // Custom tracer implementation
}

func (t *CustomTracer) StartSpan(ctx context.Context, name string) (context.Context, Span) {
    // Start a new span
}

func (t *CustomTracer) Extract(ctx context.Context) (Span, bool) {
    // Extract span from context
}

Jump to

Keyboard shortcuts

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