metrics

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPMetricsMiddleware

func HTTPMetricsMiddleware(m Recorder) gin.HandlerFunc

HTTPMetricsMiddleware creates a Gin middleware that records HTTP metrics

Types

type CacheWrapper

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

CacheWrapper provides a read-through cache for metrics data. It queries the database on cache miss and updates the cache for subsequent requests. Uses the cache's GetWithFetch method for optimal cache-aside pattern support.

func NewCacheWrapper

func NewCacheWrapper(store *store.Store, cache cache.Cache) *CacheWrapper

NewCacheWrapper creates a new cache wrapper for metrics.

func (*CacheWrapper) GetActiveTokensCount

func (m *CacheWrapper) GetActiveTokensCount(
	ctx context.Context,
	category string,
	ttl time.Duration,
) (int64, error)

GetActiveTokensCount retrieves the count of active tokens by category. Uses cache-aside pattern via GetWithFetch for optimal performance.

func (*CacheWrapper) GetPendingDeviceCodesCount

func (m *CacheWrapper) GetPendingDeviceCodesCount(
	ctx context.Context,
	ttl time.Duration,
) (int64, error)

GetPendingDeviceCodesCount retrieves the count of pending (not yet authorized) device codes. Uses cache-aside pattern via GetWithFetch for optimal performance.

func (*CacheWrapper) GetTotalDeviceCodesCount

func (m *CacheWrapper) GetTotalDeviceCodesCount(
	ctx context.Context,
	ttl time.Duration,
) (int64, error)

GetTotalDeviceCodesCount retrieves the count of total (non-expired) device codes. Uses cache-aside pattern via GetWithFetch for optimal performance.

type Metrics

type Metrics struct {
	// OAuth Device Flow Metrics
	DeviceCodesTotal                *prometheus.CounterVec
	DeviceCodesAuthorizedTotal      prometheus.Counter
	DeviceCodeValidationTotal       *prometheus.CounterVec
	DeviceCodesActive               prometheus.Gauge
	DeviceCodesPendingAuthorization prometheus.Gauge
	DeviceCodeAuthorizationDuration prometheus.Histogram

	// Token Metrics
	TokensIssuedTotal       *prometheus.CounterVec
	TokensRevokedTotal      *prometheus.CounterVec
	TokensRefreshedTotal    *prometheus.CounterVec
	TokenValidationTotal    *prometheus.CounterVec
	TokensActive            *prometheus.GaugeVec
	TokenGenerationDuration *prometheus.HistogramVec
	TokenValidationDuration *prometheus.HistogramVec

	// Authentication Metrics
	AuthAttemptsTotal       *prometheus.CounterVec
	AuthLoginTotal          *prometheus.CounterVec
	AuthLogoutTotal         prometheus.Counter
	AuthOAuthCallbackTotal  *prometheus.CounterVec
	AuthLoginDuration       *prometheus.HistogramVec
	AuthExternalAPIDuration *prometheus.HistogramVec

	// Session Metrics
	SessionsActive           prometheus.Gauge
	SessionsCreatedTotal     prometheus.Counter
	SessionsExpiredTotal     *prometheus.CounterVec
	SessionsInvalidatedTotal *prometheus.CounterVec
	SessionDuration          prometheus.Histogram

	// HTTP Request Metrics
	HTTPRequestsTotal    *prometheus.CounterVec
	HTTPRequestDuration  *prometheus.HistogramVec
	HTTPRequestsInFlight prometheus.Gauge

	// Database Query Metrics
	DatabaseQueryErrorsTotal *prometheus.CounterVec
}

Metrics holds all Prometheus metrics for the application

func GetMetrics deprecated

func GetMetrics() *Metrics

GetMetrics returns the global metrics instance

Deprecated: Use Init(true) instead

func (*Metrics) RecordAuthAttempt

func (m *Metrics) RecordAuthAttempt(method string, success bool, duration time.Duration)

RecordAuthAttempt records authentication attempt

func (*Metrics) RecordDatabaseQueryError

