1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

task: add kill-switch for edge bulk metrics (#5758)

If the kill switch is enabled unleash returns 404 and a json body explaining why a 404 was given, encouraging users to upgrade to the most recent version of Edge.
This commit is contained in:
Christopher Kolstad 2024-01-04 11:21:48 +01:00 committed by GitHub
parent 795aa18bca
commit 4c574a1e50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 19 deletions

View File

@ -81,6 +81,7 @@ exports[`should create default config 1`] = `
"disableBulkToggle": false,
"disableMetrics": false,
"disableNotifications": false,
"edgeBulkMetricsKillSwitch": false,
"embedProxy": true,
"embedProxyFrontend": true,
"enableLicense": false,

View File

@ -1,6 +1,6 @@
import { Response } from 'express';
import Controller from '../controller';
import { IUnleashConfig, IUnleashServices } from '../../types';
import { IFlagResolver, IUnleashConfig, IUnleashServices } from '../../types';
import { Logger } from '../../logger';
import { NONE } from '../../types/permissions';
import { createResponseSchema } from '../../openapi/util/create-response-schema';
@ -33,6 +33,8 @@ export default class EdgeController extends Controller {
private clientInstanceService: ClientInstanceService;
private flagResolver: IFlagResolver;
constructor(
config: IUnleashConfig,
{
@ -54,6 +56,7 @@ export default class EdgeController extends Controller {
this.openApiService = openApiService;
this.metricsV2 = clientMetricsServiceV2;
this.clientInstanceService = clientInstanceService;
this.flagResolver = config.flagResolver;
this.route({
method: 'post',
@ -115,25 +118,37 @@ export default class EdgeController extends Controller {
req: IAuthRequest<void, void, BulkMetricsSchema>,
res: Response<void>,
): Promise<void> {
const { body, ip: clientIp } = req;
const { metrics, applications } = body;
if (!this.flagResolver.isEnabled('edgeBulkMetricsKillSwitch')) {
const { body, ip: clientIp } = req;
const { metrics, applications } = body;
try {
const promises: Promise<void>[] = [];
for (const app of applications) {
promises.push(
this.clientInstanceService.registerClient(app, clientIp),
);
try {
const promises: Promise<void>[] = [];
for (const app of applications) {
promises.push(
this.clientInstanceService.registerClient(
app,
clientIp,
),
);
}
if (metrics && metrics.length > 0) {
const data =
await clientMetricsEnvBulkSchema.validateAsync(metrics);
promises.push(this.metricsV2.registerBulkMetrics(data));
}
await Promise.all(promises);
res.status(202).end();
} catch (e) {
res.status(400).end();
}
if (metrics && metrics.length > 0) {
const data =
await clientMetricsEnvBulkSchema.validateAsync(metrics);
promises.push(this.metricsV2.registerBulkMetrics(data));
}
await Promise.all(promises);
res.status(202).end();
} catch (e) {
res.status(400).end();
} else {
// @ts-expect-error Expected result here is void, but since we want to communicate extra information in our 404, we allow this to avoid type-checking
res.status(404).json({
status: 'disabled',
reason: 'disabled by killswitch',
help: 'You should upgrade Edge to the most recent version to not lose metrics',
});
}
}
}

View File

@ -37,7 +37,8 @@ export type IFlagKey =
| 'increaseUnleashWidth'
| 'featureSearchFeedback'
| 'featureSearchFeedbackPosting'
| 'newStrategyConfigurationFeedback';
| 'newStrategyConfigurationFeedback'
| 'edgeBulkMetricsKillSwitch';
export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;
@ -168,6 +169,10 @@ const flags: IFlags = {
process.env.UNLEASH_EXPERIMENTAL_NEW_STRATEGY_CONFIGURATION_FEEDBACK,
false,
),
edgeBulkMetricsKillSwitch: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_EDGE_BULK_METRICS_KILL_SWITCH,
false,
),
};
export const defaultExperimentalOptions: IExperimentalOptions = {