metrics

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPMetricsMiddleware

func HTTPMetricsMiddleware(m core.Recorder) gin.HandlerFunc

HTTPMetricsMiddleware creates a Gin middleware that records HTTP metrics

func Init

func Init(enabled bool) core.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() core.Recorder

NewNoopMetrics creates a new no-operation metrics recorder

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 core.MetricsStore, cache core.Cache[int64]) *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

	// Session Metrics
	SessionsActive       prometheus.Gauge
	SessionsCreatedTotal prometheus.Counter
	SessionsExpiredTotal *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) 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) 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) 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) 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) 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) SetActiveTokensCount

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

Gauge Setters - noop implementations

Jump to

Keyboard shortcuts

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