Skip to content

Commit

Permalink
Rule registry bundle size (#97251)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar authored Apr 18, 2021
1 parent 1a3e033 commit fed17c2
Show file tree
Hide file tree
Showing 26 changed files with 169 additions and 110 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/apm/common/alert_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import { i18n } from '@kbn/i18n';
import { ValuesType } from 'utility-types';
import { ActionGroup } from '../../alerting/common';
import { ANOMALY_SEVERITY, ANOMALY_THRESHOLD } from '../../ml/common';
import type { ValuesType } from 'utility-types';
import type { ActionGroup } from '../../alerting/common';
import { ANOMALY_SEVERITY, ANOMALY_THRESHOLD } from './ml_constants';

export enum AlertType {
ErrorCount = 'apm.error_rate', // ErrorRate was renamed to ErrorCount but the key is kept as `error_rate` for backwards-compat.
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/common/anomaly_detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { ANOMALY_SEVERITY } from '../../ml/common';
import { ANOMALY_SEVERITY } from './ml_constants';
import {
getSeverityType,
getSeverityColor as mlGetSeverityColor,
Expand Down
24 changes: 24 additions & 0 deletions x-pack/plugins/apm/common/ml_constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

// copied from ml/common, to keep the bundle size small
export enum ANOMALY_SEVERITY {
CRITICAL = 'critical',
MAJOR = 'major',
MINOR = 'minor',
WARNING = 'warning',
LOW = 'low',
UNKNOWN = 'unknown',
}

export enum ANOMALY_THRESHOLD {
CRITICAL = 75,
MAJOR = 50,
MINOR = 25,
WARNING = 3,
LOW = 0,
}
25 changes: 0 additions & 25 deletions x-pack/plugins/apm/common/rules.ts

This file was deleted.

20 changes: 20 additions & 0 deletions x-pack/plugins/apm/common/rules/apm_rule_field_map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export const apmRuleFieldMap = {
'service.environment': {
type: 'keyword',
},
'transaction.type': {
type: 'keyword',
},
'processor.event': {
type: 'keyword',
},
} as const;

export type APMRuleFieldMap = typeof apmRuleFieldMap;
10 changes: 10 additions & 0 deletions x-pack/plugins/apm/common/rules/apm_rule_registry_settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

export const apmRuleRegistrySettings = {
name: 'apm',
};
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/common/service_health_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';
import { EuiTheme } from '../../../../src/plugins/kibana_react/common';
import { ANOMALY_SEVERITY } from '../../ml/common';
import { ANOMALY_SEVERITY } from './ml_constants';

export enum ServiceHealthStatus {
healthy = 'healthy',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@

import { i18n } from '@kbn/i18n';
import { lazy } from 'react';
import { format } from 'url';
import { stringify } from 'querystring';
import { ENVIRONMENT_ALL } from '../../../common/environment_filter_values';
import { asDuration, asPercent } from '../../../common/utils/formatters';
import { AlertType } from '../../../common/alert_types';
import { ApmRuleRegistry } from '../../plugin';
import type { ApmRuleRegistry } from '../../plugin';

const format = ({
pathname,
query,
}: {
pathname: string;
query: Record<string, any>;
}): string => {
return `${pathname}?${stringify(query)}`;
};

export function registerApmAlerts(apmRuleRegistry: ApmRuleRegistry) {
apmRuleRegistry.registerType({
Expand Down Expand Up @@ -71,7 +80,7 @@ export function registerApmAlerts(apmRuleRegistry: ApmRuleRegistry) {
'Alert when the latency of a specific transaction type in a service exceeds a defined threshold.',
}
),
format: ({ alert }) => ({
format: ({ alert, formatters: { asDuration } }) => ({
reason: i18n.translate(
'xpack.apm.alertTypes.transactionDuration.reason',
{
Expand Down Expand Up @@ -131,7 +140,7 @@ export function registerApmAlerts(apmRuleRegistry: ApmRuleRegistry) {
'Alert when the rate of transaction errors in a service exceeds a defined threshold.',
}
),
format: ({ alert }) => ({
format: ({ alert, formatters: { asPercent } }) => ({
reason: i18n.translate(
'xpack.apm.alertTypes.transactionErrorRate.reason',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { useParams } from 'react-router-dom';
import { i18n } from '@kbn/i18n';
import React from 'react';
import { ANOMALY_SEVERITY } from '../../../../../ml/common';
import { ANOMALY_SEVERITY } from '../../../../common/ml_constants';
import { useEnvironmentsFetcher } from '../../../hooks/use_environments_fetcher';
import { useUrlParams } from '../../../context/url_params_context/use_url_params';
import { ServiceAlertTrigger } from '../service_alert_trigger';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { render } from '@testing-library/react';
import React, { ReactNode } from 'react';
import { IntlProvider } from 'react-intl';
import { ANOMALY_SEVERITY } from '../../../../../ml/common';
import { ANOMALY_SEVERITY } from '../../../../common/ml_constants';
import { SelectAnomalySeverity } from './select_anomaly_severity';

function Wrapper({ children }: { children?: ReactNode }) {
Expand Down
38 changes: 20 additions & 18 deletions x-pack/plugins/apm/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
* 2.0.
*/

import { ConfigSchema } from '.';
import {
FetchDataParams,
FormatterRuleRegistry,
HasDataParams,
ObservabilityPublicSetup,
} from '../../observability/public';
import type { ConfigSchema } from '.';
import {
AppMountParameters,
CoreSetup,
Expand All @@ -20,28 +14,35 @@ import {
Plugin,
PluginInitializerContext,
} from '../../../../src/core/public';
import {
import type {
DataPublicPluginSetup,
DataPublicPluginStart,
} from '../../../../src/plugins/data/public';
import { HomePublicPluginSetup } from '../../../../src/plugins/home/public';
import {
import type { EmbeddableStart } from '../../../../src/plugins/embeddable/public';
import type { HomePublicPluginSetup } from '../../../../src/plugins/home/public';
import type {
PluginSetupContract as AlertingPluginPublicSetup,
PluginStartContract as AlertingPluginPublicStart,
} from '../../alerting/public';
import { FeaturesPluginSetup } from '../../features/public';
import { LicensingPluginSetup } from '../../licensing/public';
import {
import type { FeaturesPluginSetup } from '../../features/public';
import type { LicensingPluginSetup } from '../../licensing/public';
import type { MapsStartApi } from '../../maps/public';
import type { MlPluginSetup, MlPluginStart } from '../../ml/public';
import type {
FetchDataParams,
HasDataParams,
ObservabilityPublicSetup,
} from '../../observability/public';
import { FormatterRuleRegistry } from '../../observability/public';
import type {
TriggersAndActionsUIPublicPluginSetup,
TriggersAndActionsUIPublicPluginStart,
} from '../../triggers_actions_ui/public';
import { apmRuleRegistrySettings } from '../common/rules/apm_rule_registry_settings';
import type { APMRuleFieldMap } from '../common/rules/apm_rule_field_map';
import { registerApmAlerts } from './components/alerting/register_apm_alerts';
import { featureCatalogueEntry } from './featureCatalogueEntry';
import { toggleAppLinkInNav } from './toggleAppLinkInNav';
import { EmbeddableStart } from '../../../../src/plugins/embeddable/public';
import { registerApmAlerts } from './components/alerting/register_apm_alerts';
import { MlPluginSetup, MlPluginStart } from '../../ml/public';
import { MapsStartApi } from '../../maps/public';
import { apmRuleRegistrySettings } from '../common/rules';

export type ApmPluginSetup = ReturnType<ApmPlugin['setup']>;
export type ApmRuleRegistry = ApmPluginSetup['ruleRegistry'];
Expand Down Expand Up @@ -162,6 +163,7 @@ export class ApmPlugin implements Plugin<ApmPluginSetup, ApmPluginStart> {

const apmRuleRegistry = plugins.observability.ruleRegistry.create({
...apmRuleRegistrySettings,
fieldMap: {} as APMRuleFieldMap,
ctor: FormatterRuleRegistry,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import { registerTransactionDurationAnomalyAlertType } from './register_transaction_duration_anomaly_alert_type';
import { ANOMALY_SEVERITY } from '../../../../ml/common';
import { ANOMALY_SEVERITY } from '../../../common/ml_constants';
import { Job, MlPluginSetup } from '../../../../ml/server';
import * as GetServiceAnomalies from '../service_map/get_service_anomalies';
import { createRuleTypeMocks } from './test_utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
TRANSACTION_TYPE,
} from '../../../common/elasticsearch_fieldnames';
import { asMutableArray } from '../../../common/utils/as_mutable_array';
import { ANOMALY_SEVERITY } from '../../../../ml/common';
import { ANOMALY_SEVERITY } from '../../../common/ml_constants';
import { KibanaRequest } from '../../../../../../src/core/server';
import {
AlertType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getBucketSize } from '../../helpers/get_bucket_size';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';
import { anomalySeriesFetcher } from './fetcher';
import { getMLJobIds } from '../../service_map/get_service_anomalies';
import { ANOMALY_THRESHOLD } from '../../../../../ml/common';
import { ANOMALY_THRESHOLD } from '../../../../common/ml_constants';
import { withApmSpan } from '../../../utils/with_apm_span';

export async function getAnomalySeries({
Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import {
} from './types';
import { registerRoutes } from './routes/register_routes';
import { getGlobalApmServerRouteRepository } from './routes/get_global_apm_server_route_repository';
import { apmRuleRegistrySettings } from '../common/rules';
import { apmRuleRegistrySettings } from '../common/rules/apm_rule_registry_settings';
import { apmRuleFieldMap } from '../common/rules/apm_rule_field_map';

export type APMRuleRegistry = ReturnType<APMPlugin['setup']>['ruleRegistry'];

Expand Down Expand Up @@ -151,9 +152,10 @@ export class APMPlugin
config: await mergedConfig$.pipe(take(1)).toPromise(),
});

const apmRuleRegistry = plugins.observability.ruleRegistry.create(
apmRuleRegistrySettings
);
const apmRuleRegistry = plugins.observability.ruleRegistry.create({
...apmRuleRegistrySettings,
fieldMap: apmRuleFieldMap,
});

registerApmAlerts({
registry: apmRuleRegistry,
Expand Down
22 changes: 0 additions & 22 deletions x-pack/plugins/observability/common/observability_rule_registry.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 { ecsFieldMap, pickWithPatterns } from '../../../rule_registry/common';

export const observabilityRuleFieldMap = {
...pickWithPatterns(ecsFieldMap, 'host.name', 'service.name'),
'kibana.observability.evaluation.value': {
type: 'scaled_float' as const,
scaling_factor: 1000,
},
'kibana.observability.evaluation.threshold': {
type: 'scaled_float' as const,
scaling_factor: 1000,
},
};

export type ObservabilityRuleFieldMap = typeof observabilityRuleFieldMap;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

export const observabilityRuleRegistrySettings = {
name: 'observability',
};
3 changes: 2 additions & 1 deletion x-pack/plugins/observability/public/pages/alerts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { usePluginContext } from '../../hooks/use_plugin_context';
import { RouteParams } from '../../routes';
import { callObservabilityApi } from '../../services/call_observability_api';
import { getAbsoluteDateRange } from '../../utils/date';
import { asDuration, asPercent } from '../../../common/utils/formatters';
import { AlertsSearchBar } from './alerts_search_bar';
import { AlertsTable } from './alerts_table';

Expand Down Expand Up @@ -68,7 +69,7 @@ export function AlertsPage({ routeParams }: AlertsPageProps) {
const formatted = {
link: undefined,
reason: alert['rule.name'],
...(ruleType?.format?.({ alert }) ?? {}),
...(ruleType?.format?.({ alert, formatters: { asDuration, asPercent } }) ?? {}),
};

const parsedLink = formatted.link ? parse(formatted.link, true) : undefined;
Expand Down
Loading

0 comments on commit fed17c2

Please sign in to comment.