func (m *Metrics) RecordDatabaseQueryError(operation string)

RecordDatabaseQueryError records a database query error during metric collection

func (*Metrics) RecordExternalAPICall

func (m *Metrics) RecordExternalAPICall(provider string, duration time.Duration)

RecordExternalAPICall records external API call duration

func (*Metrics) RecordLogin

func (m *Metrics) RecordLogin(authSource string, success bool)

RecordLogin records login attempt

func (*Metrics) RecordLogout

func (m *Metrics) RecordLogout(sessionDuration time.Duration)

RecordLogout records logout

func (*Metrics) RecordOAuthCallback

func (m *Metrics) RecordOAuthCallback(provider string, success bool)

RecordOAuthCallback records OAuth callback

func (*Metrics) RecordOAuthDeviceCodeAuthorized

func (m *Metrics) RecordOAuthDeviceCodeAuthorized(authorizationTime time.Duration)

RecordOAuthDeviceCodeAuthorized records device code authorization

func (*Metrics) RecordOAuthDeviceCodeGenerated

func (m *Metrics) RecordOAuthDeviceCodeGenerated(success bool)

RecordOAuthDeviceCodeGenerated records device code generation

func (*Metrics) RecordOAuthDeviceCodeValidation

func (m *Metrics) RecordOAuthDeviceCodeValidation(result string)

RecordOAuthDeviceCodeValidation records device code validation result

func (*Metrics) RecordSessionExpired

func (m *Metrics) RecordSessionExpired(reason string, duration time.Duration)

RecordSessionExpired records session expiration

func (*Metrics) RecordSessionInvalidated

func (m *Metrics) RecordSessionInvalidated(reason string)

RecordSessionInvalidated records session invalidation

func (*Metrics) RecordTokenIssued

func (m *Metrics) RecordTokenIssued(
	tokenType, grantType string,
	generationTime time.Duration,
	provider string,
)

RecordTokenIssued records token issuance

func (*Metrics) RecordTokenRefresh

func (m *Metrics) RecordTokenRefresh(success bool)

RecordTokenRefresh records token refresh attempt

func (*Metrics) RecordTokenRevoked

func (m *Metrics) RecordTokenRevoked(tokenType, reason string)

RecordTokenRevoked records token revocation

func (*Metrics) RecordTokenValidation

func (m *Metrics) RecordTokenValidation(result string, duration time.Duration, provider string)

RecordTokenValidation records token validation

func (*Metrics) SetActiveDeviceCodesCount

func (m *Metrics) SetActiveDeviceCodesCount(total, pending int)

SetActiveDeviceCodesCount sets the current count of active device codes (for periodic updates)

func (*Metrics) SetActiveSessionsCount

func (m *Metrics) SetActiveSessionsCount(count int)

SetActiveSessionsCount sets the current count of active sessions (for periodic updates)

func (*Metrics) SetActiveTokensCount

func (m *Metrics) SetActiveTokensCount(tokenType string, count int)

SetActiveTokensCount sets the current count of active tokens (for periodic updates)

func (*Metrics) String

func (m *Metrics) String() string

String formats the metrics for logging

type NoopMetrics

type NoopMetrics struct{}

NoopMetrics is a no-operation implementation of Recorder All methods are empty and do nothing, providing zero overhead when metrics are disabled

func (*NoopMetrics) RecordAuthAttempt

func (n *NoopMetrics) RecordAuthAttempt(method string, success bool, duration time.Duration)

Authentication - noop implementations

func (*NoopMetrics) RecordDatabaseQueryError

func (n *NoopMetrics) RecordDatabaseQueryError(operation string)

Database Operations - noop implementations

func (*NoopMetrics) RecordExternalAPICall

func (n *NoopMetrics) RecordExternalAPICall(provider string, duration time.Duration)

func (*NoopMetrics) RecordLogin

func (n *NoopMetrics) RecordLogin(authSource string, success bool)

func (*NoopMetrics) RecordLogout

