Skip to content

Commit

Permalink
task: add kill-switch for edge bulk metrics (#5758)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Christopher Kolstad authored Jan 4, 2024
1 parent 795aa18 commit 4c574a1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/lib/__snapshots__/create-config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ exports[`should create default config 1`] = `
"disableBulkToggle": false,
"disableMetrics": false,
"disableNotifications": false,
"edgeBulkMetricsKillSwitch": false,
"embedProxy": true,
"embedProxyFrontend": true,
"enableLicense": false,
Expand Down
51 changes: 33 additions & 18 deletions src/lib/routes/edge-api/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -33,6 +33,8 @@ export default class EdgeController extends Controller {

private clientInstanceService: ClientInstanceService;

private flagResolver: IFlagResolver;

constructor(
config: IUnleashConfig,
{
Expand All @@ -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',
Expand Down Expand Up @@ -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),
);
}
if (metrics && metrics.length > 0) {
const data =
await clientMetricsEnvBulkSchema.validateAsync(metrics);
promises.push(this.metricsV2.registerBulkMetrics(data));
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();
}
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',
});
}
}
}
7 changes: 6 additions & 1 deletion src/lib/types/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export type IFlagKey =
| 'increaseUnleashWidth'
| 'featureSearchFeedback'
| 'featureSearchFeedbackPosting'
| 'newStrategyConfigurationFeedback';
| 'newStrategyConfigurationFeedback'
| 'edgeBulkMetricsKillSwitch';

export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 4c574a1

Please sign in to comment.