-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Diego Luisi <[email protected]>
- Loading branch information
1 parent
d024b13
commit 3ec78a2
Showing
3 changed files
with
47 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// packages/backend/src/metrics.ts | ||
import { useHotCleanup } from '@backstage/backend-common'; | ||
import { RequestHandler } from 'express'; | ||
import promBundle from 'express-prom-bundle'; | ||
import prom from 'prom-client'; | ||
import * as url from 'url'; | ||
|
||
const rootRegEx = new RegExp('^/([^/]*)/.*'); | ||
const apiRegEx = new RegExp('^/api/([^/]*)/.*'); | ||
|
||
export function normalizePath(req: any): string { | ||
const path = url.parse(req.originalUrl || req.url).pathname || '/'; | ||
|
||
// Capture /api/ and the plugin name | ||
if (apiRegEx.test(path)) { | ||
return path.replace(apiRegEx, '/api/$1'); | ||
} | ||
|
||
// Only the first path segment at root level | ||
return path.replace(rootRegEx, '/$1'); | ||
} | ||
|
||
/** | ||
* Adds a /metrics endpoint, register default runtime metrics and instrument the router. | ||
*/ | ||
export function metricsHandler(): RequestHandler { | ||
// We can only initialize the metrics once and have to clean them up between hot reloads | ||
useHotCleanup(module, () => prom.register.clear()); | ||
|
||
return promBundle({ | ||
includeMethod: true, | ||
includePath: true, | ||
// Using includePath alone is problematic, as it will include path labels with high | ||
// cardinality (e.g. path params). Instead we would have to template them. However, this | ||
// is difficult, as every backend plugin might use different routes. Instead we only take | ||
// the first directory of the path, to have at least an idea how each plugin performs: | ||
normalizePath, | ||
promClient: { collectDefaultMetrics: {} }, | ||
}); | ||
} |