metrics

package
v0.0.62 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

type Collector interface {
	// RecordToolCall records metrics for an MCP tool call execution.
	RecordToolCall(ctx context.Context, name string, duration time.Duration, err error)

	// RecordHTTPRequest records metrics for an HTTP request.
	RecordHTTPRequest(ctx context.Context, method, path string, statusCode int, duration time.Duration)
}

Collector defines the interface for collecting metrics from various sources. Implementations can export metrics to different backends (OTel, Prometheus, in-memory stats, etc.).

type CollectorConfig

type CollectorConfig struct {
	MeterName      string
	ServiceName    string
	ServiceVersion string
	// Telemetry is the optional telemetry configuration.
	// If nil, env vars will be used for OTLP configuration.
	Telemetry *config.TelemetryConfig
}

CollectorConfig contains configuration for the OtelStatsCollector.

type Config

type Config struct {
	TracerName     string
	ServiceName    string
	ServiceVersion string
	// Telemetry is the optional telemetry configuration.
	// If nil, env vars will be used for OTLP configuration.
	Telemetry *config.TelemetryConfig
}

Config contains configuration for the metrics system.

type Metrics

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

Metrics coordinates multiple metric collectors. It implements the Collector interface and fans out calls to all registered collectors.

func New

func New(cfg Config) (*Metrics, error)

New creates a new Metrics instance with configured collectors. If OTEL_EXPORTER_OTLP_ENDPOINT is set, metrics will also be exported to OTLP.

func (*Metrics) GetStats

func (m *Metrics) GetStats() *Statistics

GetStats returns the current statistics from the StatsCollector. This is used by the /stats HTTP endpoint.

func (*Metrics) PrometheusHandler

func (m *Metrics) PrometheusHandler() http.Handler

PrometheusHandler returns the HTTP handler for the /metrics endpoint. This handler serves metrics in Prometheus text format

func (*Metrics) RecordHTTPRequest

func (m *Metrics) RecordHTTPRequest(ctx context.Context, method, path string, statusCode int, duration time.Duration)

RecordHTTPRequest implements the Collector interface. It fans out the call to all registered collectors.

func (*Metrics) RecordToolCall

func (m *Metrics) RecordToolCall(ctx context.Context, name string, duration time.Duration, err error)

RecordToolCall implements the Collector interface. It fans out the call to all registered collectors.

func (*Metrics) Shutdown

func (m *Metrics) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the metrics system, flushing any pending metrics.

type OtelStatsCollector

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

OtelStatsCollector collects metrics using OpenTelemetry SDK with ManualReader. It provides a simple in-memory stats collector for the /stats endpoint and a Prometheus exporter for the /metrics endpoint.

func NewOtelStatsCollector

func NewOtelStatsCollector(meterName string) (*OtelStatsCollector, error)

NewOtelStatsCollector creates a new OtelStatsCollector with ManualReader. If OTEL_EXPORTER_OTLP_ENDPOINT is set, metrics will also be exported to OTLP.

func NewOtelStatsCollectorWithConfig

func NewOtelStatsCollectorWithConfig(cfg CollectorConfig) (*OtelStatsCollector, error)

NewOtelStatsCollectorWithConfig creates a new OtelStatsCollector with full configuration.

func (*OtelStatsCollector) GetStats

func (c *OtelStatsCollector) GetStats() *Statistics

GetStats returns a snapshot of current statistics by reading from OTel metrics. Thread-safety is handled by the OTel SDK's ManualReader.

func (*OtelStatsCollector) PrometheusHandler

func (c *OtelStatsCollector) PrometheusHandler() http.Handler

PrometheusHandler returns the HTTP handler for the /metrics endpoint. This handler serves metrics in Prometheus text format.

func (*OtelStatsCollector) RecordHTTPRequest

func (c *OtelStatsCollector) RecordHTTPRequest(ctx context.Context, method, path string, statusCode int, duration time.Duration)

RecordHTTPRequest implements the Collector interface.

func (*OtelStatsCollector) RecordToolCall

func (c *OtelStatsCollector) RecordToolCall(ctx context.Context, name string, duration time.Duration, err error)

RecordToolCall implements the Collector interface.

func (*OtelStatsCollector) Shutdown

func (c *OtelStatsCollector) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the meter provider, flushing any pending metrics.

type Statistics

type Statistics struct {
	// Tool call metrics
	TotalToolCalls   int64            `json:"total_tool_calls"`
	ToolCallErrors   int64            `json:"tool_call_errors"`
	ToolCallsByName  map[string]int64 `json:"tool_calls_by_name"`
	ToolErrorsByName map[string]int64 `json:"tool_errors_by_name"`

	// HTTP request metrics
	TotalHTTPRequests    int64            `json:"total_http_requests"`
	HTTPRequestsByPath   map[string]int64 `json:"http_requests_by_path"`
	HTTPRequestsByStatus map[string]int64 `json:"http_requests_by_status"`
	HTTPRequestsByMethod map[string]int64 `json:"http_requests_by_method"`

	// Uptime
	UptimeSeconds int64 `json:"uptime_seconds"`
	StartTime     int64 `json:"start_time_unix"`
}

Statistics represents the aggregated metrics data exposed by the stats endpoint.

Jump to

Keyboard shortcuts

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