telemetry

package
v0.5.2 Latest Latest
Warning

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

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

Documentation

Overview

Package telemetry provides OpenTelemetry instrumentation for the registry server. It supports configurable tracing and metrics with OTLP exporters.

Package telemetry provides OpenTelemetry instrumentation for the registry server.

Package telemetry provides OpenTelemetry instrumentation for the registry server.

Package telemetry provides OpenTelemetry instrumentation for the registry server.

Index

Constants

View Source
const (
	// DefaultServiceName is the default service name for telemetry
	DefaultServiceName = "thv-registry-api"

	// DefaultEndpoint is the default OTLP endpoint for telemetry
	DefaultEndpoint = "localhost:4318"

	// DefaultSampling is the default trace sampling rate (5%)
	// This provides a reasonable balance between observability and overhead
	DefaultSampling = 0.05
)
View Source
const (
	// RegistryMetricsMeterName is the name used for the registry metrics meter
	RegistryMetricsMeterName = "github.com/stacklok/toolhive-registry-server/registry"

	// SyncMetricsMeterName is the name used for the sync metrics meter
	SyncMetricsMeterName = "github.com/stacklok/toolhive-registry-server/sync"
)
View Source
const (
	// TracerName is the name used for the HTTP tracer
	TracerName = "github.com/stacklok/toolhive-registry-server/http"

	// MaxUserAgentLength is the maximum length for User-Agent strings in traces
	// to prevent unbounded storage from malicious or overly long User-Agent headers
	MaxUserAgentLength = 256
)
View Source
const (
	// DefaultMetricsInterval is the default interval for metric collection
	DefaultMetricsInterval = 60 * time.Second
)
View Source
const (
	// HTTPMetricsMeterName is the name used for the HTTP metrics meter
	HTTPMetricsMeterName = "github.com/stacklok/toolhive-registry-server/http"
)

Variables

This section is empty.

Functions

func MetricsMiddleware

func MetricsMiddleware(provider metric.MeterProvider) (func(http.Handler) http.Handler, error)

MetricsMiddleware creates middleware from a MeterProvider for convenience. This is a helper function that combines NewHTTPMetrics and Middleware.

func NewMeterProvider

func NewMeterProvider(ctx context.Context, opts ...MeterProviderOption) (metric.MeterProvider, error)

NewMeterProvider creates a new OpenTelemetry MeterProvider based on the configuration. Returns a no-op provider if metrics are disabled or configuration is nil. The caller is responsible for calling Shutdown on the returned provider.

func NewTracerProvider

func NewTracerProvider(ctx context.Context, opts ...TracerProviderOption) (trace.TracerProvider, error)

NewTracerProvider creates a new OpenTelemetry TracerProvider based on the configuration. Returns a no-op provider if tracing is disabled or configuration is nil. The caller is responsible for calling Shutdown on the returned provider.

func TracingMiddleware added in v0.5.2

func TracingMiddleware(provider trace.TracerProvider) func(http.Handler) http.Handler

TracingMiddleware creates HTTP middleware for distributed tracing. If provider is nil, it returns a pass-through middleware that does nothing.

Types

type Config

type Config struct {
	// Enabled controls whether telemetry is enabled globally
	// When false, no telemetry providers are initialized
	Enabled bool `yaml:"enabled"`

	// ServiceName is the name of the service for telemetry identification
	// Defaults to "thv-registry-api" if not specified
	ServiceName string `yaml:"serviceName,omitempty"`

	// ServiceVersion is the version of the service for telemetry identification
	// Defaults to the application version if not specified
	ServiceVersion string `yaml:"serviceVersion,omitempty"`

	// Endpoint is the OTLP collector endpoint for telemetry
	// Defaults to "localhost:4318" if not specified
	// Format: "host:port" for HTTP (uses /v1/traces and /v1/metrics paths automatically)
	Endpoint string `yaml:"endpoint,omitempty"`

	// Insecure allows HTTP connections instead of HTTPS
	// Should only be true for development/testing environments
	Insecure bool `yaml:"insecure,omitempty"`

	// Tracing contains tracing-specific configuration
	Tracing *TracingConfig `yaml:"tracing,omitempty"`

	// Metrics contains metrics-specific configuration
	Metrics *MetricsConfig `yaml:"metrics,omitempty"`
}

Config represents the root telemetry configuration

func (*Config) GetEndpoint

func (c *Config) GetEndpoint() string

GetEndpoint returns the endpoint, using default if not specified

func (*Config) GetInsecure

func (c *Config) GetInsecure() bool

GetInsecure returns the insecure flag

func (*Config) GetServiceName

func (c *Config) GetServiceName() string

GetServiceName returns the service name, using default if not specified

func (*Config) GetServiceVersion

func (c *Config) GetServiceVersion() string

GetServiceVersion returns the service version, using "unknown" if not specified

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the telemetry configuration

type HTTPMetrics

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

HTTPMetrics holds the OpenTelemetry instruments for HTTP metrics

func NewHTTPMetrics

func NewHTTPMetrics(provider metric.MeterProvider) (*HTTPMetrics, error)

NewHTTPMetrics creates a new HTTPMetrics instance with the given meter provider. If provider is nil, it returns nil (no-op metrics).

func (*HTTPMetrics) Middleware

func (m *HTTPMetrics) Middleware(next http.Handler) http.Handler

Middleware returns an HTTP middleware that records metrics for each request. If HTTPMetrics is nil, it returns a pass-through middleware.

type MeterProviderOption

type MeterProviderOption func(*meterProviderConfig)

MeterProviderOption is a function that configures the meter provider setup

func WithMeterEndpoint

func WithMeterEndpoint(endpoint string) MeterProviderOption

