metrics

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package metrics provides OpenTelemetry metrics instrumentation with Prometheus export. Supports business operation metrics and HTTP request metrics for observability.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPMetricsMiddleware

func HTTPMetricsMiddleware(meterProvider metric.MeterProvider, namespace string) gin.HandlerFunc

HTTPMetricsMiddleware returns a Gin middleware that records HTTP request metrics. Tracks total requests and request durations with method, path, and status_code labels. The path is sanitized to route patterns (e.g., /v1/secrets/:path) to prevent high cardinality.

Types

type BusinessMetrics

type BusinessMetrics interface {
	// RecordOperation records a business operation with its status.
	// Domain examples: "auth", "secrets", "transit"
	// Operation examples: "client_create", "secret_get", "transit_encrypt"
	// Status examples: "success", "error"
	RecordOperation(ctx context.Context, domain, operation, status string)

	// RecordDuration records the duration of a business operation with its status.
	// Duration is recorded in seconds as a histogram for percentile calculations.
	RecordDuration(ctx context.Context, domain, operation string, duration time.Duration, status string)
}

BusinessMetrics defines the interface for recording business operation metrics. Implementations track operation counts and durations for observability across different business domains (auth, secrets, transit).

func NewBusinessMetrics

func NewBusinessMetrics(meterProvider metric.MeterProvider, namespace string) (BusinessMetrics, error)

NewBusinessMetrics creates a new BusinessMetrics implementation using the provided meter provider. The namespace parameter is used as a prefix for all metric names (e.g., "secrets"). Returns error if meters cannot be initialized.

func NewNoOpBusinessMetrics

func NewNoOpBusinessMetrics() BusinessMetrics

NewNoOpBusinessMetrics creates a no-op BusinessMetrics implementation.

type NoOpBusinessMetrics

type NoOpBusinessMetrics struct{}

NoOpBusinessMetrics is a no-op implementation of BusinessMetrics for when metrics are disabled.

func (*NoOpBusinessMetrics) RecordDuration

func (n *NoOpBusinessMetrics) RecordDuration(
	ctx context.Context,
	domain, operation string,
	duration time.Duration,
	status string,
)

RecordDuration does nothing when metrics are disabled.

func (*NoOpBusinessMetrics) RecordOperation

func (n *NoOpBusinessMetrics) RecordOperation(ctx context.Context, domain, operation, status string)

RecordOperation does nothing when metrics are disabled.

type Provider

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

Provider manages the OpenTelemetry meter provider and Prometheus exporter. Provides access to the HTTP handler for exposing metrics in Prometheus format.

func NewProvider

func NewProvider(namespace string) (*Provider, error)

NewProvider creates and initializes a new metrics provider with Prometheus exporter. The namespace parameter is used as a prefix for all metric names (e.g., "secrets"). Returns error if the Prometheus exporter cannot be initialized.

func (*Provider) Handler

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

Handler returns an HTTP handler that serves metrics in Prometheus exposition format. This handler should be exposed at the /metrics endpoint for Prometheus scraping.

func (*Provider) MeterProvider

func (p *Provider) MeterProvider() *metric.MeterProvider

MeterProvider returns the OpenTelemetry meter provider for creating meters. Use this to obtain a meter for recording metrics in different parts of the application.

func (*Provider) Shutdown

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

Shutdown performs cleanup of the metrics provider and flushes any pending metrics. Should be called during application shutdown to ensure all metrics are exported.

Jump to

Keyboard shortcuts

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