Skip to content

Commit

Permalink
Include alert types in SO client
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Apr 8, 2021
1 parent 36d4786 commit 31f97de
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/rule_registry/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export { RuleRegistryPluginSetupContract } from './plugin';
export { createLifecycleRuleTypeFactory } from './rule_registry/rule_type_helpers/create_lifecycle_rule_type_factory';
export { ecsFieldMap } from './generated/ecs_field_map';
export { pickWithPatterns } from './rule_registry/field_map/pick_with_patterns';
export { FieldMapOf } from './types';
export { ScopedRuleRegistryClient } from './rule_registry/create_scoped_rule_registry_client/types';

export const config = {
schema: schema.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const getRuleUuids = async ({
...(namespace ? { namespace } : {}),
};

const pitFinder = savedObjectsClient.createPointInTimeFinder(options);
const pitFinder = savedObjectsClient.createPointInTimeFinder({
...options,
});

const ruleUuids: string[] = [];

Expand All @@ -53,7 +55,8 @@ export function createScopedRuleRegistryClient<TFieldMap extends DefaultFieldMap
savedObjectsClient,
namespace,
clusterClientAdapter,
index,
indexAliasName,
indexTarget,
logger,
ruleData,
}: {
Expand All @@ -65,7 +68,8 @@ export function createScopedRuleRegistryClient<TFieldMap extends DefaultFieldMap
body: OutputOfFieldMap<TFieldMap>;
index: string;
}>;
index: string;
indexAliasName: string;
indexTarget: string;
logger: Logger;
ruleData?: {
rule: {
Expand Down Expand Up @@ -100,7 +104,7 @@ export function createScopedRuleRegistryClient<TFieldMap extends DefaultFieldMap

const response = await scopedClusterClient.asInternalUser.search({
...searchRequest,
index,
index: indexTarget,
body: {
...searchRequest.body,
query: {
Expand Down Expand Up @@ -139,7 +143,7 @@ export function createScopedRuleRegistryClient<TFieldMap extends DefaultFieldMap
throw createPathReporterError(validation);
}

clusterClientAdapter.indexDocument({ body: validation.right, index });
clusterClientAdapter.indexDocument({ body: validation.right, index: indexAliasName });
},
bulkIndex: (docs) => {
const validations = docs.map((doc) => {
Expand All @@ -161,7 +165,7 @@ export function createScopedRuleRegistryClient<TFieldMap extends DefaultFieldMap

const operations = compact(
validations.map((validation) => (isRight(validation) ? validation.right : null))
).map((doc) => ({ body: doc, index }));
).map((doc) => ({ body: doc, index: indexAliasName }));

return clusterClientAdapter.indexDocuments(operations);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function runtimeTypeFromFieldMap<TFieldMap extends FieldMap>(
fieldMap: TFieldMap
): FieldMapType<TFieldMap> {
function mapToType(fields: FieldMap) {
return mapValues(fields, (field) => {
return mapValues(fields, (field, key) => {
const type =
field.type in esFieldTypeMap
? esFieldTypeMap[field.type as keyof EsFieldTypeMap]
Expand Down
13 changes: 10 additions & 3 deletions x-pack/plugins/rule_registry/server/rule_registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ export class RuleRegistry<TFieldMap extends DefaultFieldMap> {

private getEsNames() {
const base = [this.options.kibanaIndex, this.options.name];
const indexTarget = `${base.join('-')}*`;
const indexAliasName = [...base, this.options.kibanaVersion.toLowerCase()].join('-');
const policyName = [...base, 'policy'].join('-');

return {
indexAliasName,
indexTarget,
policyName,
};
}
Expand Down Expand Up @@ -137,13 +139,15 @@ export class RuleRegistry<TFieldMap extends DefaultFieldMap> {
if (!this.options.writeEnabled) {
return undefined;
}
const { indexAliasName, indexTarget } = this.getEsNames();

return createScopedRuleRegistryClient({
savedObjectsClient: context.core.savedObjects.client,
savedObjectsClient: context.core.savedObjects.getClient({ includedHiddenTypes: ['alert'] }),
scopedClusterClient: context.core.elasticsearch.client,
clusterClientAdapter: this.esAdapter,
fieldMap: this.options.fieldMap,
index: this.getEsNames().indexAliasName,
indexAliasName,
indexTarget,
logger: this.options.logger,
});
}
Expand All @@ -153,6 +157,8 @@ export class RuleRegistry<TFieldMap extends DefaultFieldMap> {
) {
const logger = this.options.logger.get(type.id);

const { indexAliasName, indexTarget } = this.getEsNames();

this.options.alertingPluginSetupContract.registerType<
AlertTypeParams,
AlertTypeState,
Expand Down Expand Up @@ -187,7 +193,8 @@ export class RuleRegistry<TFieldMap extends DefaultFieldMap> {
scopedClusterClient: services.scopedClusterClient,
clusterClientAdapter: this.esAdapter,
fieldMap: this.options.fieldMap,
index: this.getEsNames().indexAliasName,
indexAliasName,
indexTarget,
namespace,
ruleData: {
producer,
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/rule_registry/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AlertTypeState,
} from '../../alerting/common';
import { ActionGroup, AlertExecutorOptions } from '../../alerting/server';
import { RuleRegistry } from './rule_registry';
import { ScopedRuleRegistryClient } from './rule_registry/create_scoped_rule_registry_client/types';
import { DefaultFieldMap } from './rule_registry/defaults/field_map';

Expand Down Expand Up @@ -93,3 +94,7 @@ export type RuleType<
TAdditionalRuleExecutorServices
>;
};

export type FieldMapOf<
TRuleRegistry extends RuleRegistry<any>
> = TRuleRegistry extends RuleRegistry<infer TFieldMap> ? TFieldMap : never;
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
expect(apmIndexName.split('-').includes('observability')).to.be(true);
expect(apmIndexName.split('-').includes('apm')).to.be(true);

expectSnapshot(indexNames[0]).toMatchInline(
`".kibana-alerts-observability-apm-8.0.0-000001"`
);
expect(indexNames[0].startsWith('.kibana-alerts-observability-apm')).to.be(true);

expect(get(apmIndex, 'index.mappings.properties.service.properties.environment.type')).to.be(
'keyword'
Expand Down

0 comments on commit 31f97de

Please sign in to comment.