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

[Rule Registry] Switch to _source for updating documents instead of Fields API #118245

Merged
merged 10 commits into from
Dec 9, 2021
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +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 { TechnicalRuleDataFieldName } from '../../common/technical_rule_data_field_names';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';

export interface IRuleDataClient {
indexName: string;
Expand All @@ -24,9 +24,7 @@ export interface IRuleDataClient {
export interface IRuleDataReader {
search<TSearchRequest extends ESSearchRequest>(
request: TSearchRequest
): Promise<
ESSearchResponse<Partial<Record<TechnicalRuleDataFieldName, unknown[]>>, TSearchRequest>
>;
): Promise<ESSearchResponse<Partial<ParsedTechnicalFields>, TSearchRequest>>;

getDynamicIndexPattern(target?: string): Promise<{
title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('createLifecycleExecutor', () => {
hits: {
hits: [
{
fields: {
_source: {
'@timestamp': '',
[ALERT_INSTANCE_ID]: 'TEST_ALERT_0',
[ALERT_UUID]: 'ALERT_0_UUID',
Expand All @@ -144,7 +144,7 @@ describe('createLifecycleExecutor', () => {
},
},
{
fields: {
_source: {
'@timestamp': '',
[ALERT_INSTANCE_ID]: 'TEST_ALERT_1',
[ALERT_UUID]: 'ALERT_1_UUID',
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('createLifecycleExecutor', () => {
hits: {
hits: [
{
fields: {
_source: {
'@timestamp': '',
[ALERT_INSTANCE_ID]: 'TEST_ALERT_0',
[ALERT_UUID]: 'ALERT_0_UUID',
Expand All @@ -263,7 +263,7 @@ describe('createLifecycleExecutor', () => {
},
},
{
fields: {
_source: {
'@timestamp': '',
[ALERT_INSTANCE_ID]: 'TEST_ALERT_1',
[ALERT_UUID]: 'ALERT_1_UUID',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
AlertTypeParams,
AlertTypeState,
} from '../../../alerting/server';
import { ParsedTechnicalFields, parseTechnicalFields } from '../../common/parse_technical_fields';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';
import {
ALERT_DURATION,
ALERT_END,
Expand Down Expand Up @@ -216,8 +216,6 @@ export const createLifecycleExecutor =
collapse: {
field: ALERT_UUID,
},
_source: false,
fields: [{ field: '*', include_unmapped: true }],
sort: {
[TIMESTAMP]: 'desc' as const,
},
Expand All @@ -226,13 +224,13 @@ export const createLifecycleExecutor =
});

hits.hits.forEach((hit) => {
const fields = parseTechnicalFields(hit.fields);
const indexName = hit._index;
const alertId = fields[ALERT_INSTANCE_ID];
trackedAlertsDataMap[alertId] = {
indexName,
fields,
};
const alertId = hit._source[ALERT_INSTANCE_ID];
if (alertId) {
trackedAlertsDataMap[alertId] = {
indexName: hit._index,
fields: hit._source,
};
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ALERT_UUID,
} from '@kbn/rule-data-utils';
import { loggerMock } from '@kbn/logging/mocks';
import { castArray, omit, mapValues } from 'lodash';
import { castArray, omit } from 'lodash';
import { RuleDataClient } from '../rule_data_client';
import { createRuleDataClientMock } from '../rule_data_client/rule_data_client.mock';
import { createLifecycleRuleTypeFactory } from './create_lifecycle_rule_type_factory';
Expand Down Expand Up @@ -293,14 +293,10 @@ describe('createLifecycleRuleTypeFactory', () => {
(doc: any) => !('index' in doc) && doc['service.name'] === 'opbeans-node'
) as Record<string, any>;

const stored = mapValues(lastOpbeansNodeDoc, (val) => {
return castArray(val);
});

// @ts-ignore 4.3.5 upgrade
helpers.ruleDataClientMock.getReader().search.mockResolvedValueOnce({
hits: {
hits: [{ fields: stored } as any],
hits: [{ _source: lastOpbeansNodeDoc } as any],
total: {
value: 1,
relation: 'eq',
Expand Down Expand Up @@ -378,13 +374,9 @@ describe('createLifecycleRuleTypeFactory', () => {
(doc: any) => !('index' in doc) && doc['service.name'] === 'opbeans-node'
) as Record<string, any>;

const stored = mapValues(lastOpbeansNodeDoc, (val) => {
return castArray(val);
});

helpers.ruleDataClientMock.getReader().search.mockResolvedValueOnce({
hits: {
hits: [{ fields: stored } as any],
hits: [{ _source: lastOpbeansNodeDoc } as any],
total: {
value: 1,
relation: 'eq',
Expand Down