[Alerting] Fixed Telemetry task: "Cannot read properties of undefined" due to the missing shard on Kibana start (#120438)

* [Alerting] Fixed Telemetry task: "Cannot read properties of undefined" due to the missing shard on Kibana start

* fixed update errors

* fixed tests

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Yuliia Naumenko 2021-12-14 10:12:43 -08:00 committed by GitHub
parent af960b61ff
commit 409387a73b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 19 deletions

View file

@ -267,13 +267,16 @@ export class ActionsPlugin implements Plugin<PluginSetupContract, PluginStartCon
this.createRouteHandlerContext(core, this.kibanaIndex)
);
if (usageCollection) {
const eventLogIndex = this.eventLogService.getIndexPattern();
const kibanaIndex = this.kibanaIndex;
initializeActionsTelemetry(
this.telemetryLogger,
plugins.taskManager,
core,
this.kibanaIndex,
kibanaIndex,
this.preconfiguredActions,
this.eventLogService
eventLogIndex
);
}
@ -433,7 +436,9 @@ export class ActionsPlugin implements Plugin<PluginSetupContract, PluginStartCon
this.getUnsecuredSavedObjectsClient(core.savedObjects, request),
});
scheduleActionsTelemetry(this.telemetryLogger, plugins.taskManager);
this.eventLogService!.isEsContextReady().then(() => {
scheduleActionsTelemetry(this.telemetryLogger, plugins.taskManager);
});
if (this.actionsConfig.preconfiguredAlertHistoryEsIndex) {
createAlertHistoryIndexTemplate({

View file

@ -7,7 +7,6 @@
import { Logger, CoreSetup } from 'kibana/server';
import moment from 'moment';
import { IEventLogService } from '../../../event_log/server';
import {
RunContext,
TaskManagerSetupContract,
@ -26,7 +25,7 @@ export function initializeActionsTelemetry(
core: CoreSetup,
kibanaIndex: string,
preconfiguredActions: PreConfiguredAction[],
eventLog: IEventLogService
eventLogIndex: string
) {
registerActionsTelemetryTask(
logger,
@ -34,7 +33,7 @@ export function initializeActionsTelemetry(
core,
kibanaIndex,
preconfiguredActions,
eventLog
eventLogIndex
);
}
@ -48,7 +47,7 @@ function registerActionsTelemetryTask(
core: CoreSetup,
kibanaIndex: string,
preconfiguredActions: PreConfiguredAction[],
eventLog: IEventLogService
eventLogIndex: string
) {
taskManager.registerTaskDefinitions({
[TELEMETRY_TASK_TYPE]: {
@ -59,7 +58,7 @@ function registerActionsTelemetryTask(
core,
kibanaIndex,
preconfiguredActions,
eventLog
eventLogIndex
),
},
});
@ -83,11 +82,10 @@ export function telemetryTaskRunner(
core: CoreSetup,
kibanaIndex: string,
preconfiguredActions: PreConfiguredAction[],
eventLog: IEventLogService
eventLogIndex: string
) {
return ({ taskInstance }: RunContext) => {
const { state } = taskInstance;
const eventLogIndex = eventLog.getIndexPattern();
const getEsClient = () =>
core.getStartServices().then(
([

View file

@ -212,12 +212,13 @@ export class AlertingPlugin {
usageCollection,
core.getStartServices().then(([_, { taskManager }]) => taskManager)
);
const eventLogIndex = this.eventLogService.getIndexPattern();
initializeAlertingTelemetry(
this.telemetryLogger,
core,
plugins.taskManager,
kibanaIndex,
this.eventLogService
eventLogIndex
);
}
@ -403,7 +404,9 @@ export class AlertingPlugin {
: Promise.resolve([]);
});
scheduleAlertingTelemetry(this.telemetryLogger, plugins.taskManager);
this.eventLogService!.isEsContextReady().then(() => {
scheduleAlertingTelemetry(this.telemetryLogger, plugins.taskManager);
});
scheduleAlertingHealthCheck(this.logger, this.config, plugins.taskManager);
scheduleApiKeyInvalidatorTask(this.telemetryLogger, this.config, plugins.taskManager);

View file

@ -7,7 +7,6 @@
import { Logger, CoreSetup } from 'kibana/server';
import moment from 'moment';
import { IEventLogService } from '../../../event_log/server';
import {
RunContext,
TaskManagerSetupContract,
@ -29,9 +28,9 @@ export function initializeAlertingTelemetry(
core: CoreSetup,
taskManager: TaskManagerSetupContract,
kibanaIndex: string,
eventLog: IEventLogService
eventLogIndex: string
) {
registerAlertingTelemetryTask(logger, core, taskManager, kibanaIndex, eventLog);
registerAlertingTelemetryTask(logger, core, taskManager, kibanaIndex, eventLogIndex);
}
export function scheduleAlertingTelemetry(logger: Logger, taskManager?: TaskManagerStartContract) {
@ -45,13 +44,13 @@ function registerAlertingTelemetryTask(
core: CoreSetup,
taskManager: TaskManagerSetupContract,
kibanaIndex: string,
eventLog: IEventLogService
eventLogIndex: string
) {
taskManager.registerTaskDefinitions({
[TELEMETRY_TASK_TYPE]: {
title: 'Alerting usage fetch task',
timeout: '5m',
createTaskRunner: telemetryTaskRunner(logger, core, kibanaIndex, eventLog),
createTaskRunner: telemetryTaskRunner(logger, core, kibanaIndex, eventLogIndex),
},
});
}
@ -73,11 +72,10 @@ export function telemetryTaskRunner(
logger: Logger,
core: CoreSetup,
kibanaIndex: string,
eventLog: IEventLogService
eventLogIndex: string
) {
return ({ taskInstance }: RunContext) => {
const { state } = taskInstance;
const eventLogIndex = eventLog.getIndexPattern();
const getEsClient = () =>
core.getStartServices().then(
([

View file

@ -18,6 +18,7 @@ const createEventLogServiceMock = () => {
registerSavedObjectProvider: jest.fn(),
getLogger: jest.fn().mockReturnValue(eventLoggerMock.create()),
getIndexPattern: jest.fn(),
isEsContextReady: jest.fn().mockResolvedValue(true),
};
return mock;
};

View file

@ -92,6 +92,10 @@ export class EventLogService implements IEventLogService {
return this.savedObjectProviderRegistry.registerProvider(type, provider);
}
async isEsContextReady() {
return await this.esContext.waitTillReady();
}
getIndexPattern() {
return this.esContext.esNames.indexPattern;
}

View file

@ -35,6 +35,7 @@ export interface IEventLogService {
registerSavedObjectProvider(type: string, provider: SavedObjectProvider): void;
getLogger(properties: IEvent): IEventLogger;
getIndexPattern(): string;
isEsContextReady(): Promise<boolean>;
}
export interface IEventLogClientService {