Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alerting] Deprecated config telemetry #112609

Closed
2 changes: 1 addition & 1 deletion src/plugins/usage_collection/server/routes/stats/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function registerStatsRoute({
if (shouldGetUsage) {
const collectorsReady = await collectorSet.areAllCollectorsReady();
if (!collectorsReady) {
return res.customError({ statusCode: 503, body: { message: STATS_NOT_READY_MESSAGE } });
// return res.customError({ statusCode: 503, body: { message: STATS_NOT_READY_MESSAGE } });
chrisronline marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/actions/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ export type CustomHostSettings = TypeOf<typeof customHostSettingsSchema>;

export const configSchema = schema.object({
enabled: schema.boolean({ defaultValue: true }),
/**
* @deprecated in favor of `allowedHosts`
**/
whitelistedHosts: schema.maybe(
schema.arrayOf(
schema.oneOf([schema.string({ hostname: true }), schema.literal(AllowedHosts.Any)]),
{
defaultValue: [AllowedHosts.Any],
}
)
),
allowedHosts: schema.arrayOf(
schema.oneOf([schema.string({ hostname: true }), schema.literal(AllowedHosts.Any)]),
{
Expand Down
31 changes: 25 additions & 6 deletions x-pack/plugins/actions/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
*/
import { config } from './index';
import { applyDeprecations, configDeprecationFactory } from '@kbn/config';
import { set } from '@elastic/safer-lodash-set';

const CONFIG_PATH = 'xpack.actions';
const applyStackAlertDeprecations = (settings: Record<string, unknown> = {}) => {
const deprecations = config.deprecations!(configDeprecationFactory);
const deprecationMessages: string[] = [];
const _config = {
[CONFIG_PATH]: settings,
};
const { config: migrated } = applyDeprecations(
const _config = {};
set(_config, CONFIG_PATH, settings);
const { config: migrated, changedPaths } = applyDeprecations(
_config,
deprecations.map((deprecation) => ({
deprecation,
path: CONFIG_PATH,
})),
() =>
({ message }) =>
deprecationMessages.push(message)
({ message }) => {
deprecationMessages.push(message);
}
);
return {
messages: deprecationMessages,
changedPaths,
migrated,
};
};
Expand All @@ -40,5 +42,22 @@ describe('index', () => {
]
`);
});

it('should perform a custom set on deprecated and removed configs', () => {
jest.spyOn(configDeprecationFactory, 'renameFromRoot');

const { changedPaths } = applyStackAlertDeprecations({
whitelistedHosts: ['smtp.gmail.com'],
});

expect(configDeprecationFactory.renameFromRoot).toHaveBeenCalledWith(
'xpack.actions.whitelistedHosts',
'xpack.actions.allowedHosts'
);
expect(changedPaths).toStrictEqual({
set: ['xpack.actions.allowedHosts'],
unset: ['xpack.actions.whitelistedHosts'],
});
});
});
});
58 changes: 55 additions & 3 deletions x-pack/plugins/actions/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/
import { get } from 'lodash';
import type { PublicMethodsOf } from '@kbn/utility-types';
import { ConfigDeprecation, AddConfigDeprecation } from 'kibana/server';
import type { PublicMethodsOf, RecursiveReadonly } from '@kbn/utility-types';
import { PluginInitializerContext, PluginConfigDescriptor } from '../../../../src/core/server';
import { ActionsPlugin } from './plugin';
import { configSchema, ActionsConfig, CustomHostSettings } from './config';
Expand Down Expand Up @@ -54,10 +55,61 @@ export { ACTION_SAVED_OBJECT_TYPE } from './constants/saved_objects';

export const plugin = (initContext: PluginInitializerContext) => new ActionsPlugin(initContext);

// Use a custom copy function here so we can perserve the telemetry provided for the deprecated config
// See https://github.com/elastic/kibana/issues/112585#issuecomment-923715363
function renameFromRootAndTrackDeprecatedUsage(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont believe this is needed. please let me know if this comment makes sense: #112585 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this clarification!

{
renameFromRoot,
}: {
renameFromRoot(oldKey: string, newKey: string): ConfigDeprecation;
},
oldPath: string,
newPath: string,
renamedNewPath?: string
) {
return (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
settings: RecursiveReadonly<Record<string, any>>,
fromPath: string,
addDeprecation: AddConfigDeprecation
) => {
// const actions = get(settings, fromPath);
const value = get(settings, oldPath);
const renameFn = renameFromRoot(oldPath, newPath);

const result = renameFn(settings, fromPath, addDeprecation);

// If it is set, make sure we return custom logic to ensure the usage is tracked
if (value) {
const unsets = [];
const sets = [{ path: newPath, value }];
if (renamedNewPath) {
sets.push({ path: renamedNewPath, value });
unsets.push({ path: oldPath });
}
return {
set: sets,
unset: unsets,
};
}

return result;
};
}

export const config: PluginConfigDescriptor<ActionsConfig> = {
schema: configSchema,
deprecations: ({ renameFromRoot, unused }) => [
renameFromRoot('xpack.actions.whitelistedHosts', 'xpack.actions.allowedHosts'),
exposeToUsage: {
whitelistedHosts: true,
rejectUnauthorized: true,
proxyRejectUnauthorizedCertificates: true,
},
deprecations: ({ renameFromRoot, unused, rename }) => [
renameFromRootAndTrackDeprecatedUsage(
{ renameFromRoot },
'xpack.actions.whitelistedHosts',
'xpack.actions.allowedHosts'
),
(settings, fromPath, addDeprecation) => {
const actions = get(settings, fromPath);
const customHostSettings = actions?.customHostSettings ?? [];
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/alerting/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ describe('config validation', () => {
"interval": "5m",
"removalDelay": "1h",
},
"legacyTrackingPurposes": Object {
"healthCheck": Object {
"interval": "60m",
},
"invalidateApiKeysTask": Object {
"interval": "5m",
"removalDelay": "1h",
},
},
"maxEphemeralActionsPerAlert": 10,
}
`);
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/alerting/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ export const configSchema = schema.object({
healthCheck: schema.object({
interval: schema.string({ validate: validateDurationSchema, defaultValue: '60m' }),
}),
/*
* Do not use
*/
legacyTrackingPurposes: schema.object({
healthCheck: schema.object({
interval: schema.string({ validate: validateDurationSchema, defaultValue: '60m' }),
}),
invalidateApiKeysTask: schema.object({
interval: schema.string({ validate: validateDurationSchema, defaultValue: '5m' }),
removalDelay: schema.string({ validate: validateDurationSchema, defaultValue: '1h' }),
}),
}),
invalidateApiKeysTask: schema.object({
interval: schema.string({ validate: validateDurationSchema, defaultValue: '5m' }),
removalDelay: schema.string({ validate: validateDurationSchema, defaultValue: '1h' }),
Expand Down
52 changes: 46 additions & 6 deletions x-pack/plugins/alerting/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
*/
import { config } from './index';
import { applyDeprecations, configDeprecationFactory } from '@kbn/config';
import { set } from '@elastic/safer-lodash-set';

const CONFIG_PATH = 'xpack.alerting';
const applyStackAlertDeprecations = (settings: Record<string, unknown> = {}) => {
const applyStackAlertDeprecations = (_config: Record<string, unknown> = {}) => {
const deprecations = config.deprecations!(configDeprecationFactory);
const deprecationMessages: string[] = [];
const _config = {
[CONFIG_PATH]: settings,
};
const { config: migrated } = applyDeprecations(
// const _config = {};
// set(_config, configPath, settings);
const { config: migrated, changedPaths } = applyDeprecations(
_config,
deprecations.map((deprecation) => ({
deprecation,
Expand All @@ -27,18 +27,58 @@ const applyStackAlertDeprecations = (settings: Record<string, unknown> = {}) =>
return {
messages: deprecationMessages,
migrated,
changedPaths,
};
};

describe('index', () => {
describe('deprecations', () => {
it('should deprecate .enabled flag', () => {
const { messages } = applyStackAlertDeprecations({ enabled: false });
const { messages } = applyStackAlertDeprecations({ [CONFIG_PATH]: { enabled: false } });
expect(messages).toMatchInlineSnapshot(`
Array [
"\\"xpack.alerting.enabled\\" is deprecated. The ability to disable this plugin will be removed in 8.0.0.",
]
`);
});

it('should perform a custom set on deprecated and removed configs', () => {
jest.spyOn(configDeprecationFactory, 'renameFromRoot');

const customConfig = {};
set(customConfig, 'xpack.alerts', {
healthCheck: { interval: '30m' },
invalidateApiKeysTask: { interval: '30m', removalDelay: '1d' },
});
const { changedPaths } = applyStackAlertDeprecations(customConfig);
expect(configDeprecationFactory.renameFromRoot).toHaveBeenCalledTimes(3);
expect((configDeprecationFactory.renameFromRoot as jest.Mock).mock.calls[0]).toEqual([
'xpack.alerts.healthCheck.interval',
'xpack.alerting.healthCheck.interval',
]);
expect((configDeprecationFactory.renameFromRoot as jest.Mock).mock.calls[1]).toEqual([
'xpack.alerts.invalidateApiKeysTask.interval',
'xpack.alerting.invalidateApiKeysTask.interval',
]);
expect((configDeprecationFactory.renameFromRoot as jest.Mock).mock.calls[2]).toEqual([
'xpack.alerts.invalidateApiKeysTask.removalDelay',
'xpack.alerting.invalidateApiKeysTask.removalDelay',
]);
expect(changedPaths).toStrictEqual({
set: [
'xpack.alerting.healthCheck.interval',
'xpack.alerting.legacyTrackingPurposes.healthCheck.interval',
'xpack.alerting.invalidateApiKeysTask.interval',
'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.interval',
'xpack.alerting.invalidateApiKeysTask.removalDelay',
'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.removalDelay',
],
unset: [
'xpack.alerts.healthCheck.interval',
'xpack.alerts.invalidateApiKeysTask.interval',
'xpack.alerts.invalidateApiKeysTask.removalDelay',
],
});
});
});
});
67 changes: 61 additions & 6 deletions x-pack/plugins/alerting/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/
import { get } from 'lodash';
import type { PublicMethodsOf } from '@kbn/utility-types';
import { ConfigDeprecation, AddConfigDeprecation } from 'kibana/server';
import type { PublicMethodsOf, RecursiveReadonly } from '@kbn/utility-types';
import { RulesClient as RulesClientClass } from './rules_client';
import { PluginConfigDescriptor, PluginInitializerContext } from '../../../../src/core/server';
import { AlertingPlugin } from './plugin';
Expand Down Expand Up @@ -46,17 +47,71 @@ export {

export const plugin = (initContext: PluginInitializerContext) => new AlertingPlugin(initContext);

// Use a custom copy function here so we can perserve the telemetry provided for the deprecated config
// See https://github.com/elastic/kibana/issues/112585#issuecomment-923715363
function renameFromRootAndTrackDeprecatedUsage(
{
renameFromRoot,
}: {
renameFromRoot(oldKey: string, newKey: string): ConfigDeprecation;
},
oldPath: string,
newPath: string,
renamedNewPath?: string
) {
return (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
settings: RecursiveReadonly<Record<string, any>>,
fromPath: string,
addDeprecation: AddConfigDeprecation
) => {
// const actions = get(settings, fromPath);
const value = get(settings, oldPath);
const renameFn = renameFromRoot(oldPath, newPath);

const result = renameFn(settings, fromPath, addDeprecation);

// If it is set, make sure we return custom logic to ensure the usage is tracked
if (value) {
const unsets = [];
const sets = [{ path: newPath, value }];
if (renamedNewPath) {
sets.push({ path: renamedNewPath, value });
unsets.push({ path: oldPath });
}
return {
set: sets,
unset: unsets,
};
}

return result;
};
}

export const config: PluginConfigDescriptor<AlertsConfigType> = {
schema: configSchema,
exposeToUsage: {
legacyTrackingPurposes: true,
},
deprecations: ({ renameFromRoot }) => [
renameFromRoot('xpack.alerts.healthCheck', 'xpack.alerting.healthCheck'),
renameFromRoot(
renameFromRootAndTrackDeprecatedUsage(
{ renameFromRoot },
'xpack.alerts.healthCheck.interval',
'xpack.alerting.healthCheck.interval',
'xpack.alerting.legacyTrackingPurposes.healthCheck.interval'
),
renameFromRootAndTrackDeprecatedUsage(
{ renameFromRoot },
'xpack.alerts.invalidateApiKeysTask.interval',
'xpack.alerting.invalidateApiKeysTask.interval'
'xpack.alerting.invalidateApiKeysTask.interval',
'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.interval'
),
renameFromRoot(
renameFromRootAndTrackDeprecatedUsage(
{ renameFromRoot },
'xpack.alerts.invalidateApiKeysTask.removalDelay',
'xpack.alerting.invalidateApiKeysTask.removalDelay'
'xpack.alerting.invalidateApiKeysTask.removalDelay',
'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.removalDelay'
),
(settings, fromPath, addDeprecation) => {
const alerting = get(settings, fromPath);
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ export interface AlertsConfigType {
healthCheck: {
interval: string;
};
legacyTrackingPurposes: {
healthCheck: {
interval: string;
};
};
}

export interface AlertsConfigType {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/task_manager/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export {

export const config: PluginConfigDescriptor<TaskManagerConfig> = {
schema: configSchema,
exposeToUsage: {
max_workers: true,
},
deprecations: () => [
(settings, fromPath, addDeprecation) => {
const taskManager = get(settings, fromPath);
Expand Down