Skip to content

Commit

Permalink
Merge branch 'main' into RHIDP-2919
Browse files Browse the repository at this point in the history
  • Loading branch information
Omar-AlJaljuli authored Sep 25, 2024
2 parents 3120e8e + a6f5821 commit e1b229e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 68 deletions.
1 change: 0 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"@janus-idp/backstage-scaffolder-backend-module-annotator": "1.3.1",
"@manypkg/get-packages": "1.1.3",
"app": "*",
"express": "4.21.0",
"express-prom-bundle": "6.6.0",
"global-agent": "3.0.0",
"prom-client": "15.1.3",
Expand Down
34 changes: 6 additions & 28 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
import { statusCheckHandler } from '@backstage/backend-common';
import { createBackend } from '@backstage/backend-defaults';
import { rootHttpRouterServiceFactory } from '@backstage/backend-defaults/rootHttpRouter';
import {
dynamicPluginsFeatureDiscoveryServiceFactory,
dynamicPluginsFrontendSchemas,
dynamicPluginsSchemasServiceFactory,
dynamicPluginsServiceFactory,
} from '@backstage/backend-dynamic-feature-service';
import { PackageRoles } from '@backstage/cli-node';
import { RequestHandler } from 'express';
import * as path from 'path';
import { configureCorporateProxyAgent } from './corporate-proxy';
import { CommonJSModuleLoader } from './loader';
import { customLogger } from './logger';
import { metricsHandler } from './metrics';
import {
metricsPlugin,
healthCheckPlugin,
pluginIDProviderService,
rbacDynamicPluginsProvider,
} from './modules/rbacDynamicPluginsModule';
} from './modules';

// RHIDP-2217: adds support for corporate proxy
configureCorporateProxyAgent();

const backend = createBackend();

backend.add(
rootHttpRouterServiceFactory({
configure(context) {
let healthCheckHandler: RequestHandler | undefined;

const { app, routes, middleware } = context;
app.use(middleware.helmet());
app.use(middleware.cors());
app.use(middleware.compression());
app.use(middleware.logging());
app.use('/healthcheck', async (_, response, next) => {
if (!healthCheckHandler) {
healthCheckHandler = await statusCheckHandler();
}
healthCheckHandler(_, response, next);
});
app.use('/metrics', metricsHandler());
app.use(routes);
app.use(middleware.notFound());
app.use(middleware.error());
},
}),
);
backend.add(dynamicPluginsFeatureDiscoveryServiceFactory); // overridden version of the FeatureDiscoveryService which provides features loaded by dynamic plugins
backend.add(
dynamicPluginsServiceFactory({
Expand All @@ -70,6 +45,9 @@ backend.add(
backend.add(dynamicPluginsFrontendSchemas);
backend.add(customLogger);

backend.add(metricsPlugin);
backend.add(healthCheckPlugin);

backend.add(import('@backstage/plugin-app-backend/alpha'));
backend.add(
import('@backstage/plugin-catalog-backend-module-scaffolder-entity-model'),
Expand Down
39 changes: 0 additions & 39 deletions packages/backend/src/metrics.ts

This file was deleted.

22 changes: 22 additions & 0 deletions packages/backend/src/modules/healthcheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
coreServices,
createBackendPlugin,
} from '@backstage/backend-plugin-api';

export const healthCheckPlugin = createBackendPlugin({
pluginId: 'healthcheck',
register(reg) {
reg.registerInit({
deps: {
rootHttpRouter: coreServices.rootHttpRouter,
rootHealth: coreServices.rootHealth,
},
async init({ rootHttpRouter, rootHealth }) {
rootHttpRouter.use('/healthcheck', async (_, res) => {
const { status, payload } = await rootHealth.getLiveness();
res.status(status).json(payload);
});
},
});
},
});
4 changes: 4 additions & 0 deletions packages/backend/src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './authProvidersModule';
export * from './rbacDynamicPluginsModule';
export * from './healthcheck';
export * from './metrics';
47 changes: 47 additions & 0 deletions packages/backend/src/modules/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
coreServices,
createBackendPlugin,
} from '@backstage/backend-plugin-api';
import promBundle from 'express-prom-bundle';
import * as url from 'url';

const rootRegEx = new RegExp('^/([^/]*)/.*');
const apiRegEx = new RegExp('^/api/([^/]*)/.*');

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');
}

export const metricsPlugin = createBackendPlugin({
pluginId: 'metrics',
register(reg) {
reg.registerInit({
deps: {
rootHttpRouter: coreServices.rootHttpRouter,
},
async init({ rootHttpRouter }) {
rootHttpRouter.use(
'/metrics',
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: {} },
}),
);
},
});
},
});

0 comments on commit e1b229e

Please sign in to comment.