observability

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package observability provides OpenTelemetry instrumentation for OmniProxy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler) error

ListenAndServe starts an HTTP server on the given address.

func NewHealthMux

func NewHealthMux(health *HealthChecker, provider *Provider) *http.ServeMux

NewHealthMux creates an http.ServeMux with health and metrics endpoints.

Types

type BackendMetrics

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

BackendMetrics adapts the observability.Metrics to the backend.Metrics interface.

func NewBackendMetrics

func NewBackendMetrics(m *Metrics) *BackendMetrics

NewBackendMetrics creates a backend.Metrics adapter.

func (*BackendMetrics) IncCacheHit

func (b *BackendMetrics) IncCacheHit()

IncCacheHit increments cache hit counter.

func (*BackendMetrics) IncCacheMiss

func (b *BackendMetrics) IncCacheMiss()

IncCacheMiss increments cache miss counter.

func (*BackendMetrics) IncStoreError

func (b *BackendMetrics) IncStoreError()

IncStoreError increments store error counter.

func (*BackendMetrics) IncStoreSuccess

func (b *BackendMetrics) IncStoreSuccess()

IncStoreSuccess increments successful store counter.

func (*BackendMetrics) ObserveStoreDuration

func (b *BackendMetrics) ObserveStoreDuration(d time.Duration)

ObserveStoreDuration records store operation duration. Note: Currently not implemented in metrics, could add if needed.

func (*BackendMetrics) SetQueueDepth

func (b *BackendMetrics) SetQueueDepth(n int)

SetQueueDepth sets the current queue depth gauge. Note: Queue depth is handled via callback in the main metrics.

type CommonChecks

type CommonChecks struct{}

CommonChecks provides factory functions for common health checks.

func (CommonChecks) DatabaseCheck

func (CommonChecks) DatabaseCheck(pingFunc func() error) HealthCheck

DatabaseCheck returns a health check for database connectivity.

func (CommonChecks) KafkaCheck

func (CommonChecks) KafkaCheck(pingFunc func() error) HealthCheck

KafkaCheck returns a health check for Kafka connectivity.

func (CommonChecks) RedisCheck

func (CommonChecks) RedisCheck(pingFunc func() error) HealthCheck

RedisCheck returns a health check for Redis connectivity.

type Config

type Config struct {
	// ServiceName is the name of the service for telemetry.
	ServiceName string

	// ServiceVersion is the version of the service.
	ServiceVersion string

	// EnablePrometheus enables the Prometheus metrics exporter.
	EnablePrometheus bool
}

Config configures the observability provider.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default configuration.

type HealthCheck

type HealthCheck func() error

HealthCheck is a function that returns nil if healthy, error if not.

type HealthChecker

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

HealthChecker manages health check state and endpoints.

func NewHealthChecker

func NewHealthChecker() *HealthChecker

NewHealthChecker creates a new health checker.

func (*HealthChecker) Handler

func (h *HealthChecker) Handler() http.Handler

Handler returns an http.Handler that serves both health endpoints. Routes:

  • /healthz - liveness probe
  • /readyz - readiness probe
  • /health - alias for /healthz

func (*HealthChecker) IsReady

func (h *HealthChecker) IsReady() bool

IsReady returns whether the service is ready.

func (*HealthChecker) LivenessHandler

func (h *HealthChecker) LivenessHandler() http.Handler

LivenessHandler returns an http.Handler for the /healthz endpoint. Returns 200 if the process is alive, used by Kubernetes liveness probe.

func (*HealthChecker) ReadinessHandler

func (h *HealthChecker) ReadinessHandler() http.Handler

ReadinessHandler returns an http.Handler for the /readyz endpoint. Returns 200 if ready to receive traffic, 503 if not. Used by Kubernetes readiness probe and load balancers.

func (*HealthChecker) RegisterCheck

func (h *HealthChecker) RegisterCheck(name string, check HealthCheck)

RegisterCheck registers a named health check.

func (*HealthChecker) SetReady

func (h *HealthChecker) SetReady(ready bool)