WithMeterEndpoint sets the endpoint for the meter provider

func WithMeterInsecure

func WithMeterInsecure(insecure bool) MeterProviderOption

WithMeterInsecure sets the insecure flag for the meter provider

func WithMeterServiceName

func WithMeterServiceName(name string) MeterProviderOption

WithMeterServiceName sets the service name for the meter provider

func WithMeterServiceVersion

func WithMeterServiceVersion(version string) MeterProviderOption

WithMeterServiceVersion sets the service version for the meter provider

func WithMetricsConfig

func WithMetricsConfig(mc *MetricsConfig) MeterProviderOption

WithMetricsConfig sets the metrics configuration

type MetricsConfig

type MetricsConfig struct {
	// Enabled controls whether metrics collection is enabled
	// When false, metrics are disabled even if telemetry is enabled globally
	Enabled bool `yaml:"enabled"`
}

MetricsConfig defines metrics-specific configuration

func (*MetricsConfig) Validate

func (c *MetricsConfig) Validate() error

Validate validates the metrics configuration

type Option

type Option func(*telemetryConfig)

Option is a function that configures the telemetry setup

func WithTelemetryConfig

func WithTelemetryConfig(cfg *Config) Option

WithTelemetryConfig sets the telemetry configuration

type RegistryMetrics

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

RegistryMetrics holds the OpenTelemetry instruments for registry metrics

func NewRegistryMetrics

func NewRegistryMetrics(provider metric.MeterProvider) (*RegistryMetrics, error)

NewRegistryMetrics creates a new RegistryMetrics instance with the given meter provider. If provider is nil, it returns nil (no-op metrics).

func (*RegistryMetrics) RecordServersTotal

func (m *RegistryMetrics) RecordServersTotal(ctx context.Context, registryName string, count int64)

RecordServersTotal records the current number of servers in a registry

type SyncMetrics

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

SyncMetrics holds the OpenTelemetry instruments for sync operation metrics

func NewSyncMetrics

func NewSyncMetrics(provider metric.MeterProvider) (*SyncMetrics, error)

NewSyncMetrics creates a new SyncMetrics instance with the given meter provider. If provider is nil, it returns nil (no-op metrics).

func (*SyncMetrics) RecordSyncDuration

func (m *SyncMetrics) RecordSyncDuration(ctx context.Context, registryName string, duration time.Duration, success bool)

RecordSyncDuration records the duration of a sync operation for a registry

type Telemetry

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

Telemetry encapsulates OpenTelemetry providers and handles their lifecycle. It provides a unified interface for initializing and shutting down telemetry.

func New

func New(ctx context.Context, opts ...Option) (*Telemetry, error)

New creates and initializes a new Telemetry instance based on the configuration. If telemetry is disabled or configuration is nil, returns a Telemetry with no-op providers. The caller is responsible for calling Shutdown when the application exits.

func (*Telemetry) Meter

func (t *Telemetry) Meter(name string, opts ...metric.MeterOption) metric.Meter

Meter returns a named meter from the meter provider

func (*Telemetry) MeterProvider

func (t *Telemetry) MeterProvider() metric.MeterProvider

MeterProvider returns the configured meter provider

func (*Telemetry) Shutdown

func (t *Telemetry) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down all telemetry providers. It should be called when the application is shutting down to flush any pending telemetry data. This method is safe to call multiple times.

func (*Telemetry) Tracer

func (t *Telemetry) Tracer(name string, opts ...trace.TracerOption) trace.Tracer

Tracer returns a named tracer from the tracer provider

func (*Telemetry) TracerProvider

func (t *Telemetry) TracerProvider() trace.TracerProvider

TracerProvider returns the configured tracer provider

type TracerProviderOption

type TracerProviderOption func(*tracerProviderConfig)

TracerProviderOption is a function that configures the tracer provider setup

func WithTracerEndpoint

func WithTracerEndpoint(endpoint string) TracerProviderOption

WithTracerEndpoint sets the endpoint for the tracer provider

func WithTracerInsecure

func WithTracerInsecure(insecure bool) TracerProviderOption

WithTracerInsecure sets the insecure flag for the tracer provider

func WithTracerServiceName

func WithTracerServiceName(name string) TracerProviderOption

WithTracerServiceName sets the service name for the tracer provider

func WithTracerServiceVersion

func WithTracerServiceVersion(version string) TracerProviderOption

WithTracerServiceVersion sets the service version for the tracer provider

func WithTracingConfig

func WithTracingConfig(tc *TracingConfig) TracerProviderOption

WithTracingConfig sets the tracing configuration

type TracingConfig

type TracingConfig struct {
	// Enabled controls whether tracing is enabled
	// When false, tracing is disabled even if telemetry is enabled globally
	Enabled bool `yaml:"enabled"`

	// Sampling controls the trace sampling rate (0.0 to 1.0, exclusive of 0.0)
	// 1.0 means sample all traces, 0.5 means sample 50%, etc.
	// Defaults to DefaultSampling if not specified (nil)
	// Using a pointer allows distinguishing between "not set" and "explicitly set to 0"
	// Setting sampling to 0 when tracing is enabled is an error (use Enabled: false instead)
	Sampling *float64 `yaml:"sampling,omitempty"`
}

TracingConfig defines tracing-specific configuration

func (*TracingConfig) GetSampling

func (c *TracingConfig) GetSampling() float64

GetSampling returns the sampling ratio. If Sampling is nil (not specified), it returns DefaultSampling. Validation should be performed before calling this method.

func (*TracingConfig) Validate

func (c *TracingConfig) Validate() error

Validate validates the tracing configuration

Jump to

Keyboard shortcuts

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