dependency-watchdog
Zero-dependency health monitoring for the things your service depends on. Register external APIs and databases, and get timeout detection, failure-rate and response-time tracking, an overall health status, and a ready-made Express /health endpoint.
Requires Node 18+ (uses built-in fetch and AbortController).
Install
npm install dependency-watchdog
Quick start (Express)
const express = require('express');
const { createWatchdog, checks } = require('dependency-watchdog');
const { Pool } = require('pg');
const pool = new Pool();
const app = express();
const watchdog = createWatchdog({ name: 'orders-service', timeoutMs: 3000 });
watchdog
.addDependency('payments-api', checks.http('https://api.example.com/ping'), { type: 'api' })
.addDependency('postgres', checks.postgres(pool), { type: 'database' })
.addDependency('email', checks.http('https://mail.example.com/status'), {
type: 'api',
critical: false, // if this is down, the service is "degraded", not "unhealthy"
});
watchdog.start(30000); // optional: poll in the background every 30s
app.get('/health', watchdog.middleware());
app.listen(3000);
GET /health returns 200 when healthy or degraded and 503 when unhealthy:
{
"name": "orders-service",
"status": "degraded",
"timestamp": "2026-11-23T17:20:00.000Z",
"uptimeSeconds": 3600,
"dependencies": {
"postgres": {
"type": "database",
"critical": true,
"status": "healthy",
"samples": 100,
"failureRate": 0,
"avgResponseMs": 4.2,
"p95ResponseMs": 9.1,
"consecutiveFailures": 0,
"lastError": null
},
"email": { "status": "unhealthy", "critical": false, "failureRate": 0.8, "timeouts": 12 }
}
}
(Trimmed for readability. Each dependency also reports min/max/last response time, totals, and last check/success/failure timestamps.)
How status is decided
Per dependency, over a rolling window of the last windowSize checks (default 100):
| Status |
Condition |
unhealthy |
unhealthyConsecutiveFailures failures in a row (default 3), or the last check failed and failure rate ≥ unhealthyFailureRate (default 0.5) |
degraded |
last check failed, or failure rate ≥ degradedFailureRate (default 0.1), or average response time > slowMs (default 1000) |
healthy |
none of the above |
unknown |
no checks run yet |
Overall: unhealthy if any critical dependency is unhealthy; degraded if anything is degraded or a non-critical dependency is unhealthy; otherwise healthy.
Latency numbers (avg/min/max/p95) are computed from successful checks only; failures and timeouts are counted separately.
Timeouts
Every check runs under a timeout (timeoutMs, default 5000, overridable per dependency). When it fires the check is recorded as a timeout failure and the AbortSignal passed to your check is aborted so it can cancel the underlying request.
Built-in checks
All take your own client, so the package adds no dependencies.
checks.http(url, { method, headers, body, expectedStatus }) // expectedStatus: number | number[] | (status) => boolean
checks.tcp(host, port)
checks.postgres(pool) // pg Pool/Client -> SELECT 1
checks.mysql(pool) // mysql2/promise -> SELECT 1
checks.mongo(dbOrClient) // Db or MongoClient -> { ping: 1 }
checks.redis(client) // node-redis/ioredis -> PING
checks.prisma(client) // Prisma -> SELECT 1
checks.database(fn) // any async function
Or write your own: any function that throws, rejects, or returns false is a failure.
watchdog.addDependency('queue', async ({ signal }) => {
await myQueue.ping({ signal });
});
Options
createWatchdog({
name: 'service',
windowSize: 100,
timeoutMs: 5000,
slowMs: 1000,
degradedFailureRate: 0.1,
unhealthyFailureRate: 0.5,
unhealthyConsecutiveFailures: 3,
cacheMs: 5000, // /health reuses results younger than this so probes can't hammer your dependencies
});
watchdog.middleware(opts):
details (default true): set false for a minimal { status, timestamp } response.
includeErrors (default false): raw error messages can contain hostnames or credentials, so they are redacted unless you opt in.
cacheMs: override the constructor value.
degradedStatusCode (default 200): e.g. set 207 or 503 if your load balancer should react to degraded.
API
addDependency(name, check, { type, critical, timeoutMs, slowMs }), removeDependency(name)
checkOne(name), checkAll() run checks now
getReport({ includeErrors }) returns the latest data without running checks
start(intervalMs), stop() background polling (timers never keep the process alive)
- Events:
watchdog.on('check', ...), watchdog.on('statusChange', ({ dependency, from, to }) => ...) (dependency is null for overall status)
Works with Express, Connect, and plain http servers. TypeScript types are included.
License
MIT