feat: add runtime statistics to healthcheck endpoint
All checks were successful
check / check (push) Successful in 1m4s
All checks were successful
check / check (push) Successful in 1m4s
Add the following counters to the healthcheck JSON response: - sessions: current active session count (from DB) - clients: current connected client count (from DB) - queuedLines: total entries in client output queues (from DB) - channels: current channel count (from DB) - connectionsSinceBoot: total client connections since server start - sessionsSinceBoot: total sessions created since server start - messagesSinceBoot: total PRIVMSG/NOTICE messages since server start Implementation: - New internal/stats package with atomic counters for boot-scoped metrics - New DB queries GetClientCount and GetQueueEntryCount - Healthcheck.Healthcheck() now accepts context for DB queries - Counter increments in session creation, registration, login, and messaging - Stats tracker wired via Uber fx dependency injection - Unit tests for stats package (100% coverage) and integration tests - README updated with full healthcheck response documentation closes #74
This commit is contained in:
33
README.md
33
README.md
@@ -1399,13 +1399,40 @@ Return server metadata. No authentication required.
|
|||||||
|
|
||||||
### GET /.well-known/healthcheck.json — Health Check
|
### GET /.well-known/healthcheck.json — Health Check
|
||||||
|
|
||||||
Standard health check endpoint. No authentication required.
|
Standard health check endpoint. No authentication required. Returns server
|
||||||
|
health status and runtime statistics.
|
||||||
|
|
||||||
**Response:** `200 OK`
|
**Response:** `200 OK`
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{"status": "ok"}
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"now": "2024-01-15T12:00:00.000000000Z",
|
||||||
|
"uptimeSeconds": 3600,
|
||||||
|
"uptimeHuman": "1h0m0s",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"appname": "neoirc",
|
||||||
|
"maintenanceMode": false,
|
||||||
|
"sessions": 42,
|
||||||
|
"clients": 85,
|
||||||
|
"queuedLines": 128,
|
||||||
|
"channels": 7,
|
||||||
|
"connectionsSinceBoot": 200,
|
||||||
|
"sessionsSinceBoot": 150,
|
||||||
|
"messagesSinceBoot": 5000
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
| Field | Description |
|
||||||
|
| ---------------------- | ------------------------------------------------- |
|
||||||
|
| `sessions` | Current number of active sessions |
|
||||||
|
| `clients` | Current number of connected clients |
|
||||||
|
| `queuedLines` | Total entries in client output queues |
|
||||||
|
| `channels` | Current number of channels |
|
||||||
|
| `connectionsSinceBoot` | Total client connections since server start |
|
||||||
|
| `sessionsSinceBoot` | Total sessions created since server start |
|
||||||
|
| `messagesSinceBoot` | Total PRIVMSG/NOTICE messages sent since server start |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Message Flow
|
## Message Flow
|
||||||
@@ -2332,6 +2359,8 @@ neoirc/
|
|||||||
│ │ └── healthcheck.go # Health check handler
|
│ │ └── healthcheck.go # Health check handler
|
||||||
│ ├── healthcheck/ # Health check logic
|
│ ├── healthcheck/ # Health check logic
|
||||||
│ │ └── healthcheck.go
|
│ │ └── healthcheck.go
|
||||||
|
│ ├── stats/ # Runtime statistics (atomic counters)
|
||||||
|
│ │ └── stats.go
|
||||||
│ ├── logger/ # slog-based logging
|
│ ├── logger/ # slog-based logging
|
||||||
│ │ └── logger.go
|
│ │ └── logger.go
|
||||||
│ ├── middleware/ # HTTP middleware (logging, CORS, metrics, auth)
|
│ ├── middleware/ # HTTP middleware (logging, CORS, metrics, auth)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"git.eeqj.de/sneak/neoirc/internal/logger"
|
"git.eeqj.de/sneak/neoirc/internal/logger"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/middleware"
|
"git.eeqj.de/sneak/neoirc/internal/middleware"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/server"
|
"git.eeqj.de/sneak/neoirc/internal/server"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/stats"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,6 +36,7 @@ func main() {
|
|||||||
server.New,
|
server.New,
|
||||||
middleware.New,
|
middleware.New,
|
||||||
healthcheck.New,
|
healthcheck.New,
|
||||||
|
stats.New,
|
||||||
),
|
),
|
||||||
fx.Invoke(func(*server.Server) {}),
|
fx.Invoke(func(*server.Server) {}),
|
||||||
).Run()
|
).Run()
|
||||||
|
|||||||
@@ -1266,3 +1266,42 @@ func (database *Database) PruneOldMessages(
|
|||||||
|
|
||||||
return deleted, nil
|
return deleted, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetClientCount returns the total number of clients.
|
||||||
|
func (database *Database) GetClientCount(
|
||||||
|
ctx context.Context,
|
||||||
|
) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
|
||||||
|
err := database.conn.QueryRowContext(
|
||||||
|
ctx,
|
||||||
|
"SELECT COUNT(*) FROM clients",
|
||||||
|
).Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf(
|
||||||
|
"get client count: %w", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQueueEntryCount returns the total number of entries
|
||||||
|
// in the client output queues.
|
||||||
|
func (database *Database) GetQueueEntryCount(
|
||||||
|
ctx context.Context,
|
||||||
|
) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
|
||||||
|
err := database.conn.QueryRowContext(
|
||||||
|
ctx,
|
||||||
|
"SELECT COUNT(*) FROM client_queues",
|
||||||
|
).Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf(
|
||||||
|
"get queue entry count: %w", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -212,6 +212,9 @@ func (hdlr *Handlers) handleCreateSession(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hdlr.stats.IncrSessions()
|
||||||
|
hdlr.stats.IncrConnections()
|
||||||
|
|
||||||
hdlr.deliverMOTD(request, clientID, sessionID, payload.Nick)
|
hdlr.deliverMOTD(request, clientID, sessionID, payload.Nick)
|
||||||
|
|
||||||
hdlr.respondJSON(writer, request, map[string]any{
|
hdlr.respondJSON(writer, request, map[string]any{
|
||||||
@@ -977,6 +980,8 @@ func (hdlr *Handlers) handlePrivmsg(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hdlr.stats.IncrMessages()
|
||||||
|
|
||||||
if strings.HasPrefix(target, "#") {
|
if strings.HasPrefix(target, "#") {
|
||||||
hdlr.handleChannelMsg(
|
hdlr.handleChannelMsg(
|
||||||
writer, request,
|
writer, request,
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"git.eeqj.de/sneak/neoirc/internal/logger"
|
"git.eeqj.de/sneak/neoirc/internal/logger"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/middleware"
|
"git.eeqj.de/sneak/neoirc/internal/middleware"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/server"
|
"git.eeqj.de/sneak/neoirc/internal/server"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/stats"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
"go.uber.org/fx/fxtest"
|
"go.uber.org/fx/fxtest"
|
||||||
)
|
)
|
||||||
@@ -90,6 +91,7 @@ func newTestServer(
|
|||||||
return cfg, nil
|
return cfg, nil
|
||||||
},
|
},
|
||||||
newTestDB,
|
newTestDB,
|
||||||
|
stats.New,
|
||||||
newTestHealthcheck,
|
newTestHealthcheck,
|
||||||
newTestMiddleware,
|
newTestMiddleware,
|
||||||
newTestHandlers,
|
newTestHandlers,
|
||||||
@@ -144,12 +146,14 @@ func newTestHealthcheck(
|
|||||||
cfg *config.Config,
|
cfg *config.Config,
|
||||||
log *logger.Logger,
|
log *logger.Logger,
|
||||||
database *db.Database,
|
database *db.Database,
|
||||||
|
tracker *stats.Tracker,
|
||||||
) (*healthcheck.Healthcheck, error) {
|
) (*healthcheck.Healthcheck, error) {
|
||||||
hcheck, err := healthcheck.New(lifecycle, healthcheck.Params{ //nolint:exhaustruct
|
hcheck, err := healthcheck.New(lifecycle, healthcheck.Params{ //nolint:exhaustruct
|
||||||
Globals: globs,
|
Globals: globs,
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
Logger: log,
|
Logger: log,
|
||||||
Database: database,
|
Database: database,
|
||||||
|
Stats: tracker,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("test healthcheck: %w", err)
|
return nil, fmt.Errorf("test healthcheck: %w", err)
|
||||||
@@ -183,6 +187,7 @@ func newTestHandlers(
|
|||||||
cfg *config.Config,
|
cfg *config.Config,
|
||||||
database *db.Database,
|
database *db.Database,
|
||||||
hcheck *healthcheck.Healthcheck,
|
hcheck *healthcheck.Healthcheck,
|
||||||
|
tracker *stats.Tracker,
|
||||||
) (*handlers.Handlers, error) {
|
) (*handlers.Handlers, error) {
|
||||||
hdlr, err := handlers.New(lifecycle, handlers.Params{ //nolint:exhaustruct
|
hdlr, err := handlers.New(lifecycle, handlers.Params{ //nolint:exhaustruct
|
||||||
Logger: log,
|
Logger: log,
|
||||||
@@ -190,6 +195,7 @@ func newTestHandlers(
|
|||||||
Config: cfg,
|
Config: cfg,
|
||||||
Database: database,
|
Database: database,
|
||||||
Healthcheck: hcheck,
|
Healthcheck: hcheck,
|
||||||
|
Stats: tracker,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("test handlers: %w", err)
|
return nil, fmt.Errorf("test handlers: %w", err)
|
||||||
@@ -1657,6 +1663,133 @@ func TestHealthcheck(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHealthcheckRuntimeStatsFields(t *testing.T) {
|
||||||
|
tserver := newTestServer(t)
|
||||||
|
|
||||||
|
resp, err := doRequest(
|
||||||
|
t,
|
||||||
|
http.MethodGet,
|
||||||
|
tserver.url("/.well-known/healthcheck.json"),
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf(
|
||||||
|
"expected 200, got %d", resp.StatusCode,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result map[string]any
|
||||||
|
|
||||||
|
decErr := json.NewDecoder(resp.Body).Decode(&result)
|
||||||
|
if decErr != nil {
|
||||||
|
t.Fatalf("decode healthcheck: %v", decErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
requiredFields := []string{
|
||||||
|
"sessions", "clients", "queuedLines",
|
||||||
|
"channels", "connectionsSinceBoot",
|
||||||
|
"sessionsSinceBoot", "messagesSinceBoot",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range requiredFields {
|
||||||
|
if _, ok := result[field]; !ok {
|
||||||
|
t.Errorf(
|
||||||
|
"missing field %q in healthcheck", field,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHealthcheckRuntimeStatsValues(t *testing.T) {
|
||||||
|
tserver := newTestServer(t)
|
||||||
|
|
||||||
|
token := tserver.createSession("statsuser")
|
||||||
|
|
||||||
|
tserver.sendCommand(token, map[string]any{
|
||||||
|
commandKey: joinCmd, toKey: "#statschan",
|
||||||
|
})
|
||||||
|
tserver.sendCommand(token, map[string]any{
|
||||||
|
commandKey: privmsgCmd,
|
||||||
|
toKey: "#statschan",
|
||||||
|
bodyKey: []string{"hello stats"},
|
||||||
|
})
|
||||||
|
|
||||||
|
result := tserver.fetchHealthcheck(t)
|
||||||
|
|
||||||
|
assertFieldGTE(t, result, "sessions", 1)
|
||||||
|
assertFieldGTE(t, result, "clients", 1)
|
||||||
|
assertFieldGTE(t, result, "channels", 1)
|
||||||
|
assertFieldGTE(t, result, "queuedLines", 0)
|
||||||
|
assertFieldGTE(t, result, "sessionsSinceBoot", 1)
|
||||||
|
assertFieldGTE(t, result, "connectionsSinceBoot", 1)
|
||||||
|
assertFieldGTE(t, result, "messagesSinceBoot", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tserver *testServer) fetchHealthcheck(
|
||||||
|
t *testing.T,
|
||||||
|
) map[string]any {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
resp, err := doRequest(
|
||||||
|
t,
|
||||||
|
http.MethodGet,
|
||||||
|
tserver.url("/.well-known/healthcheck.json"),
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf(
|
||||||
|
"expected 200, got %d", resp.StatusCode,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result map[string]any
|
||||||
|
|
||||||
|
decErr := json.NewDecoder(resp.Body).Decode(&result)
|
||||||
|
if decErr != nil {
|
||||||
|
t.Fatalf("decode healthcheck: %v", decErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertFieldGTE(
|
||||||
|
t *testing.T,
|
||||||
|
result map[string]any,
|
||||||
|
field string,
|
||||||
|
minimum float64,
|
||||||
|
) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
val, ok := result[field].(float64)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf(
|
||||||
|
"field %q: not a number (got %T)",
|
||||||
|
field, result[field],
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if val < minimum {
|
||||||
|
t.Errorf(
|
||||||
|
"expected %s >= %v, got %v",
|
||||||
|
field, minimum, val,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRegisterValid(t *testing.T) {
|
func TestRegisterValid(t *testing.T) {
|
||||||
tserver := newTestServer(t)
|
tserver := newTestServer(t)
|
||||||
|
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ func (hdlr *Handlers) handleRegister(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hdlr.stats.IncrSessions()
|
||||||
|
hdlr.stats.IncrConnections()
|
||||||
|
|
||||||
hdlr.deliverMOTD(request, clientID, sessionID, payload.Nick)
|
hdlr.deliverMOTD(request, clientID, sessionID, payload.Nick)
|
||||||
|
|
||||||
hdlr.respondJSON(writer, request, map[string]any{
|
hdlr.respondJSON(writer, request, map[string]any{
|
||||||
@@ -180,6 +183,8 @@ func (hdlr *Handlers) handleLogin(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hdlr.stats.IncrConnections()
|
||||||
|
|
||||||
hdlr.deliverMOTD(
|
hdlr.deliverMOTD(
|
||||||
request, clientID, sessionID, payload.Nick,
|
request, clientID, sessionID, payload.Nick,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"git.eeqj.de/sneak/neoirc/internal/hashcash"
|
"git.eeqj.de/sneak/neoirc/internal/hashcash"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/healthcheck"
|
"git.eeqj.de/sneak/neoirc/internal/healthcheck"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/logger"
|
"git.eeqj.de/sneak/neoirc/internal/logger"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/stats"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,6 +31,7 @@ type Params struct {
|
|||||||
Config *config.Config
|
Config *config.Config
|
||||||
Database *db.Database
|
Database *db.Database
|
||||||
Healthcheck *healthcheck.Healthcheck
|
Healthcheck *healthcheck.Healthcheck
|
||||||
|
Stats *stats.Tracker
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultIdleTimeout = 30 * 24 * time.Hour
|
const defaultIdleTimeout = 30 * 24 * time.Hour
|
||||||
@@ -41,6 +43,7 @@ type Handlers struct {
|
|||||||
hc *healthcheck.Healthcheck
|
hc *healthcheck.Healthcheck
|
||||||
broker *broker.Broker
|
broker *broker.Broker
|
||||||
hashcashVal *hashcash.Validator
|
hashcashVal *hashcash.Validator
|
||||||
|
stats *stats.Tracker
|
||||||
cancelCleanup context.CancelFunc
|
cancelCleanup context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +63,7 @@ func New(
|
|||||||
hc: params.Healthcheck,
|
hc: params.Healthcheck,
|
||||||
broker: broker.New(),
|
broker: broker.New(),
|
||||||
hashcashVal: hashcash.NewValidator(resource),
|
hashcashVal: hashcash.NewValidator(resource),
|
||||||
|
stats: params.Stats,
|
||||||
}
|
}
|
||||||
|
|
||||||
lifecycle.Append(fx.Hook{
|
lifecycle.Append(fx.Hook{
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ func (hdlr *Handlers) HandleHealthCheck() http.HandlerFunc {
|
|||||||
writer http.ResponseWriter,
|
writer http.ResponseWriter,
|
||||||
request *http.Request,
|
request *http.Request,
|
||||||
) {
|
) {
|
||||||
resp := hdlr.hc.Healthcheck()
|
resp := hdlr.hc.Healthcheck(request.Context())
|
||||||
hdlr.respondJSON(writer, request, resp, httpStatusOK)
|
hdlr.respondJSON(writer, request, resp, httpStatusOK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"git.eeqj.de/sneak/neoirc/internal/db"
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/globals"
|
"git.eeqj.de/sneak/neoirc/internal/globals"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/logger"
|
"git.eeqj.de/sneak/neoirc/internal/logger"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/stats"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,6 +22,7 @@ type Params struct {
|
|||||||
Config *config.Config
|
Config *config.Config
|
||||||
Logger *logger.Logger
|
Logger *logger.Logger
|
||||||
Database *db.Database
|
Database *db.Database
|
||||||
|
Stats *stats.Tracker
|
||||||
}
|
}
|
||||||
|
|
||||||
// Healthcheck tracks server uptime and provides health status.
|
// Healthcheck tracks server uptime and provides health status.
|
||||||
@@ -64,11 +66,22 @@ type Response struct {
|
|||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
Appname string `json:"appname"`
|
Appname string `json:"appname"`
|
||||||
Maintenance bool `json:"maintenanceMode"`
|
Maintenance bool `json:"maintenanceMode"`
|
||||||
|
|
||||||
|
// Runtime statistics.
|
||||||
|
Sessions int64 `json:"sessions"`
|
||||||
|
Clients int64 `json:"clients"`
|
||||||
|
QueuedLines int64 `json:"queuedLines"`
|
||||||
|
Channels int64 `json:"channels"`
|
||||||
|
ConnectionsSinceBoot int64 `json:"connectionsSinceBoot"`
|
||||||
|
SessionsSinceBoot int64 `json:"sessionsSinceBoot"`
|
||||||
|
MessagesSinceBoot int64 `json:"messagesSinceBoot"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Healthcheck returns the current health status of the server.
|
// Healthcheck returns the current health status of the server.
|
||||||
func (hcheck *Healthcheck) Healthcheck() *Response {
|
func (hcheck *Healthcheck) Healthcheck(
|
||||||
return &Response{
|
ctx context.Context,
|
||||||
|
) *Response {
|
||||||
|
resp := &Response{
|
||||||
Status: "ok",
|
Status: "ok",
|
||||||
Now: time.Now().UTC().Format(time.RFC3339Nano),
|
Now: time.Now().UTC().Format(time.RFC3339Nano),
|
||||||
UptimeSeconds: int64(hcheck.uptime().Seconds()),
|
UptimeSeconds: int64(hcheck.uptime().Seconds()),
|
||||||
@@ -76,6 +89,64 @@ func (hcheck *Healthcheck) Healthcheck() *Response {
|
|||||||
Appname: hcheck.params.Globals.Appname,
|
Appname: hcheck.params.Globals.Appname,
|
||||||
Version: hcheck.params.Globals.Version,
|
Version: hcheck.params.Globals.Version,
|
||||||
Maintenance: hcheck.params.Config.MaintenanceMode,
|
Maintenance: hcheck.params.Config.MaintenanceMode,
|
||||||
|
|
||||||
|
Sessions: 0,
|
||||||
|
Clients: 0,
|
||||||
|
QueuedLines: 0,
|
||||||
|
Channels: 0,
|
||||||
|
ConnectionsSinceBoot: hcheck.params.Stats.ConnectionsSinceBoot(),
|
||||||
|
SessionsSinceBoot: hcheck.params.Stats.SessionsSinceBoot(),
|
||||||
|
MessagesSinceBoot: hcheck.params.Stats.MessagesSinceBoot(),
|
||||||
|
}
|
||||||
|
|
||||||
|
hcheck.populateDBStats(ctx, resp)
|
||||||
|
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
// populateDBStats fills in database-derived counters.
|
||||||
|
func (hcheck *Healthcheck) populateDBStats(
|
||||||
|
ctx context.Context,
|
||||||
|
resp *Response,
|
||||||
|
) {
|
||||||
|
sessions, err := hcheck.params.Database.GetUserCount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
hcheck.log.Error(
|
||||||
|
"healthcheck: session count failed",
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
resp.Sessions = sessions
|
||||||
|
}
|
||||||
|
|
||||||
|
clients, err := hcheck.params.Database.GetClientCount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
hcheck.log.Error(
|
||||||
|
"healthcheck: client count failed",
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
resp.Clients = clients
|
||||||
|
}
|
||||||
|
|
||||||
|
queued, err := hcheck.params.Database.GetQueueEntryCount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
hcheck.log.Error(
|
||||||
|
"healthcheck: queue entry count failed",
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
resp.QueuedLines = queued
|
||||||
|
}
|
||||||
|
|
||||||
|
channels, err := hcheck.params.Database.GetChannelCount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
hcheck.log.Error(
|
||||||
|
"healthcheck: channel count failed",
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
resp.Channels = channels
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
52
internal/stats/stats.go
Normal file
52
internal/stats/stats.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
// Package stats tracks runtime statistics since server boot.
|
||||||
|
package stats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync/atomic"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tracker holds atomic counters for runtime statistics
|
||||||
|
// that accumulate since the server started.
|
||||||
|
type Tracker struct {
|
||||||
|
connectionsSinceBoot atomic.Int64
|
||||||
|
sessionsSinceBoot atomic.Int64
|
||||||
|
messagesSinceBoot atomic.Int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new Tracker with all counters at zero.
|
||||||
|
func New() *Tracker {
|
||||||
|
return &Tracker{} //nolint:exhaustruct // atomic fields have zero-value defaults
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrConnections increments the total connection count.
|
||||||
|
func (t *Tracker) IncrConnections() {
|
||||||
|
t.connectionsSinceBoot.Add(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrSessions increments the total session count.
|
||||||
|
func (t *Tracker) IncrSessions() {
|
||||||
|
t.sessionsSinceBoot.Add(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrMessages increments the total PRIVMSG/NOTICE count.
|
||||||
|
func (t *Tracker) IncrMessages() {
|
||||||
|
t.messagesSinceBoot.Add(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConnectionsSinceBoot returns the total number of
|
||||||
|
// client connections since boot.
|
||||||
|
func (t *Tracker) ConnectionsSinceBoot() int64 {
|
||||||
|
return t.connectionsSinceBoot.Load()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionsSinceBoot returns the total number of sessions
|
||||||
|
// created since boot.
|
||||||
|
func (t *Tracker) SessionsSinceBoot() int64 {
|
||||||
|
return t.sessionsSinceBoot.Load()
|
||||||
|
}
|
||||||
|
|
||||||
|
// MessagesSinceBoot returns the total number of
|
||||||
|
// PRIVMSG/NOTICE messages sent since boot.
|
||||||
|
func (t *Tracker) MessagesSinceBoot() int64 {
|
||||||
|
return t.messagesSinceBoot.Load()
|
||||||
|
}
|
||||||
117
internal/stats/stats_test.go
Normal file
117
internal/stats/stats_test.go
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
package stats_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/stats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tracker := stats.New()
|
||||||
|
if tracker == nil {
|
||||||
|
t.Fatal("expected non-nil tracker")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tracker.ConnectionsSinceBoot() != 0 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 0 connections, got %d",
|
||||||
|
tracker.ConnectionsSinceBoot(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tracker.SessionsSinceBoot() != 0 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 0 sessions, got %d",
|
||||||
|
tracker.SessionsSinceBoot(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tracker.MessagesSinceBoot() != 0 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 0 messages, got %d",
|
||||||
|
tracker.MessagesSinceBoot(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIncrConnections(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tracker := stats.New()
|
||||||
|
|
||||||
|
tracker.IncrConnections()
|
||||||
|
tracker.IncrConnections()
|
||||||
|
tracker.IncrConnections()
|
||||||
|
|
||||||
|
got := tracker.ConnectionsSinceBoot()
|
||||||
|
if got != 3 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 3 connections, got %d", got,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIncrSessions(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tracker := stats.New()
|
||||||
|
|
||||||
|
tracker.IncrSessions()
|
||||||
|
tracker.IncrSessions()
|
||||||
|
|
||||||
|
got := tracker.SessionsSinceBoot()
|
||||||
|
if got != 2 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 2 sessions, got %d", got,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIncrMessages(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tracker := stats.New()
|
||||||
|
|
||||||
|
tracker.IncrMessages()
|
||||||
|
|
||||||
|
got := tracker.MessagesSinceBoot()
|
||||||
|
if got != 1 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 1 message, got %d", got,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCountersAreIndependent(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tracker := stats.New()
|
||||||
|
|
||||||
|
tracker.IncrConnections()
|
||||||
|
tracker.IncrSessions()
|
||||||
|
tracker.IncrMessages()
|
||||||
|
tracker.IncrMessages()
|
||||||
|
|
||||||
|
if tracker.ConnectionsSinceBoot() != 1 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 1 connection, got %d",
|
||||||
|
tracker.ConnectionsSinceBoot(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tracker.SessionsSinceBoot() != 1 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 1 session, got %d",
|
||||||
|
tracker.SessionsSinceBoot(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tracker.MessagesSinceBoot() != 2 {
|
||||||
|
t.Errorf(
|
||||||
|
"expected 2 messages, got %d",
|
||||||
|
tracker.MessagesSinceBoot(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user