Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -269,6 +283,7 @@ function registerDependencyMetrics(result: HealthCheckResult): void {
init,
promclient,
watchDependencies,
watchDependenciesLoopOf,
collectDependencyTime,
collectRequestTime,
getAddress
Expand Down
6 changes: 6 additions & 0 deletions test/app_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
19 changes: 19 additions & 0 deletions test/test_monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
});