forked from mtusi2026/mtusi2026lab3
Lab3 baseline: HAProxy, replicated stateless API, shared PostgreSQL/Valkey, idempotent messages
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// InternalStats is the operator-only JSON document served on the loopback
|
||||
// metrics listener inside each API container. It is deliberately not part of
|
||||
// the public contract and is never routed through the load balancer; the
|
||||
// experiment scripts read it with `docker exec` to attribute saturation to
|
||||
// the connection pool versus PostgreSQL/CPU.
|
||||
type InternalStats struct {
|
||||
InstanceID string `json:"instance_id"`
|
||||
Version string `json:"version"`
|
||||
UptimeSeconds float64 `json:"uptime_seconds"`
|
||||
Pool poolStats `json:"pool"`
|
||||
Messages messageCounters `json:"messages"`
|
||||
Runtime runtimeStats `json:"runtime"`
|
||||
CollectedAt string `json:"collected_at"`
|
||||
}
|
||||
|
||||
type poolStats struct {
|
||||
MaxConns int32 `json:"max_conns"`
|
||||
TotalConns int32 `json:"total_conns"`
|
||||
AcquiredConns int32 `json:"acquired_conns"`
|
||||
IdleConns int32 `json:"idle_conns"`
|
||||
EmptyAcquireCount int64 `json:"empty_acquire_count"`
|
||||
CanceledAcquireCount int64 `json:"canceled_acquire_count"`
|
||||
AcquireCount int64 `json:"acquire_count"`
|
||||
AcquireDurationMillis int64 `json:"acquire_duration_ms_total"`
|
||||
NewConnsCount int64 `json:"new_conns_count"`
|
||||
MaxLifetimeDestroyCount int64 `json:"max_lifetime_destroy_count"`
|
||||
MaxIdleDestroyCount int64 `json:"max_idle_destroy_count"`
|
||||
}
|
||||
|
||||
type messageCounters struct {
|
||||
Created uint64 `json:"created"`
|
||||
Replayed uint64 `json:"replayed"`
|
||||
Conflicts uint64 `json:"conflicts"`
|
||||
}
|
||||
|
||||
type runtimeStats struct {
|
||||
Goroutines int `json:"goroutines"`
|
||||
HeapAlloc uint64 `json:"heap_alloc_bytes"`
|
||||
HeapSys uint64 `json:"heap_sys_bytes"`
|
||||
TotalAlloc uint64 `json:"total_alloc_bytes"`
|
||||
}
|
||||
|
||||
// InternalStatsHandler serves one snapshot. It is mounted only on the
|
||||
// 127.0.0.1 listener started by cmd/messenger.
|
||||
func (a *App) InternalStatsHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
pool := a.store.PoolStats()
|
||||
var memory runtime.MemStats
|
||||
runtime.ReadMemStats(&memory)
|
||||
stats := InternalStats{
|
||||
InstanceID: a.instanceID,
|
||||
Version: a.version,
|
||||
UptimeSeconds: time.Since(a.startedAt).Seconds(),
|
||||
Pool: poolStats{
|
||||
MaxConns: pool.MaxConns,
|
||||
TotalConns: pool.TotalConns,
|
||||
AcquiredConns: pool.AcquiredConns,
|
||||
IdleConns: pool.IdleConns,
|
||||
EmptyAcquireCount: pool.EmptyAcquireCount,
|
||||
CanceledAcquireCount: pool.CanceledAcquireCount,
|
||||
AcquireCount: pool.AcquireCount,
|
||||
AcquireDurationMillis: pool.AcquireDurationMillis,
|
||||
NewConnsCount: pool.NewConnsCount,
|
||||
MaxLifetimeDestroyCount: pool.MaxLifetimeDestroyCount,
|
||||
MaxIdleDestroyCount: pool.MaxIdleDestroyCount,
|
||||
},
|
||||
Messages: messageCounters{
|
||||
Created: a.createdMessages.Load(),
|
||||
Replayed: a.replayedMessages.Load(),
|
||||
Conflicts: a.conflictMessages.Load(),
|
||||
},
|
||||
Runtime: runtimeStats{
|
||||
Goroutines: runtime.NumGoroutine(),
|
||||
HeapAlloc: memory.HeapAlloc,
|
||||
HeapSys: memory.HeapSys,
|
||||
TotalAlloc: memory.TotalAlloc,
|
||||
},
|
||||
CollectedAt: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(stats)
|
||||
}
|
||||
Reference in New Issue
Block a user