Skip to content

Commit

Permalink
[ResponseOps][Alerting] Ping the response-ops team whenever a new rul…
Browse files Browse the repository at this point in the history
…e type is registered (#144424)

* Checking registered rule types

* Adding comment

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
ymao1 and kibanamachine authored Nov 3, 2022
1 parent 4794970 commit 360d5d7
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const createSetupMock = () => {
const createStartMock = () => {
const mock: jest.Mocked<PluginStartContract> = {
listTypes: jest.fn(),
getAllTypes: jest.fn(),
getAlertingAuthorizationWithRequest: jest.fn(),
getRulesClientWithRequest: jest.fn().mockResolvedValue(rulesClientMock.create()),
getFrameworkHealth: jest.fn(),
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export interface PluginSetupContract {
export interface PluginStartContract {
listTypes: RuleTypeRegistry['list'];

getAllTypes: RuleTypeRegistry['getAllTypes'];

getRulesClientWithRequest(request: KibanaRequest): RulesClientApi;

getAlertingAuthorizationWithRequest(
Expand Down Expand Up @@ -464,6 +466,7 @@ export class AlertingPlugin {

return {
listTypes: ruleTypeRegistry!.list.bind(this.ruleTypeRegistry!),
getAllTypes: ruleTypeRegistry!.getAllTypes.bind(this.ruleTypeRegistry!),
getAlertingAuthorizationWithRequest,
getRulesClientWithRequest,
getFrameworkHealth: async () =>
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/rule_type_registry.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const createRuleTypeRegistryMock = () => {
register: jest.fn(),
get: jest.fn(),
list: jest.fn(),
getAllTypes: jest.fn(),
ensureRuleTypeEnabled: jest.fn(),
};
return mocked;
Expand Down
31 changes: 31 additions & 0 deletions x-pack/plugins/alerting/server/rule_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,37 @@ describe('Create Lifecycle', () => {
});
});

describe('getAllTypes()', () => {
test('should return empty when nothing is registered', () => {
const registry = new RuleTypeRegistry(ruleTypeRegistryParams);
const result = registry.getAllTypes();
expect(result).toEqual([]);
});

test('should return list of registered type ids', () => {
const registry = new RuleTypeRegistry(ruleTypeRegistryParams);
registry.register({
id: 'test',
name: 'Test',
actionGroups: [
{
id: 'testActionGroup',
name: 'Test Action Group',
},
],
defaultActionGroupId: 'testActionGroup',
doesSetRecoveryContext: false,
isExportable: true,
ruleTaskTimeout: '20m',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
});
const result = registry.getAllTypes();
expect(result).toEqual(['test']);
});
});

describe('ensureRuleTypeEnabled', () => {
let ruleTypeRegistry: RuleTypeRegistry;

Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/alerting/server/rule_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ export class RuleTypeRegistry {
)
);
}

public getAllTypes(): string[] {
return [...this.ruleTypes.keys()];
}
}

function normalizedActionVariables(actionVariables: RuleType['actionVariables']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import { Plugin, CoreSetup, CoreStart, Logger, PluginInitializerContext } from '@kbn/core/server';
import { firstValueFrom, Subject } from 'rxjs';
import { PluginSetupContract as ActionsPluginSetup } from '@kbn/actions-plugin/server/plugin';
import { PluginSetupContract as AlertingPluginSetup } from '@kbn/alerting-plugin/server/plugin';
import {
PluginStartContract as AlertingPluginsStart,
PluginSetupContract as AlertingPluginSetup,
} from '@kbn/alerting-plugin/server/plugin';
import {
TaskManagerSetupContract,
TaskManagerStartContract,
Expand All @@ -30,6 +33,7 @@ export interface FixtureSetupDeps {
}

export interface FixtureStartDeps {
alerting: AlertingPluginsStart;
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
security?: SecurityPluginStart;
spaces?: SpacesPluginStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ export function defineRoutes(
{ logger }: { logger: Logger }
) {
const router = core.http.createRouter();
router.get(
{
path: '/api/alerts_fixture/registered_rule_types',
validate: {},
},
async (
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> => {
try {
const [_, { alerting }] = await core.getStartServices();
return res.ok({
body: alerting.getAllTypes(),
});
} catch (err) {
return res.badRequest({ body: err });
}
}
);
router.put(
{
path: '/api/alerts_fixture/{id}/replace_api_key',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../common/ftr_provider_context';

// eslint-disable-next-line import/no-default-export
export default function createRegisteredRuleTypeTests({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

// This test is intended to fail when new rule types are registered.
// To resolve, add the new rule type ID to this list. This will trigger
// a CODEOWNERS review by Response Ops.
describe('check registered rule types', () => {
it('should list all registered rule types', async () => {
const registeredRuleTypes = await supertest
.get('/api/alerts_fixture/registered_rule_types')
.expect(200)
.then((response) => response.body);

expect(
registeredRuleTypes.filter((ruleType: string) => !ruleType.startsWith('test.'))
).to.eql([
'example.always-firing',
'transform_health',
'.index-threshold',
'.geo-containment',
'.es-query',
'xpack.ml.anomaly_detection_alert',
'xpack.ml.anomaly_detection_jobs_health',
'xpack.uptime.alerts.monitorStatus',
'xpack.uptime.alerts.tlsCertificate',
'xpack.uptime.alerts.durationAnomaly',
'xpack.uptime.alerts.tls',
'siem.eqlRule',
'siem.savedQueryRule',
'siem.indicatorRule',
'siem.mlRule',
'siem.queryRule',
'siem.thresholdRule',
'siem.newTermsRule',
'siem.notifications',
'metrics.alert.anomaly',
'logs.alert.document.count',
'metrics.alert.inventory.threshold',
'metrics.alert.threshold',
'monitoring_alert_cluster_health',
'monitoring_alert_license_expiration',
'monitoring_alert_cpu_usage',
'monitoring_alert_missing_monitoring_data',
'monitoring_alert_disk_usage',
'monitoring_alert_thread_pool_search_rejections',
'monitoring_alert_thread_pool_write_rejections',
'monitoring_alert_jvm_memory_usage',
'monitoring_alert_nodes_changed',
'monitoring_alert_logstash_version_mismatch',
'monitoring_alert_kibana_version_mismatch',
'monitoring_alert_elasticsearch_version_mismatch',
'monitoring_ccr_read_exceptions',
'monitoring_shard_size',
'apm.transaction_duration',
'apm.anomaly',
'apm.error_rate',
'apm.transaction_error_rate',
]);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function alertingTests({ loadTestFile, getService }: FtrProviderC
loadTestFile(require.resolve('./capped_action_type'));
loadTestFile(require.resolve('./scheduled_task_id'));
loadTestFile(require.resolve('./run_soon'));
loadTestFile(require.resolve('./check_registered_rule_types'));
// Do not place test files here, due to https://github.com/elastic/kibana/issues/123059

// note that this test will destroy existing spaces
Expand Down

0 comments on commit 360d5d7

Please sign in to comment.