SetReady marks the service as ready to receive traffic.

type HealthStatus

type HealthStatus struct {
	Status    string            `json:"status"`
	Timestamp string            `json:"timestamp"`
	Uptime    string            `json:"uptime,omitempty"`
	Checks    map[string]string `json:"checks,omitempty"`
}

HealthStatus represents the health status response.

type Metrics

type Metrics struct {
	// Request metrics
	RequestsTotal   metric.Int64Counter
	RequestDuration metric.Float64Histogram
	ActiveRequests  metric.Int64UpDownCounter

	// Response metrics
	ResponseSize metric.Int64Histogram

	// Certificate metrics
	CertsGenerated metric.Int64Counter
	CertsCacheHits metric.Int64Counter
	CertsCacheMiss metric.Int64Counter

	// Traffic store metrics
	TrafficStored      metric.Int64Counter
	TrafficStoreErrors metric.Int64Counter
	TrafficQueueDepth  metric.Int64ObservableGauge

	// Connection metrics
	ActiveConnections metric.Int64UpDownCounter
	// contains filtered or unexported fields
}

Metrics holds all OmniProxy metrics.

func NewMetrics

func NewMetrics(meterProvider metric.MeterProvider) (*Metrics, error)

NewMetrics creates a new Metrics instance with all instruments registered.

func (*Metrics) ConnectionClosed

func (m *Metrics) ConnectionClosed(ctx context.Context)

ConnectionClosed should be called when a connection is closed.

func (*Metrics) ConnectionOpened

func (m *Metrics) ConnectionOpened(ctx context.Context)

ConnectionOpened should be called when a connection is opened.

func (*Metrics) MetricsMiddleware

func (m *Metrics) MetricsMiddleware(next http.Handler) http.Handler

MetricsMiddleware wraps an http.Handler with metrics collection.

func (*Metrics) RecordCertCacheHit

func (m *Metrics) RecordCertCacheHit(ctx context.Context)

RecordCertCacheHit records a certificate cache hit.

func (*Metrics) RecordCertCacheMiss

func (m *Metrics) RecordCertCacheMiss(ctx context.Context)

RecordCertCacheMiss records a certificate cache miss.

func (*Metrics) RecordCertGenerated

func (m *Metrics) RecordCertGenerated(ctx context.Context, host string)

RecordCertGenerated records a certificate generation.

func (*Metrics) RecordRequest

func (m *Metrics) RecordRequest(ctx context.Context, method, host string, statusCode int, duration time.Duration, responseSize int64)

RecordRequest records metrics for a completed request.

func (*Metrics) RecordTrafficStoreError

func (m *Metrics) RecordTrafficStoreError(ctx context.Context)

RecordTrafficStoreError records a traffic store error.

func (*Metrics) RecordTrafficStored

func (m *Metrics) RecordTrafficStored(ctx context.Context)

RecordTrafficStored records a successful traffic store.

func (*Metrics) RegisterQueueDepthCallback

func (m *Metrics) RegisterQueueDepthCallback(meterProvider metric.MeterProvider, fn func() int64) error

RegisterQueueDepthCallback registers a callback to observe queue depth.

func (*Metrics) RequestEnd

func (m *Metrics) RequestEnd(ctx context.Context)

RequestEnd should be called when a request ends.

func (*Metrics) RequestStart

func (m *Metrics) RequestStart(ctx context.Context)

RequestStart should be called when a request starts.

type Provider

type Provider struct {
	MeterProvider *sdkmetric.MeterProvider
	Metrics       *Metrics
	// contains filtered or unexported fields
}

Provider holds the OpenTelemetry providers and exporters.

func NewProvider

func NewProvider(cfg *Config) (*Provider, error)

NewProvider creates a new observability provider.

func (*Provider) PrometheusHandler

func (p *Provider) PrometheusHandler() http.Handler

PrometheusHandler returns an http.Handler for the /metrics endpoint.

func (*Provider) Shutdown

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

Shutdown gracefully shuts down the provider.

Jump to

Keyboard shortcuts

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