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

Fixed usage of isReady for usage collection of alerts and actions #83760

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions x-pack/plugins/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
* 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,
CoreSetup,
CoreStart,
KibanaRequest,
Logger,
SharedGlobalConfig,
RequestHandler,
IContextProvider,
ElasticsearchServiceStart,
Expand Down Expand Up @@ -128,7 +128,6 @@ const includedHiddenTypes = [
];

export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, PluginStartContract> {
private readonly kibanaIndex: Promise<string>;
private readonly config: Promise<ActionsConfig>;

private readonly logger: Logger;
Expand All @@ -145,20 +144,14 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, 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<ActionsConfig>().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(
Expand Down Expand Up @@ -224,21 +217,24 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi

const usageCollection = plugins.usageCollection;
if (usageCollection) {
registerActionsUsageCollector(
usageCollection,
core
.getStartServices()
.then(([_, { taskManager }]) => taskManager as TaskManagerStartContract)
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
);

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
Expand Down Expand Up @@ -273,7 +269,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
actionExecutor,
actionTypeRegistry,
taskRunnerFactory,
kibanaIndex,
kibanaIndexConfig,
isESOUsingEphemeralEncryptionKey,
preconfiguredActions,
instantiateAuthorization,
Expand Down Expand Up @@ -301,10 +297,12 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, 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,
Expand Down Expand Up @@ -430,7 +428,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi

private createRouteHandlerContext = (
core: CoreSetup<ActionsPluginsStart>,
defaultKibanaIndex: string
config: Observable<{ kibana: { index: string } }>
): IContextProvider<RequestHandler<unknown, unknown, unknown>, 'actions'> => {
const {
actionTypeRegistry,
Expand All @@ -442,6 +440,8 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi

return async function actionsRouteHandlerContext(context, request) {
const [{ savedObjects }, { taskManager }] = await core.getStartServices();
const defaultKibanaIndex = (await config.pipe(first()).toPromise()).kibana.index;
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved

return {
getActionsClient: () => {
if (isESOUsingEphemeralEncryptionKey === true) {
Expand Down
11 changes: 8 additions & 3 deletions x-pack/plugins/actions/server/usage/actions_usage_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ const byTypeSchema: MakeSchemaFrom<ActionsUsage>['count_by_type'] = {

export function createActionsUsageCollector(
usageCollection: UsageCollectionSetup,
taskManager: TaskManagerStartContract
taskManager: Promise<TaskManagerStartContract>
) {
let isCollectorReady = false;
taskManager.then(() => {
// mark lensUsageCollector as ready to collect when the TaskManager is ready
isCollectorReady = true;
});
return usageCollection.makeUsageCollector<ActionsUsage>({
type: 'actions',
isReady: () => true,
isReady: () => isCollectorReady,
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
schema: {
count_total: { type: 'long' },
count_active_total: { type: 'long' },
Expand Down Expand Up @@ -79,7 +84,7 @@ async function getLatestTaskState(taskManager: TaskManagerStartContract) {

export function registerActionsUsageCollector(
usageCollection: UsageCollectionSetup,
taskManager: TaskManagerStartContract
taskManager: Promise<TaskManagerStartContract>
) {
const collector = createActionsUsageCollector(usageCollection, taskManager);
usageCollection.registerCollector(collector);
Expand Down
17 changes: 12 additions & 5 deletions x-pack/plugins/actions/server/usage/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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),
},
});
}
Expand All @@ -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<LegacyAPICaller>) => {
Expand All @@ -68,6 +74,7 @@ export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex
};
return {
async run() {
const kibanaIndex = (await config.pipe(first()).toPromise()).kibana.index;
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
return Promise.all([
getTotalCount(callCluster, kibanaIndex),
getInUseTotalCount(callCluster, kibanaIndex),
Expand Down
27 changes: 13 additions & 14 deletions x-pack/plugins/alerts/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -126,30 +127,25 @@ export class AlertingPlugin {
private security?: SecurityPluginSetup;
private readonly alertsClientFactory: AlertsClientFactory;
private readonly telemetryLogger: Logger;
private readonly kibanaIndex: Promise<string>;
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<AlertsConfig>().pipe(first()).toPromise();
this.logger = initializerContext.logger.get('plugins', 'alerting');
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<AlertingPluginsStart, unknown>,
plugins: AlertingPluginsSetup
): Promise<PluginSetupContract> {
): PluginSetupContract {
this.licenseState = new LicenseState(plugins.licensing.license$);
this.spaces = plugins.spaces?.spacesService;
this.security = plugins.security;
Expand Down Expand Up @@ -192,16 +188,19 @@ export class AlertingPlugin {

const usageCollection = plugins.usageCollection;
if (usageCollection) {
registerAlertsUsageCollector(
usageCollection,
core
.getStartServices()
.then(([_, { taskManager }]) => taskManager as TaskManagerStartContract)
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
);

initializeAlertingTelemetry(
this.telemetryLogger,
core,
plugins.taskManager,
await this.kibanaIndex
this.kibanaIndexConfig
);

core.getStartServices().then(async ([, startPlugins]) => {
registerAlertsUsageCollector(usageCollection, startPlugins.taskManager);
});
}

initializeApiKeyInvalidator(
Expand Down
11 changes: 8 additions & 3 deletions x-pack/plugins/alerts/server/usage/alerts_usage_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ const byTypeSchema: MakeSchemaFrom<AlertsUsage>['count_by_type'] = {

export function createAlertsUsageCollector(
usageCollection: UsageCollectionSetup,
taskManager: TaskManagerStartContract
taskManager: Promise<TaskManagerStartContract>
) {
let isCollectorReady = false;
taskManager.then(() => {
// mark lensUsageCollector as ready to collect when the TaskManager is ready
isCollectorReady = true;
});
return usageCollection.makeUsageCollector<AlertsUsage>({
type: 'alerts',
isReady: () => true,
isReady: () => isCollectorReady,
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
fetch: async () => {
try {
const doc = await getLatestTaskState(await taskManager);
Expand Down Expand Up @@ -129,7 +134,7 @@ async function getLatestTaskState(taskManager: TaskManagerStartContract) {

export function registerAlertsUsageCollector(
usageCollection: UsageCollectionSetup,
taskManager: TaskManagerStartContract
taskManager: Promise<TaskManagerStartContract>
) {
const collector = createAlertsUsageCollector(usageCollection, taskManager);
usageCollection.registerCollector(collector);
Expand Down
17 changes: 12 additions & 5 deletions x-pack/plugins/alerts/server/usage/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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),
},
});
}
Expand All @@ -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<LegacyAPICaller>) => {
Expand All @@ -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),
Expand Down