func (n *NoopMetrics) RecordLogout(sessionDuration time.Duration)

func (*NoopMetrics) RecordOAuthCallback

func (n *NoopMetrics) RecordOAuthCallback(provider string, success bool)

func (*NoopMetrics) RecordOAuthDeviceCodeAuthorized

func (n *NoopMetrics) RecordOAuthDeviceCodeAuthorized(authorizationTime time.Duration)

func (*NoopMetrics) RecordOAuthDeviceCodeGenerated

func (n *NoopMetrics) RecordOAuthDeviceCodeGenerated(success bool)

OAuth Device Flow - noop implementations

func (*NoopMetrics) RecordOAuthDeviceCodeValidation

func (n *NoopMetrics) RecordOAuthDeviceCodeValidation(result string)

func (*NoopMetrics) RecordSessionExpired

func (n *NoopMetrics) RecordSessionExpired(reason string, duration time.Duration)

Session Management - noop implementations

func (*NoopMetrics) RecordSessionInvalidated

func (n *NoopMetrics) RecordSessionInvalidated(reason string)

func (*NoopMetrics) RecordTokenIssued

func (n *NoopMetrics) RecordTokenIssued(
	tokenType, grantType string,
	generationTime time.Duration,
	provider string,
)

Token Operations - noop implementations

func (*NoopMetrics) RecordTokenRefresh

func (n *NoopMetrics) RecordTokenRefresh(
	success bool,
)

func (*NoopMetrics) RecordTokenRevoked

func (n *NoopMetrics) RecordTokenRevoked(
	tokenType, reason string,
)

func (*NoopMetrics) RecordTokenValidation

func (n *NoopMetrics) RecordTokenValidation(
	result string,
	duration time.Duration,
	provider string,
)

func (*NoopMetrics) SetActiveDeviceCodesCount

func (n *NoopMetrics) SetActiveDeviceCodesCount(total, pending int)

func (*NoopMetrics) SetActiveSessionsCount

func (n *NoopMetrics) SetActiveSessionsCount(count int)

func (*NoopMetrics) SetActiveTokensCount

func (n *NoopMetrics) SetActiveTokensCount(tokenType string, count int)

Gauge Setters - noop implementations

type Recorder

type Recorder interface {
	// OAuth Device Flow
	RecordOAuthDeviceCodeGenerated(success bool)
	RecordOAuthDeviceCodeAuthorized(authorizationTime time.Duration)
	RecordOAuthDeviceCodeValidation(result string)

	// Token Operations
	RecordTokenIssued(tokenType, grantType string, generationTime time.Duration, provider string)
	RecordTokenRevoked(tokenType, reason string)
	RecordTokenRefresh(success bool)
	RecordTokenValidation(result string, duration time.Duration, provider string)

	// Authentication
	RecordAuthAttempt(method string, success bool, duration time.Duration)
	RecordLogin(authSource string, success bool)
	RecordLogout(sessionDuration time.Duration)
	RecordOAuthCallback(provider string, success bool)
	RecordExternalAPICall(provider string, duration time.Duration)

	// Session Management
	RecordSessionExpired(reason string, duration time.Duration)
	RecordSessionInvalidated(reason string)

	// Gauge Setters (for periodic updates)
	SetActiveTokensCount(tokenType string, count int)
	SetActiveDeviceCodesCount(total, pending int)
	SetActiveSessionsCount(count int)

	// Database Operations
	RecordDatabaseQueryError(operation string)
}

Recorder defines the interface for recording application metrics Implementations include Metrics (Prometheus-based) and NoopMetrics (no-op)

func Init

func Init(enabled bool) Recorder

Init initializes metrics based on enabled flag If enabled=true, returns Prometheus-based Metrics If enabled=false, returns NoopMetrics (zero overhead) Uses sync.Once to ensure Prometheus metrics are only registered once

func NewNoopMetrics

func NewNoopMetrics() Recorder

NewNoopMetrics creates a new no-operation metrics recorder

Jump to

Keyboard shortcuts

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