Skip to content

Commit

Permalink
feat: add basic health check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
thaaddeus committed Apr 26, 2022
1 parent 99aa496 commit 2292ecf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/http/healthCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IncomingMessage, ServerResponse } from 'http'
const BYTES_IN_MB = 1024 * 1024

export const healthCheck = (_: IncomingMessage, res: ServerResponse) => {
res.setHeader('Content-Type', 'application/json')

try {
const memUsage = process.memoryUsage()

const message = {
status: 'Healthy',
uptimeHours: Number((process.uptime() / (60 * 60)).toFixed(2)),
timestampMs: Date.now(),
memoryInfo: {
rssMB: Number((memUsage.rss / BYTES_IN_MB).toFixed(1)),
heapTotalMB: Number((memUsage.heapTotal / BYTES_IN_MB).toFixed(1)),
heapUsedMB: Number((memUsage.heapUsed / BYTES_IN_MB).toFixed(1)),
externalMB: Number((memUsage.external / BYTES_IN_MB).toFixed(1))
}
}

res.end(JSON.stringify(message))
} catch {
if (!res.finished) {
res.statusCode = 500

res.end(
JSON.stringify({
message: 'Unhealthy'
})
)
}
}
}
1 change: 1 addition & 0 deletions src/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './replay'
export * from './replaynormalized'
export * from './healthCheck'
3 changes: 2 additions & 1 deletion src/tardismachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import findMyWay from 'find-my-way'
import http from 'http'
import { clearCache, init } from 'tardis-dev'
import { App, DISABLED, TemplatedApp, WebSocket } from 'uWebSockets.js'
import { replayHttp, replayNormalizedHttp } from './http'
import { replayHttp, replayNormalizedHttp, healthCheck } from './http'
import { replayNormalizedWS, replayWS, streamNormalizedWS } from './ws'

const pkg = require('../package.json')
Expand All @@ -29,6 +29,7 @@ export class TardisMachine {

router.on('GET', '/replay', replayHttp)
router.on('GET', '/replay-normalized', replayNormalizedHttp)
router.on('GET', '/health-check', healthCheck)

const wsRoutes = {
'/ws-replay': replayWS,
Expand Down

0 comments on commit 2292ecf

Please sign in to comment.