-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bff): add health checks (#1099)
- Loading branch information
Showing
5 changed files
with
217 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { setTimeout } from 'node:timers/promises'; | ||
import { logger } from '@digdir/dialogporten-node-logger'; | ||
import axios from 'axios'; | ||
import type { FastifyPluginAsync } from 'fastify'; | ||
import fp from 'fastify-plugin'; | ||
import config from '../config.ts'; | ||
import { dataSource } from '../db.ts'; | ||
import redisClient from '../redisClient.ts'; | ||
|
||
/** | ||
* Health Check System | ||
* | ||
* - Provides a '/api/health' endpoint that: | ||
* 1. Runs all health checks concurrently with timeouts | ||
* 2. Calculates overall status ('ok', 'error', 'degraded') | ||
* 3. Measures total latency | ||
* 4. Returns JSON with overall status, individual check results, and latency | ||
* - Handles errors and returns 503 status if checks fail | ||
*/ | ||
|
||
interface Props { | ||
version: string; | ||
} | ||
|
||
interface HealthCheckResult { | ||
status: 'ok' | 'error' | 'timeout'; | ||
detail?: string; | ||
latency: number; | ||
} | ||
|
||
interface HealthChecksResponse { | ||
status: 'ok' | 'error' | 'degraded'; | ||
healthChecks: Record<string, HealthCheckResult>; | ||
latency: number; | ||
} | ||
|
||
interface HealthCheck { | ||
name: string; | ||
checkFn: () => Promise<{ status: 'ok' | 'error'; detail?: string }>; | ||
} | ||
|
||
const HEALTH_CHECK_TIMEOUT = 60000; | ||
|
||
const healthCheckList: HealthCheck[] = [ | ||
{ | ||
name: 'postgresql', | ||
checkFn: async () => { | ||
try { | ||
if (!dataSource!.isInitialized) { | ||
return { status: 'error', detail: 'PostgreSQL not connected' }; | ||
} | ||
await dataSource!.query('SELECT 1'); | ||
return { status: 'ok' }; | ||
} catch (error) { | ||
logger.error(error, 'PostgreSQL health check failed'); | ||
return { status: 'error', detail: 'PostgreSQL connection failed' }; | ||
} | ||
}, | ||
}, | ||
{ | ||
name: 'redis', | ||
checkFn: async () => { | ||
try { | ||
await redisClient.ping(); | ||
return { status: 'ok' }; | ||
} catch (error) { | ||
logger.error(error, 'Redis health check failed'); | ||
return { status: 'error', detail: 'Redis connection failed' }; | ||
} | ||
}, | ||
}, | ||
{ | ||
name: 'oidc', | ||
checkFn: async () => { | ||
try { | ||
// todo: change to a URL we can use to check secret id and secret key | ||
await axios.get(`https://${config.oidc_url}/.well-known/openid-configuration`); | ||
return { status: 'ok' }; | ||
} catch (error) { | ||
logger.error(error, 'OIDC health check failed'); | ||
return { status: 'error', detail: 'OIDC URL unreachable' }; | ||
} | ||
}, | ||
}, | ||
// ... add more health checks here ... | ||
]; | ||
|
||
const performCheck = async ( | ||
name: string, | ||
checkFn: () => Promise<{ status: 'ok' | 'error'; detail?: string }>, | ||
): Promise<HealthCheckResult> => { | ||
const start = Date.now(); | ||
return Promise.race([ | ||
checkFn().then( | ||
(result): HealthCheckResult => ({ | ||
...result, | ||
latency: Date.now() - start, | ||
}), | ||
), | ||
setTimeout(HEALTH_CHECK_TIMEOUT).then( | ||
(): HealthCheckResult => ({ | ||
status: 'timeout', | ||
detail: `${name} timed out`, | ||
latency: HEALTH_CHECK_TIMEOUT, | ||
}), | ||
), | ||
]); | ||
}; | ||
|
||
const plugin: FastifyPluginAsync<Props> = async (fastify) => { | ||
fastify.get('/api/health', async (req, reply) => { | ||
const overallStart = Date.now(); | ||
|
||
try { | ||
const healthChecks: Record<string, HealthCheckResult> = await Promise.all( | ||
healthCheckList.map(async ({ name, checkFn }) => { | ||
const result = await performCheck(name, checkFn); | ||
return [name, result] as const; | ||
}), | ||
).then((results) => Object.fromEntries(results)); | ||
|
||
const overallStatus: HealthChecksResponse['status'] = Object.values(healthChecks).every( | ||
(check) => check.status === 'ok', | ||
) | ||
? 'ok' | ||
: Object.values(healthChecks).some((check) => check.status === 'error' || check.status === 'timeout') | ||
? 'error' | ||
: 'degraded'; | ||
|
||
const latency = Date.now() - overallStart; | ||
|
||
reply.status(200).send({ status: overallStatus, healthChecks, latency }); | ||
} catch (error) { | ||
const errorMsg = 'Health check endpoint failed'; | ||
logger.error(error, errorMsg); | ||
reply.status(503).send({ error: errorMsg }); | ||
} | ||
}); | ||
}; | ||
|
||
export default fp(plugin, { | ||
fastify: '4.x', | ||
name: 'azure-healthprobs', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { logger } from '@digdir/dialogporten-node-logger'; | ||
import Redis from 'ioredis'; | ||
import config from './config.ts'; | ||
|
||
const redisClient = new Redis.default(config.redisConnectionString, { | ||
enableAutoPipelining: true, | ||
}); | ||
|
||
redisClient.on('error', (err) => { | ||
logger.error(err, 'Redis Client Error'); | ||
}); | ||
|
||
redisClient.on('connect', () => { | ||
logger.info('Redis Client Connected'); | ||
}); | ||
|
||
redisClient.on('ready', () => { | ||
logger.info('Redis Client Ready'); | ||
}); | ||
|
||
redisClient.on('close', () => { | ||
logger.info('Redis Client Closed'); | ||
}); | ||
|
||
redisClient.on('reconnecting', () => { | ||
logger.info('Redis Client Reconnecting'); | ||
}); | ||
|
||
export default redisClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters