-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(central-dashboard): Add Prometheus metrics with prom-client (#7639)
* Expose metrics with prom-client Expose default and custom metrics with prom-client [1]. Custom metrics: - rest_http_request_duration_seconds (Histogram) - response time - rest_http_request_total (Counter) - response count - app_info (Gauge) - app information: app name and version --- [1]: https://www.npmjs.com/package/prom-client Signed-off-by: Robert Gildein <robert.gildein@canonical.com> * add simple test and apply suggestions Signed-off-by: Robert Gildein <robert.gildein@canonical.com> * apply suggestions Signed-off-by: Robert Gildein <robert.gildein@canonical.com> --------- Signed-off-by: Robert Gildein <robert.gildein@canonical.com>
- Loading branch information
Showing
7 changed files
with
218 additions
and
7 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,48 @@ | ||
import express, {Request, Response} from "express"; | ||
import client from "prom-client"; | ||
import responseTime from "response-time"; | ||
|
||
const appName = require("./../package.json").name; | ||
const appVersion = require("./../package.json").version; | ||
|
||
|
||
const restHttpRequestDuration = new client.Histogram({ | ||
name: "rest_http_request_duration_seconds", | ||
help: "REST API response time in seconds", | ||
labelNames: ["method", "path", "status"], | ||
}); | ||
|
||
|
||
const restHttpRequestTotal = new client.Counter({ | ||
name: "rest_http_request_total", | ||
help: "Total number of HTTP requests for REST API", | ||
labelNames: ["method", "status"], | ||
}); | ||
|
||
|
||
const appInfo = new client.Gauge({ | ||
name: "app_info", | ||
help: "Information about application", | ||
labelNames: ["version"], | ||
}); | ||
|
||
|
||
export function enableMetricsCollection(app: express.Application) { | ||
client.register.setDefaultLabels({ app: appName }); // set default labels | ||
client.collectDefaultMetrics(); | ||
|
||
appInfo.labels({version: appVersion}).set(1); // set value 1 for app_info with desired labels | ||
|
||
app.get("/prometheus/metrics", async (req: Request, res: Response) => { | ||
res.set("Content-Type", client.register.contentType); | ||
return res.send(await client.register.metrics()); | ||
}); | ||
|
||
app.use( | ||
responseTime((req: Request, res: Response, time: number) => { | ||
restHttpRequestTotal.labels({ method: req.method, status: res.statusCode }).inc(); | ||
restHttpRequestDuration.labels( | ||
{ method: req.method, path: req.baseUrl, status: res.statusCode }).observe(time); | ||
}), | ||
); | ||
} |
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,48 @@ | ||
import express, {Request, Response} from "express"; | ||
import {enableMetricsCollection} from "./metrics"; | ||
import client from "prom-client"; | ||
import {sendTestRequest} from './test_resources'; | ||
|
||
|
||
describe("metrics", () => { | ||
let app: express.Application; | ||
|
||
beforeEach(() => { | ||
jasmine.clock().install(); | ||
jasmine.clock().mockDate(new Date(1557705600000)); | ||
app = express(); | ||
}); | ||
|
||
describe("enableMetricsCollection", function () { | ||
it("enable metrics", function() { | ||
spyOn(client.register, "setDefaultLabels"); | ||
spyOn(client, "collectDefaultMetrics"); | ||
spyOn(app, "get"); | ||
spyOn(app, "use"); | ||
|
||
enableMetricsCollection(app); | ||
|
||
expect(client.collectDefaultMetrics).toHaveBeenCalled(); | ||
expect(client.register.setDefaultLabels).toHaveBeenCalled(); | ||
expect(app.get).toHaveBeenCalledWith("/prometheus/metrics", jasmine.any(Function)); | ||
expect(app.use).toHaveBeenCalledWith(jasmine.any(Function)); | ||
}); | ||
|
||
it("collect metrics", async () => { | ||
const appName = require("./../package.json").name; | ||
const appVersion = require("./../package.json").version; | ||
const expectedAppInfo = `app_info{version="${appVersion}",app="${appName}"} 1`; | ||
const addressInfo = app.listen(0).address(); | ||
const url = `http://localhost:${addressInfo.port}/prometheus/metrics`; | ||
|
||
enableMetricsCollection(app); | ||
|
||
const response = await sendTestRequest(url); | ||
expect(response.includes(expectedAppInfo)).toBe(true); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jasmine.clock().uninstall(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
CD_CLUSTER_DOMAIN=cluster.local | ||
CD_USERID_HEADER=kubeflow-userid | ||
CD_USERID_PREFIX= | ||
CD_REGISTRATION_FLOW=false | ||
CD_REGISTRATION_FLOW=false | ||
CD_COLLECT_METRICS=true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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