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, "disableBulkToggle": false,
"disableMetrics": false, "disableMetrics": false,
"disableNotifications": false, "disableNotifications": false,
"edgeBulkMetricsKillSwitch": false,
"embedProxy": true, "embedProxy": true,
"embedProxyFrontend": true, "embedProxyFrontend": true,
"enableLicense": false, "enableLicense": false,

View File

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

View File

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