Skip to content

Commit

Permalink
mark experimental fields (#120904)
Browse files Browse the repository at this point in the history
* temp trying out stuff

* cleanup

* use experimentalFieldMap

* fix experimental component template export name

* clean up

* remove experimental field names from kbn package

* remove experimental_rule_data_field_names

* fix typescript errors & cleanup

* remove experimental_component_template

* bring back alert rule parameters

* add experimentalRuleFieldMap to apm and uptime componentTemplates

* parse experimental fields

* fix linter problem

* parse exact fieldmap

* fix linter issues

* use exact combinator for the required fieldmap

* pass only alert experimental fields to stop overriding already parsed alerts

* remove experimental fields from observability rule type registry

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
mgiota and kibanamachine authored Dec 20, 2021
1 parent 22a3c9c commit 35b483f
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 15 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from 'src/core/server';
import { isEmpty, mapValues } from 'lodash';
import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map';
import { experimentalRuleFieldMap } from '../../rule_registry/common/assets/field_maps/experimental_rule_field_map';
import { Dataset } from '../../rule_registry/server';
import { APMConfig, APM_SERVER_FEATURE_ID } from '.';
import { UI_SETTINGS } from '../../../../src/plugins/data/common';
Expand Down Expand Up @@ -110,6 +111,7 @@ export class APMPlugin
name: 'mappings',
mappings: mappingFromFieldMap(
{
...experimentalRuleFieldMap,
[SERVICE_NAME]: {
type: 'keyword',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

import { CoreSetup, Logger } from 'src/core/server';
import { mappingFromFieldMap } from '../../../../rule_registry/common/mapping_from_field_map';
import { experimentalRuleFieldMap } from '../../../../rule_registry/common/assets/field_maps/experimental_rule_field_map';

import { Dataset, RuleRegistryPluginSetupContract } from '../../../../rule_registry/server';
import type { InfraFeatureId } from '../../../common/constants';
import { RuleRegistrationContext, RulesServiceStartDeps } from './types';
Expand All @@ -31,7 +34,7 @@ export const createRuleDataClient = ({
componentTemplates: [
{
name: 'mappings',
mappings: {},
mappings: mappingFromFieldMap(experimentalRuleFieldMap, 'strict'),
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ import {
} from '@kbn/rule-data-utils/technical_field_names';
import { ALERT_STATUS_ACTIVE } from '@kbn/rule-data-utils/alerts_as_data_status';
import type { TopAlert } from '../';
import { experimentalRuleFieldMap } from '../../../../../rule_registry/common/assets/field_maps/experimental_rule_field_map';
import { parseTechnicalFields } from '../../../../../rule_registry/common/parse_technical_fields';
import { parseExperimentalFields } from '../../../../../rule_registry/common/parse_experimental_fields';
import { asDuration, asPercent } from '../../../../common/utils/formatters';
import { ObservabilityRuleTypeRegistry } from '../../../rules/create_observability_rule_type_registry';

export const parseAlert =
(observabilityRuleTypeRegistry: ObservabilityRuleTypeRegistry) =>
(alert: Record<string, unknown>): TopAlert => {
const parsedFields = parseTechnicalFields(alert);
const experimentalFields = Object.keys(experimentalRuleFieldMap);
const alertWithExperimentalFields = experimentalFields.reduce(
(acc, key) => ({ ...acc, [key]: alert[key] }),
{}
);
const parsedFields = {
...parseTechnicalFields(alert),
...parseExperimentalFields(alertWithExperimentalFields),
};
const formatter = observabilityRuleTypeRegistry.getFormatter(parsedFields[ALERT_RULE_TYPE_ID]!);
const formatted = {
link: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { euiStyled } from '../../../../../../../../src/plugins/kibana_react/comm
import { loadAlertAggregations as loadRuleAggregations } from '../../../../../../../plugins/triggers_actions_ui/public';
import { AlertStatusFilterButton } from '../../../../../common/typings';
import { ParsedTechnicalFields } from '../../../../../../rule_registry/common/parse_technical_fields';
import { ParsedExperimentalFields } from '../../../../../../rule_registry/common/parse_experimental_fields';
import { ExperimentalBadge } from '../../../../components/shared/experimental_badge';
import { useBreadcrumbs } from '../../../../hooks/use_breadcrumbs';
import { useFetcher } from '../../../../hooks/use_fetcher';
Expand All @@ -43,7 +44,7 @@ interface RuleStatsState {
error: number;
}
export interface TopAlert {
fields: ParsedTechnicalFields;
fields: ParsedTechnicalFields & ParsedExperimentalFields;
start: number;
reason: string;
link?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 * as Fields from '../../../common/technical_rule_data_field_names';

export const experimentalRuleFieldMap = {
[Fields.ALERT_INSTANCE_ID]: { type: 'keyword', required: true },
[Fields.ALERT_EVALUATION_THRESHOLD]: { type: 'scaled_float', scaling_factor: 100 },
[Fields.ALERT_EVALUATION_VALUE]: { type: 'scaled_float', scaling_factor: 100 },
} as const;

export type ExperimentalRuleFieldMap = typeof experimentalRuleFieldMap;
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ export const technicalRuleFieldMap = {
[Fields.ALERT_RULE_PRODUCER]: { type: 'keyword', required: true },
[Fields.SPACE_IDS]: { type: 'keyword', array: true, required: true },
[Fields.ALERT_UUID]: { type: 'keyword', required: true },
[Fields.ALERT_INSTANCE_ID]: { type: 'keyword', required: true },
[Fields.ALERT_START]: { type: 'date' },
[Fields.ALERT_END]: { type: 'date' },
[Fields.ALERT_DURATION]: { type: 'long' },
[Fields.ALERT_SEVERITY]: { type: 'keyword' },
[Fields.ALERT_STATUS]: { type: 'keyword', required: true },
[Fields.ALERT_EVALUATION_THRESHOLD]: { type: 'scaled_float', scaling_factor: 100 },
[Fields.ALERT_EVALUATION_VALUE]: { type: 'scaled_float', scaling_factor: 100 },
[Fields.VERSION]: {
type: 'version',
array: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ export function runtimeTypeFromFieldMap<TFieldMap extends FieldMap>(
}

const required = pickBy(fieldMap, (field) => field.required);

return t.intersection([
t.exact(t.partial(mapToType(fieldMap))),
t.type(mapToType(required)),
Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/rule_registry/common/field_map/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
*/

export interface FieldMap {
[key: string]: { type: string; required?: boolean; array?: boolean; path?: string };
[key: string]: {
type: string;
required?: boolean;
array?: boolean;
path?: string;
};
}
28 changes: 28 additions & 0 deletions x-pack/plugins/rule_registry/common/parse_experimental_fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 { isLeft } from 'fp-ts/lib/Either';
import { PathReporter } from 'io-ts/lib/PathReporter';
import {
experimentalRuleFieldMap,
ExperimentalRuleFieldMap,
} from './assets/field_maps/experimental_rule_field_map';

import { runtimeTypeFromFieldMap } from './field_map';

const experimentalFieldRuntimeType =
runtimeTypeFromFieldMap<ExperimentalRuleFieldMap>(experimentalRuleFieldMap);

export const parseExperimentalFields = (input: unknown) => {
const validate = experimentalFieldRuntimeType.decode(input);

if (isLeft(validate)) {
throw new Error(PathReporter.report(validate).join('\n'));
}
return experimentalFieldRuntimeType.encode(validate.right);
};

export type ParsedExperimentalFields = ReturnType<typeof parseExperimentalFields>;
8 changes: 6 additions & 2 deletions x-pack/plugins/rule_registry/common/parse_technical_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

import { isLeft } from 'fp-ts/lib/Either';
import { PathReporter } from 'io-ts/lib/PathReporter';
import { technicalRuleFieldMap } from './assets/field_maps/technical_rule_field_map';
import {
technicalRuleFieldMap,
TechnicalRuleFieldMap,
} from './assets/field_maps/technical_rule_field_map';
import { runtimeTypeFromFieldMap } from './field_map';

const technicalFieldRuntimeType = runtimeTypeFromFieldMap(technicalRuleFieldMap);
const technicalFieldRuntimeType =
runtimeTypeFromFieldMap<TechnicalRuleFieldMap>(technicalRuleFieldMap);

export const parseTechnicalFields = (input: unknown) => {
const validate = technicalFieldRuntimeType.decode(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import {
} from '@kbn/rule-data-utils';
import { BASE_RAC_ALERTS_API_PATH } from '../../common/constants';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';
import { ParsedExperimentalFields } from '../../common/parse_experimental_fields';
import { getAlertByIdRoute } from './get_alert_by_id';
import { requestContextMock } from './__mocks__/request_context';
import { getReadRequest } from './__mocks__/request_responses';
import { requestMock, serverMock } from './__mocks__/server';

const getMockAlert = (): ParsedTechnicalFields => ({
const getMockAlert = (): ParsedTechnicalFields & ParsedExperimentalFields => ({
[ALERT_INSTANCE_ID]: 'fake-alert-id',
[ALERT_RULE_CATEGORY]: 'apm.error_rate',
[ALERT_RULE_CONSUMER]: 'apm',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BulkRequest, BulkResponse } from '@elastic/elasticsearch/lib/api/typesW

import { ESSearchRequest, ESSearchResponse } from 'src/core/types/elasticsearch';
import { FieldDescriptor } from 'src/plugins/data/server';
import { ParsedExperimentalFields } from '../../common/parse_experimental_fields';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';

export interface IRuleDataClient {
Expand All @@ -24,7 +25,9 @@ export interface IRuleDataClient {
export interface IRuleDataReader {
search<TSearchRequest extends ESSearchRequest>(
request: TSearchRequest
): Promise<ESSearchResponse<Partial<ParsedTechnicalFields>, TSearchRequest>>;
): Promise<
ESSearchResponse<Partial<ParsedTechnicalFields & ParsedExperimentalFields>, TSearchRequest>
>;

getDynamicIndexPattern(target?: string): Promise<{
title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
AlertTypeParams,
AlertTypeState,
} from '../../../alerting/server';
import { ParsedExperimentalFields } from '../../common/parse_experimental_fields';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';
import {
ALERT_DURATION,
Expand Down Expand Up @@ -188,7 +189,7 @@ export const createLifecycleExecutor =

const trackedAlertsDataMap: Record<
string,
{ indexName: string; fields: Partial<ParsedTechnicalFields> }
{ indexName: string; fields: Partial<ParsedTechnicalFields & ParsedExperimentalFields> }
> = {};

if (trackedAlertStates.length) {
Expand Down Expand Up @@ -252,7 +253,7 @@ export const createLifecycleExecutor =
started: commonRuleFields[TIMESTAMP],
};

const event: ParsedTechnicalFields = {
const event: ParsedTechnicalFields & ParsedExperimentalFields = {
...alertData?.fields,
...commonRuleFields,
...currentAlertData,
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/uptime/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from './lib/adapters';
import { registerUptimeSavedObjects, savedObjectsAdapter } from './lib/saved_objects/saved_objects';
import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map';
import { experimentalRuleFieldMap } from '../../rule_registry/common/assets/field_maps/experimental_rule_field_map';
import { Dataset } from '../../rule_registry/server';
import { UptimeConfig } from '../common/config';
import { SyntheticsService } from './lib/synthetics_service/synthetics_service';
Expand Down Expand Up @@ -59,7 +60,10 @@ export class Plugin implements PluginType {
componentTemplates: [
{
name: 'mappings',
mappings: mappingFromFieldMap(uptimeRuleFieldMap, 'strict'),
mappings: mappingFromFieldMap(
{ ...uptimeRuleFieldMap, ...experimentalRuleFieldMap },
'strict'
),
},
],
});
Expand Down

0 comments on commit 35b483f

Please sign in to comment.