Documentation
¶
Index ¶
- Variables
- func GetAPIMetrics() map[string]interface{}
- func GetCacheHitRatio(cacheType string) float64
- func GinMetricsMiddleware(config *MetricsConfig) gin.HandlerFunc
- func HTTPMetricsMiddleware() func(next http.Handler) http.Handler
- func RecordAPICall(service, operation, status string)
- func RecordAPICallWithContext(endpoint, method, status, version string, duration time.Duration)
- func RecordAuthenticationAttempt(status, method string)
- func RecordCacheHit(cacheType, key string)
- func RecordCacheMiss(cacheType, key string)
- func RecordDBMetrics(operation string, duration time.Duration, err error)
- func RecordError(errorType, component, severity string)
- func RecordExternalServiceCall(service, endpoint, status string, duration time.Duration)
- func RecordMessageQueueMetrics(queue, operation string, duration time.Duration, err error)
- func RecordRateLimitHit(key, endpoint string)
- func RecordRedisMetrics(command string, duration time.Duration, err error)
- func RecordSLAViolation(endpoint string, threshold float64)
- func RecordUserLogin(status string)
- func RecordUserRegistration()
- func SetActiveUsers(count float64)
- func SetAppInfo(version, instanceID string)
- func SetDBConnections(database string, active, idle int)
- func SetRedisConnections(active int)
- func StartServer(cfg *config.MonitoringConfig, logger *slog.Logger)
- type DatabaseMetricsCollector
- type HealthCheckCollector
- type MetricsConfig
Constants ¶
This section is empty.
Variables ¶
var ( // Gin specific metrics GinRequestsTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "gin_requests_total", Help: "Total number of HTTP requests processed by Gin", }, []string{"method", "route", "status_code"}, ) GinRequestDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "gin_request_duration_seconds", Help: "Duration of HTTP requests processed by Gin", Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, }, []string{"method", "route"}, ) GinRequestSize = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "gin_request_size_bytes", Help: "Size of HTTP requests processed by Gin", Buckets: prometheus.ExponentialBuckets(1024, 2, 10), }, []string{"method", "route"}, ) GinResponseSize = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "gin_response_size_bytes", Help: "Size of HTTP responses processed by Gin", Buckets: prometheus.ExponentialBuckets(1024, 2, 10), }, []string{"method", "route"}, ) GinActiveRequests = promauto.NewGaugeVec( prometheus.GaugeOpts{ Name: "gin_active_requests", Help: "Number of active HTTP requests being processed by Gin", }, []string{"method", "route"}, ) // API endpoint specific metrics APIEndpointMetrics = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "api_endpoint_requests_total", Help: "Total number of requests per API endpoint", }, []string{"endpoint", "method", "status_code", "version"}, ) APIEndpointDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "api_endpoint_duration_seconds", Help: "Duration of API endpoint requests", Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, }, []string{"endpoint", "method", "version"}, ) // SLA metrics SLAViolations = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "sla_violations_total", Help: "Total number of SLA violations", }, []string{"endpoint", "threshold"}, ) )
var ( // HTTP metrics HTTPRequestsTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", Help: "Total number of HTTP requests", }, []string{"method", "endpoint", "status"}, ) HTTPRequestDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "Duration of HTTP requests", Buckets: prometheus.DefBuckets, }, []string{"method", "endpoint"}, ) // Database metrics DBConnectionsActive = promauto.NewGaugeVec( prometheus.GaugeOpts{ Name: "db_connections_active", Help: "Number of active database connections", }, []string{"database"}, ) DBConnectionsIdle = promauto.NewGaugeVec( prometheus.GaugeOpts{ Name: "db_connections_idle", Help: "Number of idle database connections", }, []string{"database"}, ) DBQueryDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "db_query_duration_seconds", Help: "Duration of database queries", Buckets: prometheus.DefBuckets, }, []string{"operation"}, ) DBQueryTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "db_query_total", Help: "Total number of database queries", }, []string{"operation", "status"}, ) // Redis metrics RedisCommandsTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "redis_commands_total", Help: "Total number of Redis commands", }, []string{"command", "status"}, ) RedisCommandsDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "redis_commands_duration_seconds", Help: "Duration of Redis commands", Buckets: prometheus.DefBuckets, }, []string{"command"}, ) RedisConnectionsActive = promauto.NewGauge( prometheus.GaugeOpts{ Name: "redis_connections_active", Help: "Number of active Redis connections", }, ) // Message queue metrics MessageQueuePublishedTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "message_queue_published_total", Help: "Total number of messages published to queue", }, []string{"queue", "status"}, ) MessageQueueConsumedTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "message_queue_consumed_total", Help: "Total number of messages consumed from queue", }, []string{"queue", "status"}, ) MessageQueueProcessingDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "message_queue_processing_duration_seconds", Help: "Duration of message processing", Buckets: prometheus.DefBuckets, }, []string{"queue"}, ) // Application metrics AppInfo = promauto.NewGaugeVec( prometheus.GaugeOpts{ Name: "app_info", Help: "Application information", }, []string{"version", "instance_id"}, ) AppUptime = promauto.NewGauge( prometheus.GaugeOpts{ Name: "app_uptime_seconds", Help: "Application uptime in seconds", }, ) // Custom business metrics ActiveUsers = promauto.NewGauge( prometheus.GaugeOpts{ Name: "active_users", Help: "Number of active users", }, ) RequestsInFlight = promauto.NewGauge( prometheus.GaugeOpts{ Name: "requests_in_flight", Help: "Number of requests currently being processed", }, ) // System metrics CPUUsage = promauto.NewGauge( prometheus.GaugeOpts{ Name: "cpu_usage_percent", Help: "Current CPU usage percentage", }, ) MemoryUsage = promauto.NewGauge( prometheus.GaugeOpts{ Name: "memory_usage_bytes", Help: "Current memory usage in bytes", }, ) MemoryTotal = promauto.NewGauge( prometheus.GaugeOpts{ Name: "memory_total_bytes", Help: "Total available memory in bytes", }, ) Goroutines = promauto.NewGauge( prometheus.GaugeOpts{ Name: "goroutines_total", Help: "Number of goroutines currently running", }, ) GCDuration = promauto.NewHistogram( prometheus.HistogramOpts{ Name: "gc_duration_seconds", Help: "Time spent in garbage collection", Buckets: prometheus.DefBuckets, }, ) // Middleware metrics AuthenticationAttempts = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "authentication_attempts_total", Help: "Total number of authentication attempts", }, []string{"status", "method"}, ) RateLimitHits = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "rate_limit_hits_total", Help: "Total number of rate limit hits", }, []string{"key", "endpoint"}, ) CacheHits = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "cache_hits_total", Help: "Total number of cache hits", }, []string{"cache_type", "key"}, ) CacheMisses = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "cache_misses_total", Help: "Total number of cache misses", }, []string{"cache_type", "key"}, ) // Business domain metrics UserRegistrations = promauto.NewCounter( prometheus.CounterOpts{ Name: "user_registrations_total", Help: "Total number of user registrations", }, ) UserLogins = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "user_logins_total", Help: "Total number of user logins", }, []string{"status"}, ) APICallsTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "api_calls_total", Help: "Total number of API calls", }, []string{"service", "operation", "status"}, ) ErrorsTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "errors_total", Help: "Total number of errors", }, []string{"type", "component", "severity"}, ) // External service metrics ExternalServiceCalls = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "external_service_calls_total", Help: "Total number of external service calls", }, []string{"service", "endpoint", "status"}, ) ExternalServiceDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "external_service_duration_seconds", Help: "Duration of external service calls", Buckets: prometheus.DefBuckets, }, []string{"service", "endpoint"}, ) )
Functions ¶
func GetAPIMetrics ¶
func GetAPIMetrics() map[string]interface{}
GetAPIMetrics returns current API metrics for health checks
func GetCacheHitRatio ¶
GetCacheHitRatio calculates cache hit ratio
func GinMetricsMiddleware ¶
func GinMetricsMiddleware(config *MetricsConfig) gin.HandlerFunc
GinMetricsMiddleware returns a Gin middleware that collects metrics
func HTTPMetricsMiddleware ¶
HTTPMetricsMiddleware returns a middleware that collects HTTP metrics
func RecordAPICall ¶
func RecordAPICall(service, operation, status string)
RecordAPICall records API call metrics
func RecordAPICallWithContext ¶
RecordAPICallWithContext records an API call with additional context
func RecordAuthenticationAttempt ¶
func RecordAuthenticationAttempt(status, method string)
RecordAuthenticationAttempt records authentication attempt metrics
func RecordCacheHit ¶
func RecordCacheHit(cacheType, key string)
RecordCacheHit records cache hit metrics
func RecordCacheMiss ¶
func RecordCacheMiss(cacheType, key string)
RecordCacheMiss records cache miss metrics
func RecordDBMetrics ¶
RecordDBMetrics records database metrics
func RecordError ¶
func RecordError(errorType, component, severity string)
RecordError records error metrics
func RecordExternalServiceCall ¶
RecordExternalServiceCall records external service call metrics
func RecordMessageQueueMetrics ¶
RecordMessageQueueMetrics records message queue metrics
func RecordRateLimitHit ¶
func RecordRateLimitHit(key, endpoint string)
RecordRateLimitHit records rate limit hit metrics
func RecordRedisMetrics ¶
RecordRedisMetrics records Redis metrics
func RecordSLAViolation ¶
RecordSLAViolation records an SLA violation
func RecordUserLogin ¶
func RecordUserLogin(status string)
RecordUserLogin records user login metrics
func RecordUserRegistration ¶
func RecordUserRegistration()
RecordUserRegistration records user registration metrics
func SetActiveUsers ¶
func SetActiveUsers(count float64)
SetActiveUsers sets the number of active users
func SetAppInfo ¶
func SetAppInfo(version, instanceID string)
SetAppInfo sets application information
func SetDBConnections ¶
SetDBConnections sets database connection metrics
func SetRedisConnections ¶
func SetRedisConnections(active int)
SetRedisConnections sets Redis connection metrics
func StartServer ¶
func StartServer(cfg *config.MonitoringConfig, logger *slog.Logger)
StartServer starts the Prometheus metrics server
Types ¶
type DatabaseMetricsCollector ¶
type DatabaseMetricsCollector struct {
// contains filtered or unexported fields
}
DatabaseMetricsCollector collects database metrics from database instances
func NewDatabaseMetricsCollector ¶
func NewDatabaseMetricsCollector(databases map[string]interface{}) *DatabaseMetricsCollector
NewDatabaseMetricsCollector creates a new database metrics collector
func (*DatabaseMetricsCollector) CollectDatabaseMetrics ¶
func (c *DatabaseMetricsCollector) CollectDatabaseMetrics()
CollectDatabaseMetrics collects metrics from all registered databases
type HealthCheckCollector ¶
type HealthCheckCollector struct {
// contains filtered or unexported fields
}
HealthCheckCollector provides health check metrics
func NewHealthCheckCollector ¶
func NewHealthCheckCollector() *HealthCheckCollector
NewHealthCheckCollector creates a new health check collector
func (*HealthCheckCollector) CollectHealthMetrics ¶
func (c *HealthCheckCollector) CollectHealthMetrics()
CollectHealthMetrics collects health check metrics
func (*HealthCheckCollector) RegisterHealthCheck ¶
func (c *HealthCheckCollector) RegisterHealthCheck(name string, check func() error)
RegisterHealthCheck registers a health check function
type MetricsConfig ¶
type MetricsConfig struct {
// SLA thresholds for different endpoints (in seconds)
SLAThresholds map[string]float64
// Skip paths that should not be monitored
SkipPaths []string
// Group similar paths to reduce cardinality
PathGrouping map[string]string
}
MetricsConfig holds configuration for metrics middleware
func DefaultMetricsConfig ¶
func DefaultMetricsConfig() *MetricsConfig
DefaultMetricsConfig returns default configuration