Skip to content

Commit

Permalink
register alerts_demo & add list method to show observability rac regi…
Browse files Browse the repository at this point in the history
…stered rule types
  • Loading branch information
mgiota committed Nov 15, 2021
1 parent 1f75959 commit 6dd0d25
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/alerts_demo/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { AlertTypeParams } from '../../../x-pack/plugins/alerting/common';

export const ALERTS_DEMO_APP_ID = 'AlertsDemo';
export const ALERTS_DEMO_APP_ID = 'alerts_demo';

// always firing
export const DEFAULT_INSTANCES_TO_GENERATE = 5;
Expand Down
7 changes: 5 additions & 2 deletions examples/alerts_demo/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AlertsDemoClientSetupDeps,
AlertsDemoClientStartDeps,
} from './types';
import { ALERTS_DEMO_APP_ID } from '../common/constants';
import { PLUGIN_NAME } from '../common';
import { DEFAULT_APP_CATEGORIES } from '../../../src/core/public';
// import { getAlertType as getAlwaysFiringAlertType } from './alert_types/always_firing';
Expand Down Expand Up @@ -47,9 +48,11 @@ export class AlertsDemoPlugin implements AlertsDemoPluginClass {
pluginsSetup.observability.observabilityRuleTypeRegistry.register(
createAlwaysFiringAlertType()
);

// console.log(pluginsSetup.observability.observabilityRuleTypeRegistry.list(), '!!list');
// pluginsSetup.triggersActionsUi.ruleTypeRegistry.register(getAlwaysFiringAlertType());
pluginsSetup.developerExamples.register({
appId: 'alertsDemo',
appId: ALERTS_DEMO_APP_ID,
title: 'Alerts Demo example',
description: 'This alerting example walks you through how to set up a new rule.',
links: [
Expand All @@ -64,7 +67,7 @@ export class AlertsDemoPlugin implements AlertsDemoPluginClass {
});
// Register an application into the side navigation menu
core.application.register({
id: 'alertsDemo',
id: ALERTS_DEMO_APP_ID,
title: PLUGIN_NAME,
category: DEFAULT_APP_CATEGORIES.observability,
async mount(params: AppMountParameters) {
Expand Down
8 changes: 6 additions & 2 deletions examples/alerts_demo/server/alert_types/always_firing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const createAlertsDemoExecutor = (libs: BackendLibs) =>
>(async function ({
services,
params: { instances = DEFAULT_INSTANCES_TO_GENERATE, thresholds },
state,
}) {
const { alertWithLifecycle } = services;
const alertInstanceFactory: AlertInstanceFactory = (id, reason) =>
Expand All @@ -80,12 +81,15 @@ export const createAlertsDemoExecutor = (libs: BackendLibs) =>
[ALERT_REASON]: reason,
},
});

const count = (state.count ?? 0) + 1;
range(instances)
.map(() => uuid.v4())
.forEach((id: string) => {
const tShirtSize = getTShirtSizeByIdAndThreshold(id, thresholds);
const alertInstance = alertInstanceFactory(id, tShirtSize);
alertInstance.scheduleActions(tShirtSize);
alertInstanceFactory(id, tShirtSize)
.replaceState({ triggerdOnCycle: count })
.scheduleActions(tShirtSize);
});
});

Expand Down
2 changes: 1 addition & 1 deletion examples/alerts_demo/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class AlertsDemoPlugin implements Plugin<void, void, AlertsDemoServerSetu

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
this.rules = new RulesService('logs', 'observability.logs', this.logger);
this.rules = new RulesService(ALERTS_DEMO_APP_ID, 'observability.alerts_demo', this.logger);
}

public setup(core: CoreSetup, { alerting, features, ruleRegistry }: AlertsDemoServerSetupDeps) {
Expand Down
4 changes: 2 additions & 2 deletions examples/alerts_demo/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export interface RulesServiceSetup {
createLifecycleRuleExecutor: LifecycleRuleExecutorCreator;
}

export type RuleRegistrationContext = 'observability.logs';
export type RuleRegistrationContext = 'observability.alerts_demo';

export interface BackendLibs {
rules: RulesServiceSetup;
}

export type DemoFeatureId = 'logs'; // TODO must be one of the allowed consumers
export type DemoFeatureId = 'alerts_demo'; // TODO must be one of the allowed consumers
1 change: 1 addition & 0 deletions packages/kbn-rule-data-utils/src/alerts_as_data_rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const AlertConsumers = {
OBSERVABILITY: 'observability',
SIEM: 'siem',
UPTIME: 'uptime',
ALERTS_DEMO: 'alerts_demo',
} as const;
export type AlertConsumers = typeof AlertConsumers[keyof typeof AlertConsumers];
export type STATUS_VALUES = 'open' | 'acknowledged' | 'closed' | 'in-progress'; // TODO: remove 'in-progress' after migration to 'acknowledged'
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/observability/public/pages/alerts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function AlertsPage() {
'observability.logs',
'observability.metrics',
'observability.uptime',
'observability.alerts_demo',
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ export interface ObservabilityRuleTypeModel<Params extends AlertTypeParams = Ale

export function createObservabilityRuleTypeRegistry(ruleTypeRegistry: RuleTypeRegistryContract) {
const formatters: Array<{ typeId: string; fn: ObservabilityRuleTypeFormatter }> = [];

const registeredRuleTypes: string[] = [];
return {
list: () => {
return registeredRuleTypes;
},
register: (type: ObservabilityRuleTypeModel<any>) => {
const { format, ...rest } = type;
registeredRuleTypes.push(type.id);
formatters.push({ typeId: type.id, fn: format });
ruleTypeRegistry.register(rest);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { CoreStart } from 'kibana/public';
import { Alert } from '../../../alerting/common';

const allowedConsumers = ['apm', 'uptime', 'logs', 'infrastructure', 'alerts'];
const allowedConsumers = ['apm', 'uptime', 'logs', 'infrastructure', 'alerts', 'alerts_demo'];

export async function getObservabilityAlerts({ core }: { core: CoreStart }) {
try {
Expand Down

0 comments on commit 6dd0d25

Please sign in to comment.