middleware

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RequestIDKey is the context key for request ID
	RequestIDKey = "request_id"
	// RequestIDHeader is the header key for request ID
	RequestIDHeader = "X-Request-ID"
)

Variables

View Source
var (
	// AppInfo is a constant gauge (value 1) with version and go_version labels.
	AppInfo = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "app_info",
			Help: "Application build information.",
		},
		[]string{"version", "go_version"},
	)

	// AppUptimeSeconds reports seconds since server start.
	AppUptimeSeconds = prometheus.NewGaugeFunc(
		prometheus.GaugeOpts{
			Name: "app_uptime_seconds",
			Help: "Seconds since server start.",
		},
		func() float64 { return 0 },
	)

	// HealthCheckStatus is 1 when the service is healthy, 0 when unhealthy.
	HealthCheckStatus = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "health_check_status",
			Help: "Health check status: 1 = healthy, 0 = unhealthy.",
		},
	)
)
View Source
var (
	// HTTPRequestsTotal counts total HTTP requests by method, path, and status.
	HTTPRequestsTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "http_requests_total",
			Help: "Total number of HTTP requests.",
		},
		[]string{"method", "path", "status"},
	)

	// HTTPRequestDurationSeconds tracks HTTP request latency.
	HTTPRequestDurationSeconds = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "http_request_duration_seconds",
			Help:    "HTTP request latency in seconds.",
			Buckets: prometheus.DefBuckets,
		},
		[]string{"method", "path"},
	)

	// HTTPResponseSizeBytes tracks HTTP response sizes.
	HTTPResponseSizeBytes = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "http_response_size_bytes",
			Help:    "HTTP response size in bytes.",
			Buckets: prometheus.ExponentialBuckets(100, 10, 7),
		},
		[]string{"method", "path"},
	)

	// HTTPRequestsInFlight tracks the number of in-flight HTTP requests.
	HTTPRequestsInFlight = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "http_requests_in_flight",
			Help: "Number of HTTP requests currently being processed.",
		},
	)

	// APIPanicsRecoveredTotal counts panics recovered by the recovery middleware.
	APIPanicsRecoveredTotal = prometheus.NewCounter(
		prometheus.CounterOpts{
			Name: "api_panics_recovered_total",
			Help: "Total number of panics recovered.",
		},
	)
)
View Source
var (
	// RateLimitHitsTotal counts requests that were rate-limited.
	RateLimitHitsTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "rate_limit_hits_total",
			Help: "Total number of requests that were rate-limited.",
		},
		[]string{"path"},
	)
)

Functions

func AdminMiddleware

func AdminMiddleware(jwtManager *jwt.Manager, adminEmail string, getUserEmail func(userID uuid.UUID) (string, error)) gin.HandlerFunc

AdminMiddleware creates a middleware that restricts access to admin users only. It requires the auth middleware to have already set "userID" in the context. The admin user is determined by matching the user's email against the ADMIN_EMAIL env var.

func AuthMiddleware

func AuthMiddleware(jwtManager *jwt.Manager, publicPaths []string) gin.HandlerFunc

AuthMiddleware enforces JWT authentication on all routes except explicitly allowed public paths. It extracts the user ID from the token and sets it in the Gin context as "userID".

func LoggerMiddleware

func LoggerMiddleware() gin.HandlerFunc

LoggerMiddleware creates a middleware for request logging

func MetricsMiddleware

func MetricsMiddleware() gin.HandlerFunc

MetricsMiddleware returns a Gin middleware that records Prometheus metrics for every HTTP request: counter, duration histogram, response size histogram, and in-flight gauge.

func NewRateLimitMiddleware

func NewRateLimitMiddleware(rate int64, period time.Duration) gin.HandlerFunc

NewRateLimitMiddleware creates a Gin middleware that rate-limits requests. rate is the number of requests allowed per period (e.g., 10 requests per 1 minute). It uses Gin's c.ClientIP() to key rate limits by the real client IP (respecting X-Real-IP / X-Forwarded-For headers set by nginx) instead of the proxy's address.

func RateLimitByPath

func RateLimitByPath(rl gin.HandlerFunc, paths ...string) gin.HandlerFunc

RateLimitByPath applies a rate-limit middleware only to requests whose path (relative to the router group) matches one of the given suffixes.

func RecoveryWithMetrics

func RecoveryWithMetrics() gin.HandlerFunc

RecoveryWithMetrics returns a Gin middleware that recovers from panics, increments the api_panics_recovered_total counter, and returns a 500 response.

func RegisterAppMetrics

func RegisterAppMetrics(version string, startedAt time.Time)

RegisterAppMetrics registers app_info and app_uptime_seconds gauges. version is the application version string (e.g. "1.0.0" or a git SHA).

func SecurityHeadersMiddleware

func SecurityHeadersMiddleware() gin.HandlerFunc

SecurityHeadersMiddleware adds standard security headers to all API responses.

Types

type DBStatsCollector

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

DBStatsCollector implements prometheus.Collector and exposes sql.DBStats as Prometheus gauges on every scrape.

func NewDBStatsCollector

func NewDBStatsCollector(db *sql.DB) *DBStatsCollector

NewDBStatsCollector creates a collector that reads stats from db on every scrape.

func (*DBStatsCollector) Collect

func (c *DBStatsCollector) Collect(ch chan<- prometheus.Metric)

Collect reads the current sql.DBStats and sends each metric value.

func (*DBStatsCollector) Describe

func (c *DBStatsCollector) Describe(ch chan<- *prometheus.Desc)

Describe sends the descriptors of each metric to the channel.

Jump to

Keyboard shortcuts

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