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
- func MetricsMiddleware(provider metric.MeterProvider) (func(http.Handler) http.Handler, error)
- func NewMeterProvider(ctx context.Context, opts ...MeterProviderOption) (metric.MeterProvider, error)
- func NewTracerProvider(ctx context.Context, opts ...TracerProviderOption) (trace.TracerProvider, error)
- func TracingMiddleware(provider trace.TracerProvider) func(http.Handler) http.Handler
- type Config
- type HTTPMetrics
- type MeterProviderOption
- func WithMeterEndpoint(endpoint string) MeterProviderOption
- func WithMeterInsecure(insecure bool) MeterProviderOption
- func WithMeterServiceName(name string) MeterProviderOption
- func WithMeterServiceVersion(version string) MeterProviderOption
- func WithMetricsConfig(mc *MetricsConfig) MeterProviderOption
- type MetricsConfig
- type Option
- type RegistryMetrics
- type SyncMetrics
- type Telemetry
- func (t *Telemetry) Meter(name string, opts ...metric.MeterOption) metric.Meter
- func (t *Telemetry) MeterProvider() metric.MeterProvider
- func (t *Telemetry) Shutdown(ctx context.Context) error
- func (t *Telemetry) Tracer(name string, opts ...trace.TracerOption) trace.Tracer
- func (t *Telemetry) TracerProvider() trace.TracerProvider
- type TracerProviderOption
- func WithTracerEndpoint(endpoint string) TracerProviderOption
- func WithTracerInsecure(insecure bool) TracerProviderOption
- func WithTracerServiceName(name string) TracerProviderOption
- func WithTracerServiceVersion(version string) TracerProviderOption
- func WithTracingConfig(tc *TracingConfig) TracerProviderOption
- type TracingConfig
Constants ¶
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 )
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" )
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 )
const ( // DefaultMetricsInterval is the default interval for metric collection DefaultMetricsInterval = 60 * time.Second )
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 ¶
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
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 ¶
GetEndpoint returns the endpoint, using default if not specified
func (*Config) GetInsecure ¶
GetInsecure returns the insecure flag
func (*Config) GetServiceName ¶
GetServiceName returns the service name, using default if not specified
func (*Config) GetServiceVersion ¶
GetServiceVersion returns the service version, using "unknown" if not specified
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 ¶
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 ¶
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) MeterProvider ¶
func (t *Telemetry) MeterProvider() metric.MeterProvider
MeterProvider returns the configured meter provider
func (*Telemetry) Shutdown ¶
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) 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