diff --git a/lib/monitor.ts b/lib/monitor.ts index 9e15e33..e41670f 100644 --- a/lib/monitor.ts +++ b/lib/monitor.ts @@ -9,6 +9,7 @@ export type Monitor = { init (app: express.Application, shouldCollectDefaultMetrics: boolean, buckets?: number[], version?: string, isErrorCallback?:isErrorCallback, metricsEndpoint?: string):void; promclient: typeof import("prom-client"); watchDependencies(healthCheckCallback: HealthCheckCallback):void; + watchDependenciesLoopOf(healthCheckCallback: HealthCheckCallback):void; collectDependencyTime(name: string, type: string, statusCode: number, method: string, addr: string, errorMessage: string, start: [number, number]):void; collectRequestTime(type: string, statusCode: number, addr: string, start: [number, number], errorMessage?: string): void; getAddress(request: express.Request):string; @@ -256,6 +257,19 @@ function watchDependencies(healthCheck: HealthCheckCallback) { } } +/** + * Inits a routine to register the health of the app's dependencies. + * Needs to return a valid array of HealthCheckResult. + * @param {HealthCheckCallback} healthCheck + */ +function watchDependenciesLoopOf(healthCheck: HealthCheckCallback) { + if (typeof healthCheck === 'function') { + healthCheck(registerDependencyMetrics) + } else { + console.log("[Express Monitor][Watch Dependencies Loop Of]: healthCheck callback needs to be a valid function") + } +} + /** * Registers the current metrics for a specific dependency * @param {HealthCheckResult} result the result of health checking a specific dependency @@ -269,6 +283,7 @@ function registerDependencyMetrics(result: HealthCheckResult): void { init, promclient, watchDependencies, + watchDependenciesLoopOf, collectDependencyTime, collectRequestTime, getAddress diff --git a/test/app_test.js b/test/app_test.js index 83e05d8..5306df2 100644 --- a/test/app_test.js +++ b/test/app_test.js @@ -8,6 +8,12 @@ const app = express() Monitor.init(app, true) +// inits a routine to expose health metrics with no loop +Monitor.watchDependenciesLoopOf((register) => { + register({ name: "Fake dependency 1", up: true}); + register({ name: "Fake dependency 2", up: false}); +}); + app.get('/test', (req, res) => { res.status(200) res.send('test') diff --git a/test/test_monitor.js b/test/test_monitor.js index 94dba7d..2020819 100644 --- a/test/test_monitor.js +++ b/test/test_monitor.js @@ -337,4 +337,23 @@ describe('Collect metrics middleware', () => { expect(res.text).to.include('response_size_bytes{type="http",status="200",method="GET",addr="/users/{userId}",isError="false",errorMessage=""}') }) }) + + + it('should collect healthcheck metrics', () => { + chai.request(app) + .get('/healthcheck') + .set('Content-Type', 'application/json') + .send() + .end((err) => { + if(err) console.log(err) + }) + chai.request(app) + .get('/metrics') + .set('Content-Type', 'application/json') + .send() + .end((err, res) => { + expect(res.text).to.include('dependency_up{name="Fake dependency 1"} 1') + expect(res.text).to.include('dependency_up{name="Fake dependency 2"} 0') + }) + }) }); \ No newline at end of file