Implement Phase 1 task 1.10 (Prometheus metrics + /healthz + /readyz)

Replaces the placeholder Metrics shim with a prom-client implementation
in src/observability/metrics.ts: all 10 Phase 1 metrics from the wiki
spec, plus nodejs_* defaults. Exposes /metrics, /healthz, /readyz over
node:http on METRICS_PORT (9090); /readyz returns 503 when Redis status
is not 'ready' or the TCP listener isn't bound.

The Metrics interface in src/core/types.ts is unchanged — adapter call
sites continue to use the same inc/observe shape. Only main.ts sees the
extended type that adds serializeMetrics().

Side effects:
- Dockerfile re-enables HEALTHCHECK pointing at /readyz, and EXPOSE 9090.
- frame-ingested log downgraded back to debug now that
  teltonika_records_published_total is scrapeable.
- 19 new unit tests covering exposition format, all metric types, and
  every HTTP endpoint path. Total now 98 passing.

Note: deploy/compose.yaml still does not expose 9090 — separate decision
about how Prometheus reaches the service (host port vs. internal scraper
on the same Docker network).
This commit is contained in:
2026-04-30 20:52:12 +02:00
parent ff9c8d67a4
commit d4a6d8f713
8 changed files with 720 additions and 27 deletions
+22 -12
View File
@@ -1,13 +1,14 @@
import type { Redis } from 'ioredis';
import type * as http from 'node:http';
import type * as net from 'node:net';
import { loadConfig } from './config/load.js';
import type { Config } from './config/load.js';
import { createLogger } from './observability/logger.js';
import { createMetrics, startMetricsServer } from './observability/metrics.js';
import { createPublisher, connectRedis } from './core/publish.js';
import { startServer } from './core/server.js';
import { createTeltonikaAdapter } from './adapters/teltonika/index.js';
import { AllowAllAuthority } from './adapters/teltonika/device-authority.js';
import type { Metrics } from './core/types.js';
// -------------------------------------------------------------------------
// Startup: validate config (fail fast on bad env), build logger, boot server
@@ -31,12 +32,8 @@ const logger = createLogger({
logger.info('tcp-ingestion starting');
// Placeholder metrics implementation — replaced in task 1.10.
// Using the Metrics interface from types.ts (no prom-client yet).
const metrics: Metrics = {
inc: (name, labels) => logger.trace({ metric: name, labels }, 'metric inc'),
observe: (name, value, labels) => logger.trace({ metric: name, value, labels }, 'metric observe'),
};
// Real prom-client metrics implementation (task 1.10).
const metrics = createMetrics();
// -------------------------------------------------------------------------
// Wire up the pipeline
@@ -65,10 +62,18 @@ async function main(): Promise<void> {
metrics,
});
// 5. Install graceful shutdown (stub — full hardening in task 1.12)
installGracefulShutdown({ server, redis, publisher, logger });
// 5. Start metrics HTTP server (task 1.10).
// readyzDeps use ioredis's synchronous `.status` field and net.Server's
// `.listening` boolean — no I/O, so these closures are always cheap.
const metricsServer = startMetricsServer(config.METRICS_PORT, metrics.serializeMetrics, {
isRedisReady: () => redis.status === 'ready',
isTcpListening: () => server.listening,
});
logger.info({ port: config.TELTONIKA_PORT }, 'tcp-ingestion ready');
// 6. Install graceful shutdown (stub — full hardening in task 1.12)
installGracefulShutdown({ server, metricsServer, redis, publisher, logger });
logger.info({ port: config.TELTONIKA_PORT, metricsPort: config.METRICS_PORT }, 'tcp-ingestion ready');
}
// -------------------------------------------------------------------------
@@ -77,13 +82,14 @@ async function main(): Promise<void> {
type ShutdownDeps = {
readonly server: net.Server;
readonly metricsServer: http.Server;
readonly redis: Redis;
readonly publisher: { drain(timeoutMs: number): Promise<void> };
readonly logger: ReturnType<typeof createLogger>;
};
function installGracefulShutdown(deps: ShutdownDeps): void {
const { server, redis, publisher, logger: log } = deps;
const { server, metricsServer, redis, publisher, logger: log } = deps;
let shuttingDown = false;
@@ -93,11 +99,15 @@ function installGracefulShutdown(deps: ShutdownDeps): void {
log.info({ signal }, 'shutdown signal received');
// Stop accepting new connections
// Stop accepting new TCP connections
server.close(() => {
log.info('TCP server closed');
});
// Close the metrics HTTP server before quitting Redis so /readyz reports
// not-ready during the drain window (task 1.12 will tighten this further).
metricsServer.close();
// Drain publisher queue then disconnect Redis
publisher
.drain(10_000)