From f418f7bbcb66d7b3d1b89a851106ef9db8b7120a Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Wed, 18 Nov 2020 19:27:05 -0800 Subject: [PATCH 1/5] Fixed usage of `isReady` for usage collection of alerts and actions --- x-pack/plugins/actions/server/plugin.ts | 30 ++++++++----------- .../server/usage/actions_usage_collector.ts | 11 +++++-- x-pack/plugins/actions/server/usage/task.ts | 17 +++++++---- x-pack/plugins/alerts/server/plugin.ts | 27 ++++++++--------- .../server/usage/alerts_usage_collector.ts | 11 +++++-- x-pack/plugins/alerts/server/usage/task.ts | 17 +++++++---- 6 files changed, 66 insertions(+), 47 deletions(-) diff --git a/x-pack/plugins/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index 9db07f653872f..ccd5068240807 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -4,8 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ import type { PublicMethodsOf } from '@kbn/utility-types'; -import { first, map } from 'rxjs/operators'; +import { first } from 'rxjs/operators'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { Observable } from 'rxjs'; import { PluginInitializerContext, Plugin, @@ -13,7 +14,6 @@ import { CoreStart, KibanaRequest, Logger, - SharedGlobalConfig, RequestHandler, IContextProvider, ElasticsearchServiceStart, @@ -128,7 +128,6 @@ const includedHiddenTypes = [ ]; export class ActionsPlugin implements Plugin, PluginStartContract> { - private readonly kibanaIndex: Promise; private readonly config: Promise; private readonly logger: Logger; @@ -145,20 +144,14 @@ export class ActionsPlugin implements Plugin, Plugi private isESOUsingEphemeralEncryptionKey?: boolean; private readonly telemetryLogger: Logger; private readonly preconfiguredActions: PreConfiguredAction[]; + private readonly kibanaIndexConfig: Observable<{ kibana: { index: string } }>; constructor(initContext: PluginInitializerContext) { this.config = initContext.config.create().pipe(first()).toPromise(); - - this.kibanaIndex = initContext.config.legacy.globalConfig$ - .pipe( - first(), - map((config: SharedGlobalConfig) => config.kibana.index) - ) - .toPromise(); - this.logger = initContext.logger.get('actions'); this.telemetryLogger = initContext.logger.get('usage'); this.preconfiguredActions = []; + this.kibanaIndexConfig = initContext.config.legacy.globalConfig$; } public async setup( @@ -224,21 +217,24 @@ export class ActionsPlugin implements Plugin, Plugi const usageCollection = plugins.usageCollection; if (usageCollection) { + registerActionsUsageCollector( + usageCollection, + core + .getStartServices() + .then(([_, { taskManager }]) => taskManager as TaskManagerStartContract) + ); + initializeActionsTelemetry( this.telemetryLogger, plugins.taskManager, core, - await this.kibanaIndex + this.kibanaIndexConfig ); - - core.getStartServices().then(async ([, startPlugins]) => { - registerActionsUsageCollector(usageCollection, startPlugins.taskManager); - }); } core.http.registerRouteHandlerContext( 'actions', - this.createRouteHandlerContext(core, await this.kibanaIndex) + this.createRouteHandlerContext(core, this.kibanaIndexConfig) ); // Routes diff --git a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts index fac57b6282c44..5bf1cd4dad2cd 100644 --- a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts +++ b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts @@ -26,11 +26,16 @@ const byTypeSchema: MakeSchemaFrom['count_by_type'] = { export function createActionsUsageCollector( usageCollection: UsageCollectionSetup, - taskManager: TaskManagerStartContract + taskManager: Promise ) { + let isCollectorReady = false; + taskManager.then(() => { + // mark lensUsageCollector as ready to collect when the TaskManager is ready + isCollectorReady = true; + }); return usageCollection.makeUsageCollector({ type: 'actions', - isReady: () => true, + isReady: () => isCollectorReady, schema: { count_total: { type: 'long' }, count_active_total: { type: 'long' }, @@ -79,7 +84,7 @@ async function getLatestTaskState(taskManager: TaskManagerStartContract) { export function registerActionsUsageCollector( usageCollection: UsageCollectionSetup, - taskManager: TaskManagerStartContract + taskManager: Promise ) { const collector = createActionsUsageCollector(usageCollection, taskManager); usageCollection.registerCollector(collector); diff --git a/x-pack/plugins/actions/server/usage/task.ts b/x-pack/plugins/actions/server/usage/task.ts index 176ba29ef748a..ebb2b181ab8ea 100644 --- a/x-pack/plugins/actions/server/usage/task.ts +++ b/x-pack/plugins/actions/server/usage/task.ts @@ -6,6 +6,8 @@ import { Logger, CoreSetup, LegacyAPICaller } from 'kibana/server'; import moment from 'moment'; +import { first } from 'rxjs/operators'; +import { Observable } from 'rxjs'; import { RunContext, TaskManagerSetupContract, @@ -21,9 +23,9 @@ export function initializeActionsTelemetry( logger: Logger, taskManager: TaskManagerSetupContract, core: CoreSetup, - kibanaIndex: string + config: Observable<{ kibana: { index: string } }> ) { - registerActionsTelemetryTask(logger, taskManager, core, kibanaIndex); + registerActionsTelemetryTask(logger, taskManager, core, config); } export function scheduleActionsTelemetry(logger: Logger, taskManager: TaskManagerStartContract) { @@ -34,13 +36,13 @@ function registerActionsTelemetryTask( logger: Logger, taskManager: TaskManagerSetupContract, core: CoreSetup, - kibanaIndex: string + config: Observable<{ kibana: { index: string } }> ) { taskManager.registerTaskDefinitions({ [TELEMETRY_TASK_TYPE]: { title: 'Actions usage fetch task', timeout: '5m', - createTaskRunner: telemetryTaskRunner(logger, core, kibanaIndex), + createTaskRunner: telemetryTaskRunner(logger, core, config), }, }); } @@ -58,7 +60,11 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra } } -export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex: string) { +export function telemetryTaskRunner( + logger: Logger, + core: CoreSetup, + config: Observable<{ kibana: { index: string } }> +) { return ({ taskInstance }: RunContext) => { const { state } = taskInstance; const callCluster = (...args: Parameters) => { @@ -68,6 +74,7 @@ export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex }; return { async run() { + const kibanaIndex = (await config.pipe(first()).toPromise()).kibana.index; return Promise.all([ getTotalCount(callCluster, kibanaIndex), getInUseTotalCount(callCluster, kibanaIndex), diff --git a/x-pack/plugins/alerts/server/plugin.ts b/x-pack/plugins/alerts/server/plugin.ts index 0c91e93938346..a4f0290fd313c 100644 --- a/x-pack/plugins/alerts/server/plugin.ts +++ b/x-pack/plugins/alerts/server/plugin.ts @@ -5,6 +5,7 @@ */ import type { PublicMethodsOf } from '@kbn/utility-types'; import { first, map } from 'rxjs/operators'; +import { Observable } from 'rxjs'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { combineLatest } from 'rxjs'; import { SecurityPluginSetup } from '../../security/server'; @@ -126,10 +127,10 @@ export class AlertingPlugin { private security?: SecurityPluginSetup; private readonly alertsClientFactory: AlertsClientFactory; private readonly telemetryLogger: Logger; - private readonly kibanaIndex: Promise; private readonly kibanaVersion: PluginInitializerContext['env']['packageInfo']['version']; private eventLogService?: IEventLogService; private eventLogger?: IEventLogger; + private readonly kibanaIndexConfig: Observable<{ kibana: { index: string } }>; constructor(initializerContext: PluginInitializerContext) { this.config = initializerContext.config.create().pipe(first()).toPromise(); @@ -137,19 +138,14 @@ export class AlertingPlugin { this.taskRunnerFactory = new TaskRunnerFactory(); this.alertsClientFactory = new AlertsClientFactory(); this.telemetryLogger = initializerContext.logger.get('usage'); - this.kibanaIndex = initializerContext.config.legacy.globalConfig$ - .pipe( - first(), - map((config: SharedGlobalConfig) => config.kibana.index) - ) - .toPromise(); + this.kibanaIndexConfig = initializerContext.config.legacy.globalConfig$; this.kibanaVersion = initializerContext.env.packageInfo.version; } - public async setup( + public setup( core: CoreSetup, plugins: AlertingPluginsSetup - ): Promise { + ): PluginSetupContract { this.licenseState = new LicenseState(plugins.licensing.license$); this.spaces = plugins.spaces?.spacesService; this.security = plugins.security; @@ -192,16 +188,19 @@ export class AlertingPlugin { const usageCollection = plugins.usageCollection; if (usageCollection) { + registerAlertsUsageCollector( + usageCollection, + core + .getStartServices() + .then(([_, { taskManager }]) => taskManager as TaskManagerStartContract) + ); + initializeAlertingTelemetry( this.telemetryLogger, core, plugins.taskManager, - await this.kibanaIndex + this.kibanaIndexConfig ); - - core.getStartServices().then(async ([, startPlugins]) => { - registerAlertsUsageCollector(usageCollection, startPlugins.taskManager); - }); } initializeApiKeyInvalidator( diff --git a/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts b/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts index de82dd31877af..19ea758180495 100644 --- a/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts +++ b/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts @@ -44,11 +44,16 @@ const byTypeSchema: MakeSchemaFrom['count_by_type'] = { export function createAlertsUsageCollector( usageCollection: UsageCollectionSetup, - taskManager: TaskManagerStartContract + taskManager: Promise ) { + let isCollectorReady = false; + taskManager.then(() => { + // mark lensUsageCollector as ready to collect when the TaskManager is ready + isCollectorReady = true; + }); return usageCollection.makeUsageCollector({ type: 'alerts', - isReady: () => true, + isReady: () => isCollectorReady, fetch: async () => { try { const doc = await getLatestTaskState(await taskManager); @@ -129,7 +134,7 @@ async function getLatestTaskState(taskManager: TaskManagerStartContract) { export function registerAlertsUsageCollector( usageCollection: UsageCollectionSetup, - taskManager: TaskManagerStartContract + taskManager: Promise ) { const collector = createAlertsUsageCollector(usageCollection, taskManager); usageCollection.registerCollector(collector); diff --git a/x-pack/plugins/alerts/server/usage/task.ts b/x-pack/plugins/alerts/server/usage/task.ts index ba0ffae7ebfbd..05771f8595e52 100644 --- a/x-pack/plugins/alerts/server/usage/task.ts +++ b/x-pack/plugins/alerts/server/usage/task.ts @@ -6,6 +6,8 @@ import { Logger, CoreSetup, LegacyAPICaller } from 'kibana/server'; import moment from 'moment'; +import { first } from 'rxjs/operators'; +import { Observable } from 'rxjs'; import { RunContext, TaskManagerSetupContract, @@ -22,9 +24,9 @@ export function initializeAlertingTelemetry( logger: Logger, core: CoreSetup, taskManager: TaskManagerSetupContract, - kibanaIndex: string + config: Observable<{ kibana: { index: string } }> ) { - registerAlertingTelemetryTask(logger, core, taskManager, kibanaIndex); + registerAlertingTelemetryTask(logger, core, taskManager, config); } export function scheduleAlertingTelemetry(logger: Logger, taskManager?: TaskManagerStartContract) { @@ -37,13 +39,13 @@ function registerAlertingTelemetryTask( logger: Logger, core: CoreSetup, taskManager: TaskManagerSetupContract, - kibanaIndex: string + config: Observable<{ kibana: { index: string } }> ) { taskManager.registerTaskDefinitions({ [TELEMETRY_TASK_TYPE]: { title: 'Alerting usage fetch task', timeout: '5m', - createTaskRunner: telemetryTaskRunner(logger, core, kibanaIndex), + createTaskRunner: telemetryTaskRunner(logger, core, config), }, }); } @@ -61,7 +63,11 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra } } -export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex: string) { +export function telemetryTaskRunner( + logger: Logger, + core: CoreSetup, + config: Observable<{ kibana: { index: string } }> +) { return ({ taskInstance }: RunContext) => { const { state } = taskInstance; const callCluster = (...args: Parameters) => { @@ -72,6 +78,7 @@ export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex return { async run() { + const kibanaIndex = (await config.pipe(first()).toPromise()).kibana.index; return Promise.all([ getTotalCountAggregations(callCluster, kibanaIndex), getTotalCountInUse(callCluster, kibanaIndex), From 17a42bcbebe515fe143ddab1caa7da2262b86e53 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Wed, 18 Nov 2020 19:36:48 -0800 Subject: [PATCH 2/5] fixed index --- x-pack/plugins/actions/server/plugin.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index ccd5068240807..1ab827059e7c6 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -269,7 +269,7 @@ export class ActionsPlugin implements Plugin, Plugi actionExecutor, actionTypeRegistry, taskRunnerFactory, - kibanaIndex, + kibanaIndexConfig, isESOUsingEphemeralEncryptionKey, preconfiguredActions, instantiateAuthorization, @@ -297,10 +297,12 @@ export class ActionsPlugin implements Plugin, Plugi request ); + const kibanaIndex = (await kibanaIndexConfig.pipe(first()).toPromise()).kibana.index; + return new ActionsClient({ unsecuredSavedObjectsClient, actionTypeRegistry: actionTypeRegistry!, - defaultKibanaIndex: await kibanaIndex, + defaultKibanaIndex: kibanaIndex, scopedClusterClient: core.elasticsearch.legacy.client.asScoped(request), preconfiguredActions, request, @@ -426,7 +428,7 @@ export class ActionsPlugin implements Plugin, Plugi private createRouteHandlerContext = ( core: CoreSetup, - defaultKibanaIndex: string + config: Observable<{ kibana: { index: string } }> ): IContextProvider, 'actions'> => { const { actionTypeRegistry, @@ -438,6 +440,8 @@ export class ActionsPlugin implements Plugin, Plugi return async function actionsRouteHandlerContext(context, request) { const [{ savedObjects }, { taskManager }] = await core.getStartServices(); + const defaultKibanaIndex = (await config.pipe(first()).toPromise()).kibana.index; + return { getActionsClient: () => { if (isESOUsingEphemeralEncryptionKey === true) { From 4159b46901ed07f4c435862e4ed45cb7af4acde4 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Thu, 19 Nov 2020 13:37:24 -0800 Subject: [PATCH 3/5] fixed due to comments --- x-pack/plugins/actions/server/plugin.ts | 36 ++++++++++--------- .../usage/actions_usage_collector.test.ts | 6 ++-- .../server/usage/actions_usage_collector.ts | 10 +++--- x-pack/plugins/actions/server/usage/task.ts | 17 +++------ x-pack/plugins/alerts/server/plugin.ts | 23 ++++++------ .../usage/alerts_usage_collector.test.ts | 12 +++++-- .../server/usage/alerts_usage_collector.ts | 10 +++--- x-pack/plugins/alerts/server/usage/task.ts | 17 +++------ 8 files changed, 63 insertions(+), 68 deletions(-) diff --git a/x-pack/plugins/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index 1ab827059e7c6..faf063568a431 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -219,23 +219,27 @@ export class ActionsPlugin implements Plugin, Plugi if (usageCollection) { registerActionsUsageCollector( usageCollection, - core - .getStartServices() - .then(([_, { taskManager }]) => taskManager as TaskManagerStartContract) - ); - - initializeActionsTelemetry( - this.telemetryLogger, - plugins.taskManager, - core, - this.kibanaIndexConfig + core.getStartServices().then(([_, { taskManager }]) => taskManager) ); } - core.http.registerRouteHandlerContext( - 'actions', - this.createRouteHandlerContext(core, this.kibanaIndexConfig) - ); + this.kibanaIndexConfig + .pipe(first()) + .toPromise() + .then((config) => { + core.http.registerRouteHandlerContext( + 'actions', + this.createRouteHandlerContext(core, config.kibana.index) + ); + if (usageCollection) { + initializeActionsTelemetry( + this.telemetryLogger, + plugins.taskManager, + core, + config.kibana.index + ); + } + }); // Routes const router = core.http.createRouter(); @@ -428,7 +432,7 @@ export class ActionsPlugin implements Plugin, Plugi private createRouteHandlerContext = ( core: CoreSetup, - config: Observable<{ kibana: { index: string } }> + defaultKibanaIndex: string ): IContextProvider, 'actions'> => { const { actionTypeRegistry, @@ -440,8 +444,6 @@ export class ActionsPlugin implements Plugin, Plugi return async function actionsRouteHandlerContext(context, request) { const [{ savedObjects }, { taskManager }] = await core.getStartServices(); - const defaultKibanaIndex = (await config.pipe(first()).toPromise()).kibana.index; - return { getActionsClient: () => { if (isESOUsingEphemeralEncryptionKey === true) { diff --git a/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts b/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts index 0e6c2ff37eb02..9f0b95dab5440 100644 --- a/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts +++ b/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts @@ -7,8 +7,10 @@ import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { registerActionsUsageCollector } from './actions_usage_collector'; import { taskManagerMock } from '../../../task_manager/server/mocks'; +import { coreMock } from 'src/core/server/mocks'; const mockTaskManagerStart = taskManagerMock.createStart(); +const core = coreMock.createSetup(); beforeEach(() => jest.resetAllMocks()); @@ -24,7 +26,7 @@ describe('registerActionsUsageCollector', () => { it('should call registerCollector', () => { registerActionsUsageCollector( usageCollectionMock as UsageCollectionSetup, - mockTaskManagerStart + core.getStartServices().then(([_, {}]) => mockTaskManagerStart) ); expect(usageCollectionMock.registerCollector).toHaveBeenCalledTimes(1); }); @@ -32,7 +34,7 @@ describe('registerActionsUsageCollector', () => { it('should call makeUsageCollector with type = actions', () => { registerActionsUsageCollector( usageCollectionMock as UsageCollectionSetup, - mockTaskManagerStart + core.getStartServices().then(([_, {}]) => mockTaskManagerStart) ); expect(usageCollectionMock.makeUsageCollector).toHaveBeenCalledTimes(1); expect(usageCollectionMock.makeUsageCollector.mock.calls[0][0].type).toBe('actions'); diff --git a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts index 5bf1cd4dad2cd..f86c6a40e0505 100644 --- a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts +++ b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts @@ -28,14 +28,12 @@ export function createActionsUsageCollector( usageCollection: UsageCollectionSetup, taskManager: Promise ) { - let isCollectorReady = false; - taskManager.then(() => { - // mark lensUsageCollector as ready to collect when the TaskManager is ready - isCollectorReady = true; - }); return usageCollection.makeUsageCollector({ type: 'actions', - isReady: () => isCollectorReady, + isReady: async () => { + await taskManager; + return true; + }, schema: { count_total: { type: 'long' }, count_active_total: { type: 'long' }, diff --git a/x-pack/plugins/actions/server/usage/task.ts b/x-pack/plugins/actions/server/usage/task.ts index ebb2b181ab8ea..176ba29ef748a 100644 --- a/x-pack/plugins/actions/server/usage/task.ts +++ b/x-pack/plugins/actions/server/usage/task.ts @@ -6,8 +6,6 @@ import { Logger, CoreSetup, LegacyAPICaller } from 'kibana/server'; import moment from 'moment'; -import { first } from 'rxjs/operators'; -import { Observable } from 'rxjs'; import { RunContext, TaskManagerSetupContract, @@ -23,9 +21,9 @@ export function initializeActionsTelemetry( logger: Logger, taskManager: TaskManagerSetupContract, core: CoreSetup, - config: Observable<{ kibana: { index: string } }> + kibanaIndex: string ) { - registerActionsTelemetryTask(logger, taskManager, core, config); + registerActionsTelemetryTask(logger, taskManager, core, kibanaIndex); } export function scheduleActionsTelemetry(logger: Logger, taskManager: TaskManagerStartContract) { @@ -36,13 +34,13 @@ function registerActionsTelemetryTask( logger: Logger, taskManager: TaskManagerSetupContract, core: CoreSetup, - config: Observable<{ kibana: { index: string } }> + kibanaIndex: string ) { taskManager.registerTaskDefinitions({ [TELEMETRY_TASK_TYPE]: { title: 'Actions usage fetch task', timeout: '5m', - createTaskRunner: telemetryTaskRunner(logger, core, config), + createTaskRunner: telemetryTaskRunner(logger, core, kibanaIndex), }, }); } @@ -60,11 +58,7 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra } } -export function telemetryTaskRunner( - logger: Logger, - core: CoreSetup, - config: Observable<{ kibana: { index: string } }> -) { +export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex: string) { return ({ taskInstance }: RunContext) => { const { state } = taskInstance; const callCluster = (...args: Parameters) => { @@ -74,7 +68,6 @@ export function telemetryTaskRunner( }; return { async run() { - const kibanaIndex = (await config.pipe(first()).toPromise()).kibana.index; return Promise.all([ getTotalCount(callCluster, kibanaIndex), getInUseTotalCount(callCluster, kibanaIndex), diff --git a/x-pack/plugins/alerts/server/plugin.ts b/x-pack/plugins/alerts/server/plugin.ts index a4f0290fd313c..1ff855ca90dce 100644 --- a/x-pack/plugins/alerts/server/plugin.ts +++ b/x-pack/plugins/alerts/server/plugin.ts @@ -29,7 +29,6 @@ import { SavedObjectsServiceStart, IContextProvider, RequestHandler, - SharedGlobalConfig, ElasticsearchServiceStart, ILegacyClusterClient, StatusServiceSetup, @@ -190,17 +189,19 @@ export class AlertingPlugin { if (usageCollection) { registerAlertsUsageCollector( usageCollection, - core - .getStartServices() - .then(([_, { taskManager }]) => taskManager as TaskManagerStartContract) - ); - - initializeAlertingTelemetry( - this.telemetryLogger, - core, - plugins.taskManager, - this.kibanaIndexConfig + core.getStartServices().then(([_, { taskManager }]) => taskManager) ); + this.kibanaIndexConfig + .pipe(first()) + .toPromise() + .then((config) => + initializeAlertingTelemetry( + this.telemetryLogger, + core, + plugins.taskManager, + config.kibana.index + ) + ); } initializeApiKeyInvalidator( diff --git a/x-pack/plugins/alerts/server/usage/alerts_usage_collector.test.ts b/x-pack/plugins/alerts/server/usage/alerts_usage_collector.test.ts index a5f83bc393d4e..a2f45c6169d12 100644 --- a/x-pack/plugins/alerts/server/usage/alerts_usage_collector.test.ts +++ b/x-pack/plugins/alerts/server/usage/alerts_usage_collector.test.ts @@ -7,8 +7,10 @@ import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { registerAlertsUsageCollector } from './alerts_usage_collector'; import { taskManagerMock } from '../../../task_manager/server/mocks'; +import { coreMock } from 'src/core/server/mocks'; const taskManagerStart = taskManagerMock.createStart(); +const core = coreMock.createSetup(); beforeEach(() => jest.resetAllMocks()); describe('registerAlertsUsageCollector', () => { @@ -22,12 +24,18 @@ describe('registerAlertsUsageCollector', () => { }); it('should call registerCollector', () => { - registerAlertsUsageCollector(usageCollectionMock as UsageCollectionSetup, taskManagerStart); + registerAlertsUsageCollector( + usageCollectionMock as UsageCollectionSetup, + core.getStartServices().then(([_, {}]) => taskManagerStart) + ); expect(usageCollectionMock.registerCollector).toHaveBeenCalledTimes(1); }); it('should call makeUsageCollector with type = alerts', () => { - registerAlertsUsageCollector(usageCollectionMock as UsageCollectionSetup, taskManagerStart); + registerAlertsUsageCollector( + usageCollectionMock as UsageCollectionSetup, + core.getStartServices().then(([_, {}]) => taskManagerStart) + ); expect(usageCollectionMock.makeUsageCollector).toHaveBeenCalledTimes(1); expect(usageCollectionMock.makeUsageCollector.mock.calls[0][0].type).toBe('alerts'); }); diff --git a/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts b/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts index 19ea758180495..40a9983ae2786 100644 --- a/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts +++ b/x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts @@ -46,14 +46,12 @@ export function createAlertsUsageCollector( usageCollection: UsageCollectionSetup, taskManager: Promise ) { - let isCollectorReady = false; - taskManager.then(() => { - // mark lensUsageCollector as ready to collect when the TaskManager is ready - isCollectorReady = true; - }); return usageCollection.makeUsageCollector({ type: 'alerts', - isReady: () => isCollectorReady, + isReady: async () => { + await taskManager; + return true; + }, fetch: async () => { try { const doc = await getLatestTaskState(await taskManager); diff --git a/x-pack/plugins/alerts/server/usage/task.ts b/x-pack/plugins/alerts/server/usage/task.ts index 05771f8595e52..ba0ffae7ebfbd 100644 --- a/x-pack/plugins/alerts/server/usage/task.ts +++ b/x-pack/plugins/alerts/server/usage/task.ts @@ -6,8 +6,6 @@ import { Logger, CoreSetup, LegacyAPICaller } from 'kibana/server'; import moment from 'moment'; -import { first } from 'rxjs/operators'; -import { Observable } from 'rxjs'; import { RunContext, TaskManagerSetupContract, @@ -24,9 +22,9 @@ export function initializeAlertingTelemetry( logger: Logger, core: CoreSetup, taskManager: TaskManagerSetupContract, - config: Observable<{ kibana: { index: string } }> + kibanaIndex: string ) { - registerAlertingTelemetryTask(logger, core, taskManager, config); + registerAlertingTelemetryTask(logger, core, taskManager, kibanaIndex); } export function scheduleAlertingTelemetry(logger: Logger, taskManager?: TaskManagerStartContract) { @@ -39,13 +37,13 @@ function registerAlertingTelemetryTask( logger: Logger, core: CoreSetup, taskManager: TaskManagerSetupContract, - config: Observable<{ kibana: { index: string } }> + kibanaIndex: string ) { taskManager.registerTaskDefinitions({ [TELEMETRY_TASK_TYPE]: { title: 'Alerting usage fetch task', timeout: '5m', - createTaskRunner: telemetryTaskRunner(logger, core, config), + createTaskRunner: telemetryTaskRunner(logger, core, kibanaIndex), }, }); } @@ -63,11 +61,7 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra } } -export function telemetryTaskRunner( - logger: Logger, - core: CoreSetup, - config: Observable<{ kibana: { index: string } }> -) { +export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex: string) { return ({ taskInstance }: RunContext) => { const { state } = taskInstance; const callCluster = (...args: Parameters) => { @@ -78,7 +72,6 @@ export function telemetryTaskRunner( return { async run() { - const kibanaIndex = (await config.pipe(first()).toPromise()).kibana.index; return Promise.all([ getTotalCountAggregations(callCluster, kibanaIndex), getTotalCountInUse(callCluster, kibanaIndex), From d469bdddc9b8abe9c114e2fdcf8be66ce7743cf9 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Thu, 19 Nov 2020 14:08:43 -0800 Subject: [PATCH 4/5] fixed type check --- .../plugins/actions/server/usage/actions_usage_collector.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts b/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts index 67f0a4c82fb8f..39a61cebe92dc 100644 --- a/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts +++ b/x-pack/plugins/actions/server/usage/actions_usage_collector.test.ts @@ -7,7 +7,6 @@ import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { registerActionsUsageCollector } from './actions_usage_collector'; import { taskManagerMock } from '../../../task_manager/server/mocks'; -import { coreMock } from 'src/core/server/mocks'; const mockTaskManagerStart = taskManagerMock.createStart(); From 5a3fb47cf4c585f2caa1a7aacfb942bf565a59d3 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Fri, 20 Nov 2020 17:30:51 -0800 Subject: [PATCH 5/5] fixed due to comments --- x-pack/plugins/actions/server/plugin.ts | 29 +++++++++++-------------- x-pack/plugins/alerts/server/plugin.ts | 17 ++++++--------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/x-pack/plugins/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index 100d9e24dd32b..e61936321b8e0 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -219,23 +219,20 @@ export class ActionsPlugin implements Plugin, Plugi ); } - this.kibanaIndexConfig - .pipe(first()) - .toPromise() - .then((config) => { - core.http.registerRouteHandlerContext( - 'actions', - this.createRouteHandlerContext(core, config.kibana.index) + this.kibanaIndexConfig.subscribe((config) => { + core.http.registerRouteHandlerContext( + 'actions', + this.createRouteHandlerContext(core, config.kibana.index) + ); + if (usageCollection) { + initializeActionsTelemetry( + this.telemetryLogger, + plugins.taskManager, + core, + config.kibana.index ); - if (usageCollection) { - initializeActionsTelemetry( - this.telemetryLogger, - plugins.taskManager, - core, - config.kibana.index - ); - } - }); + } + }); // Routes const router = core.http.createRouter(); diff --git a/x-pack/plugins/alerts/server/plugin.ts b/x-pack/plugins/alerts/server/plugin.ts index a466c9e48c618..4bfb44425544a 100644 --- a/x-pack/plugins/alerts/server/plugin.ts +++ b/x-pack/plugins/alerts/server/plugin.ts @@ -186,17 +186,14 @@ export class AlertingPlugin { usageCollection, core.getStartServices().then(([_, { taskManager }]) => taskManager) ); - this.kibanaIndexConfig - .pipe(first()) - .toPromise() - .then((config) => - initializeAlertingTelemetry( - this.telemetryLogger, - core, - plugins.taskManager, - config.kibana.index - ) + this.kibanaIndexConfig.subscribe((config) => { + initializeAlertingTelemetry( + this.telemetryLogger, + core, + plugins.taskManager, + config.kibana.index ); + }); } initializeApiKeyInvalidator(