mjolnir/test/integration/openMetricsTest.ts
David Teller c3cb22bf36
Very basic support for OpenMetrics (aka Prometheus) (#442)
This PR:

- creates an OpenMetrics server that enables collecting performance data from this process by e.g. a Prometheus server;
- exposes as metrics the performance of http requests with MatrixBot.

Further metrics may of course be added.
2023-01-05 08:37:54 +01:00

43 lines
1.6 KiB
TypeScript

import { strict as assert } from "assert";
import { getRequestFn } from "matrix-bot-sdk";
import { IConfig } from "../../src/config";
async function fetchMetrics(config: IConfig): Promise<string> {
if (!config.health.openMetrics?.enabled) {
throw new TypeError("openMetrics deactivated, cannot fetch");
}
let uri = new URL("http://localhost");
uri.port = `${config.health.openMetrics!.port}`;
uri.pathname = config.health.openMetrics!.endpoint;
return await new Promise((resolve, reject) =>
getRequestFn()({
method: "GET",
uri
}, (error: object, _response: any, body: string) => {
if (error) {
reject(error);
} else {
resolve(body);
}
})
);
}
describe("Test that we can read metrics using the API", function() {
it('can fetch default metrics', async function() {
console.debug("config", this.mjolnir.config);
let metrics = await fetchMetrics(this.mjolnir.config);
console.debug("Got metrics", metrics);
// Sample of Prometheus-recommended metrics.
for (let name of ["process_cpu_seconds_total"]) {
assert(metrics.includes(name), `Metrics should contain default metric \`${name}\``);
}
// Sample of metrics that we're injecting.
for (let name of ["mjolnir_performance_http_request_sum", "mjolnir_status_api_request_pass", "mjolnir_status_api_request_fail"]) {
assert(metrics.includes(name), `Metrics should contain custom metric \`${name}\``);
}
})
});