Documentation
¶
Index ¶
- func GetMetrics() map[string]any
- func LatencyMiddleware(latencyMs int) func(echo.HandlerFunc) echo.HandlerFunc
- func MemoryStatsMiddleware(next echo.HandlerFunc) echo.HandlerFunc
- func RecordDeleteQueueDepth(service string, depth int)
- func RecordLockDuration(lockType string, durationSeconds float64)
- func RecordOperation(operation, _ string, durationSeconds float64, status string)
- func RecordStreamEvents(service string, count int)
- func RecordTTLEvictions(service string, count int)
- func RecordWorkerItems(service, worker string, count int)
- func RecordWorkerQueueDepth(service, worker string, depth int)
- func RecordWorkerTask(service, worker, status string)
- func SetServiceCount(n int)
- func WrapEchoHandler(serviceName string, handler echo.HandlerFunc, observer ObservabilityObserver) echo.HandlerFunc
- type Dashboard
- type DeadlockInfo
- type ObservabilityObserver
- type RuntimeMetrics
- type Summary
- type WorkerStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetMetrics ¶
GetMetrics returns a snapshot of metrics for dashboard consumption.
func LatencyMiddleware ¶
func LatencyMiddleware(latencyMs int) func(echo.HandlerFunc) echo.HandlerFunc
LatencyMiddleware returns a middleware that injects a random sleep of [0, latencyMs) milliseconds before each request, simulating real-world network latency. This helps surface timeout bugs, race conditions, and missing retry logic. If latencyMs is zero or negative, the middleware is a no-op. The sleep is context-aware: it is interrupted immediately when the request context is cancelled (e.g. client disconnect, server shutdown).
func MemoryStatsMiddleware ¶
func MemoryStatsMiddleware(next echo.HandlerFunc) echo.HandlerFunc
MemoryStatsMiddleware injects the X-Gopherstack-Memory-Stats header into all responses. The header contains Alloc, TotalAlloc, Sys, and NumGC stats from runtime.MemStats. This allows long-term monitoring of the Gopherstack container's memory health.
func RecordDeleteQueueDepth ¶
RecordDeleteQueueDepth updates the live delete-queue depth gauge for a service. service should be "s3" or "dynamodb".
func RecordLockDuration ¶
RecordLockDuration records lock hold time.
func RecordOperation ¶
RecordOperation records an operation latency and status. operation: name of the operation (e.g., "GetItem", "PutObject") resource: name of the resource (ignored for Prometheus metrics to reduce cardinality) durationSeconds: how long the operation took status: "success" or "error"
func RecordStreamEvents ¶
RecordStreamEvents records the number of stream records delivered via GetRecords.
func RecordTTLEvictions ¶
RecordTTLEvictions records that count items were evicted via background TTL sweep.
func RecordWorkerItems ¶
RecordWorkerItems records the number of items processed by a background worker.
func RecordWorkerQueueDepth ¶
RecordWorkerQueueDepth updates the current queue depth for a background worker.
func RecordWorkerTask ¶
func RecordWorkerTask(service, worker, status string)
RecordWorkerTask records that a background worker has processed a task. service: e.g., "dynamodb", "s3" worker: e.g., "TableCleaner", "TTLSweeper" status: "success" or "error"
func SetServiceCount ¶
func SetServiceCount(n int)
SetServiceCount records the number of registered services. Call this once at startup after all services have been initialised. The value is exposed via the "gopherstack_registered_services" Prometheus gauge and included in the RuntimeMetrics.NumServices field returned by CollectMetrics.
func WrapEchoHandler ¶
func WrapEchoHandler( serviceName string, handler echo.HandlerFunc, observer ObservabilityObserver, ) echo.HandlerFunc
WrapEchoHandler wraps an Echo handler with automated metrics and logging. The wrapper extracts operation and resource names via the observer, times the handler execution, records metrics, and logs the result.
Per request, it enriches the request context logger with a "service" attribute so that all downstream log lines are automatically tagged. The logger is pulled from the request context via pkglogger.Load; callers are expected to inject a logger into the context before requests arrive (e.g. via pkglogger.EchoMiddleware).
This eliminates boilerplate from service handlers—they focus on business logic while observability is handled automatically.
Types ¶
type Dashboard ¶
type Dashboard struct {
Runtime *RuntimeMetrics `json:"runtime"`
Operations []Summary `json:"operations"`
Deadlocks []DeadlockInfo `json:"deadlocks"`
Workers []WorkerStats `json:"workers"`
}
Dashboard holds all metrics for dashboard display.
func CollectMetrics ¶
func CollectMetrics() *Dashboard
CollectMetrics gathers current metrics from Prometheus registry.
type DeadlockInfo ¶
type DeadlockInfo struct {
Lock string `json:"lock"`
Operation string `json:"operation"`
HeldSec float64 `json:"held_sec"`
Waiters int `json:"waiters"`
}
DeadlockInfo holds information about a potential deadlock.
type ObservabilityObserver ¶
type ObservabilityObserver interface {
// ExtractOperation returns the operation name.
ExtractOperation(c *echo.Context) string
// ExtractResource returns the resource identifier.
ExtractResource(c *echo.Context) string
}
ObservabilityObserver extracts metrics labels from an Echo request.
type RuntimeMetrics ¶
type RuntimeMetrics struct {
Goroutines int `json:"goroutines"`
HeapAllocMB float64 `json:"heap_alloc_mb"`
HeapInuseMB float64 `json:"heap_inuse_mb"`
HeapSysMB float64 `json:"heap_sys_mb"`
NumGC uint32 `json:"num_gc"`
LastGCPause float64 `json:"last_gc_pause_ms"`
TotalAllocMB float64 `json:"total_alloc_mb"`
NumServices int `json:"num_services"`
}
RuntimeMetrics holds Go runtime statistics.
type Summary ¶
type Summary struct {
Operation string `json:"operation"`
Count int64 `json:"count"`
ErrorCount int64 `json:"error_count"`
P50Ms float64 `json:"p50_ms"`
P95Ms float64 `json:"p95_ms"`
P99Ms float64 `json:"p99_ms"`
AvgMs float64 `json:"avg_ms"`
MaxMs float64 `json:"max_ms"`
}
Summary holds aggregated metrics for a single operation.
type WorkerStats ¶
type WorkerStats struct {
Service string `json:"service"`
Worker string `json:"worker"`
QueueDepth int `json:"queue_depth"`
TasksTotal int64 `json:"tasks_total"`
ErrorsTotal int64 `json:"errors_total"`
ItemsProcessed int64 `json:"items_processed_total"`
}
WorkerStats holds aggregated metrics for a background worker.