tracing

package
v0.1.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package tracing provides distributed tracing for the gridctl MCP gateway. It initializes the OpenTelemetry SDK, propagates W3C trace context, stores completed traces in an in-memory ring buffer, and optionally exports to OTLP.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer stores completed traces in a thread-safe ring buffer and implements sdktrace.SpanExporter so it can be registered with the OTel TracerProvider.

func NewBuffer

func NewBuffer(maxSize int, retention time.Duration) *Buffer

NewBuffer creates a trace ring buffer with the given capacity and retention.

func (*Buffer) Count

func (b *Buffer) Count() int

Count returns the number of traces currently stored.

func (*Buffer) ExportSpans

func (b *Buffer) ExportSpans(_ context.Context, spans []sdktrace.ReadOnlySpan) error

ExportSpans implements sdktrace.SpanExporter. Spans are accumulated by trace ID. When the local-root span is exported, all accumulated spans are finalised into a TraceRecord.

func (*Buffer) Filter

func (b *Buffer) Filter(opts FilterOpts) []TraceRecord

Filter returns traces matching the given options, newest first.

func (*Buffer) GetByID

func (b *Buffer) GetByID(traceID string) *TraceRecord

GetByID returns the trace with the given ID, or nil if not found.

func (*Buffer) GetRecent

func (b *Buffer) GetRecent(n int) []TraceRecord

GetRecent returns up to n most-recent traces, newest first.

func (*Buffer) Shutdown

func (b *Buffer) Shutdown(_ context.Context) error

Shutdown implements sdktrace.SpanExporter. Flushes any pending in-progress traces.

type Config

type Config struct {
	// Enabled controls whether tracing is active. Default: true.
	Enabled bool `yaml:"enabled"`

	// Sampling is the head-based sampling rate [0.0, 1.0]. Default: 1.0 (sample all).
	Sampling float64 `yaml:"sampling"`

	// Retention is how long completed traces are kept in the in-memory buffer.
	// Parsed as a Go duration string (e.g. "24h", "1h"). Default: "24h".
	Retention string `yaml:"retention"`

	// Export selects an exporter: "otlp" or "" (none). Default: "".
	Export string `yaml:"export,omitempty"`

	// Endpoint is the OTLP endpoint URL (e.g. "http://localhost:4318").
	// Required when Export is "otlp".
	Endpoint string `yaml:"endpoint,omitempty"`

	// MaxTraces is the ring buffer capacity. Default: 1000.
	MaxTraces int `yaml:"max_traces,omitempty"`
}

Config holds tracing configuration for the gateway.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults.

func (*Config) RetentionDuration

func (c *Config) RetentionDuration() time.Duration

RetentionDuration parses Retention as a time.Duration, returning the default (24h) on error.

type FilterOpts

type FilterOpts struct {
	ServerName  string
	ErrorsOnly  bool
	MinDuration time.Duration
	Limit       int
}

FilterOpts configures trace list filtering.

type MetaCarrier

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

MetaCarrier implements propagation.TextMapCarrier for MCP _meta maps. It enables standard OTel Extract()/Inject() calls on the JSON-RPC params._meta field, following MCP spec PR #414.

func NewMetaCarrier

func NewMetaCarrier(meta map[string]any) *MetaCarrier

NewMetaCarrier wraps an existing _meta map (or creates one if nil).

func (*MetaCarrier) Get

func (c *MetaCarrier) Get(key string) string

Get returns the value for a key, normalising to lowercase.

func (*MetaCarrier) Keys

func (c *MetaCarrier) Keys() []string

Keys returns all keys present in the _meta map.

func (*MetaCarrier) Map

func (c *MetaCarrier) Map() map[string]any

Map returns the underlying _meta map (e.g. to re-marshal into JSON).

func (*MetaCarrier) Set

func (c *MetaCarrier) Set(key, value string)

Set stores a key-value pair in the _meta map.

type Provider

type Provider struct {
	Buffer *Buffer
	// contains filtered or unexported fields
}

Provider wraps the OTel TracerProvider and the in-memory trace buffer. Call Init to configure and register the global OTel provider. Call Shutdown to flush and shut down cleanly.

func NewProvider

func NewProvider(cfg *Config) *Provider

NewProvider creates a Provider from config. Call Init to activate it.

func (*Provider) Init

func (p *Provider) Init(ctx context.Context) error

Init initialises the OTel SDK, registers the global TracerProvider and propagator, and sets up the in-memory buffer (+ optional OTLP exporter).

func (*Provider) SetLogger

func (p *Provider) SetLogger(logger *slog.Logger)

SetLogger sets the logger for tracing startup messages.

func (*Provider) Shutdown

func (p *Provider) Shutdown(ctx context.Context) error

Shutdown flushes pending spans and shuts down the TracerProvider.

type SpanRecord

type SpanRecord struct {
	TraceID    string            `json:"trace_id"`
	SpanID     string            `json:"span_id"`
	ParentID   string            `json:"parent_id,omitempty"`
	Name       string            `json:"name"`
	StartTime  time.Time         `json:"start_time"`
	EndTime    time.Time         `json:"end_time"`
	DurationMs int64             `json:"duration_ms"`
	Status     string            `json:"status"`
	IsError    bool              `json:"is_error"`
	Attrs      map[string]string `json:"attrs,omitempty"`
}

SpanRecord is a completed OTel span stored in the trace buffer.

type TraceRecord

type TraceRecord struct {
	TraceID    string       `json:"trace_id"`
	Operation  string       `json:"operation"` // root span name
	ServerName string       `json:"server_name,omitempty"`
	MethodName string       `json:"method_name,omitempty"`
	StartTime  time.Time    `json:"start_time"`
	EndTime    time.Time    `json:"end_time"`
	DurationMs int64        `json:"duration_ms"`
	SpanCount  int          `json:"span_count"`
	IsError    bool         `json:"is_error"`
	Spans      []SpanRecord `json:"spans"`
}

TraceRecord groups all spans belonging to a single trace.

Jump to

Keyboard shortcuts

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