Skip to content

Commit

Permalink
Revert "Use start lifecycle instead of setup"
Browse files Browse the repository at this point in the history
This reverts commit 8f3abab9d44614b78e67a85f0492cbc10be7d0a0.
  • Loading branch information
sorenlouv committed Aug 22, 2023
1 parent 40bf18c commit a8c4e9c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 48 deletions.
21 changes: 14 additions & 7 deletions x-pack/plugins/apm/server/lib/apm_telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
*/

import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { CoreSetup, Logger, SavedObjectsErrorHelpers } from '@kbn/core/server';
import {
CoreSetup,
Logger,
SavedObjectsClientContract,
SavedObjectsErrorHelpers,
} from '@kbn/core/server';
import {
TaskManagerSetupContract,
TaskManagerStartContract,
} from '@kbn/task-manager-plugin/server';
import { APMDataAccessConfig } from '@kbn/apm-data-access-plugin/server';
import {
APM_TELEMETRY_SAVED_OBJECT_ID,
APM_TELEMETRY_SAVED_OBJECT_TYPE,
Expand All @@ -20,19 +26,22 @@ import { collectDataTelemetry } from './collect_data_telemetry';
import { APMUsage } from './types';
import { apmSchema } from './schema';
import { getTelemetryClient } from './telemetry_client';
import { APMPluginStartDependencies } from '../../types';

export const APM_TELEMETRY_TASK_NAME = 'apm-telemetry-task';

export async function createApmTelemetry({
core,
getApmIndices,
usageCollector,
taskManager,
logger,
kibanaVersion,
isProd,
}: {
core: CoreSetup<APMPluginStartDependencies>;
core: CoreSetup;
getApmIndices: (
soClient: SavedObjectsClientContract
) => Promise<APMDataAccessConfig['indices']>;
usageCollector: UsageCollectionSetup;
taskManager: TaskManagerSetupContract;
logger: Logger;
Expand All @@ -54,11 +63,9 @@ export async function createApmTelemetry({
});

const telemetryClient = await getTelemetryClient({ core });
const [coreStart, pluginStart] = await core.getStartServices();
const [coreStart] = await core.getStartServices();
const savedObjectsClient = await getInternalSavedObjectsClient(coreStart);
const apmIndices = await pluginStart.apmDataAccess.getApmIndices(
savedObjectsClient
);
const apmIndices = await getApmIndices(savedObjectsClient);

const collectAndStore = async () => {
const dataTelemetry = await collectDataTelemetry({
Expand Down
35 changes: 18 additions & 17 deletions x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class APMPlugin
) {
createApmTelemetry({
core,
getApmIndices: plugins.apmDataAccess.getApmIndices,
usageCollector: plugins.usageCollection,
taskManager: plugins.taskManager,
logger: this.logger,
Expand Down Expand Up @@ -133,9 +134,9 @@ export class APMPlugin

const apmIndicesPromise = (async () => {
const coreStart = await getCoreStart();
const pluginStart = await getPluginStart();
const soClient = await getInternalSavedObjectsClient(coreStart);
return pluginStart.apmDataAccess.getApmIndices(soClient);
const { getApmIndices } = plugins.apmDataAccess;
return getApmIndices(soClient);
})();

// This if else block will go away in favour of removing Home Tutorial Integration
Expand Down Expand Up @@ -179,21 +180,21 @@ export class APMPlugin
kibanaVersion: this.initContext.env.packageInfo.version,
});

core.getStartServices().then(([, pluginStart]) => {
if (plugins.alerting) {
registerApmRuleTypes({
getApmIndices: pluginStart.apmDataAccess.getApmIndices,
alerting: plugins.alerting,
basePath: core.http.basePath,
apmConfig: currentConfig,
logger: this.logger!.get('rule'),
ml: plugins.ml,
observability: plugins.observability,
ruleDataClient,
alertsLocator: plugins.share.url.locators.get(alertsLocatorID),
});
}
});
const { getApmIndices } = plugins.apmDataAccess;

if (plugins.alerting) {
registerApmRuleTypes({
getApmIndices,
alerting: plugins.alerting,
basePath: core.http.basePath,
apmConfig: currentConfig,
logger: this.logger!.get('rule'),
ml: plugins.ml,
observability: plugins.observability,
ruleDataClient,
alertsLocator: plugins.share.url.locators.get(alertsLocatorID),
});
}

registerFleetPolicyCallbacks({
logger: this.logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export function registerRoutes({

const getApmIndices = async () => {
const coreContext = await context.core;
const apmDataAccessStart = await plugins.apmDataAccess.start();
return apmDataAccessStart.getApmIndices(
const apmIndices = await plugins.apmDataAccess.setup.getApmIndices(
coreContext.savedObjects.client
);
return apmIndices;
};

const { aborted, data } = await Promise.race([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function registerFleetPolicyCallbacks({
}

const fleetPluginStart = await plugins.fleet.start();
const { getApmIndices } = await plugins.apmDataAccess.start();
const { getApmIndices } = plugins.apmDataAccess.setup;
const coreStart = await coreStartPromise;

fleetPluginStart.registerExternalCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export type ApmIndexSettingsResponse = Array<{
export async function getApmIndexSettings(
resources: APMRouteHandlerResources
): Promise<ApmIndexSettingsResponse> {
const { apmIndicesFromConfigFile } =
await resources.plugins.apmDataAccess.start();
const { apmIndicesFromConfigFile } = resources.plugins.apmDataAccess.setup;

const soClient = (await resources.context.core).savedObjects.client;
const apmIndicesSavedObject = await getApmIndicesSavedObject(soClient);
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/apm_data_access/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { schema, TypeOf } from '@kbn/config-schema';
import { PluginConfigDescriptor, PluginInitializerContext } from '@kbn/core/server';
import { ApmDataAccessPlugin } from './plugin';

export type APMDataAccessConfig = TypeOf<typeof configSchema>;
export type APMIndices = APMDataAccessConfig['indices'];

const configSchema = schema.object({
indices: schema.object({
transaction: schema.string({ defaultValue: 'traces-apm*,apm-*' }), // TODO: remove apm-* pattern in 9.0
Expand Down Expand Up @@ -73,6 +70,8 @@ export const config: PluginConfigDescriptor<APMDataAccessConfig> = {

schema: configSchema,
};
export type APMDataAccessConfig = TypeOf<typeof configSchema>;
export type APMIndices = APMDataAccessConfig['indices'];

export function plugin(initializerContext: PluginInitializerContext) {
return new ApmDataAccessPlugin(initializerContext);
Expand Down
26 changes: 13 additions & 13 deletions x-pack/plugins/apm_data_access/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ export class ApmDataAccessPlugin
}

public setup(core: CoreSetup): ApmDataAccessPluginSetup {
// retrieve APM indices from config
const apmDataAccessConfig = this.initContext.config.get<APMDataAccessConfig>();
const apmIndicesFromConfigFile = apmDataAccessConfig.indices;

// register saved object
core.savedObjects.registerType(apmIndicesSavedObjectDefinition);

return {};
// expose
return {
apmIndicesFromConfigFile,
getApmIndices: async (savedObjectsClient: SavedObjectsClientContract) => {
const apmIndicesFromSavedObject = await getApmIndicesSavedObject(savedObjectsClient);
return { ...apmIndicesFromConfigFile, ...apmIndicesFromSavedObject };
},
};
}

public start(core: CoreStart) {
Expand All @@ -41,18 +52,7 @@ export class ApmDataAccessPlugin
logger.error('Failed to run migration making APM indices space aware');
logger.error(e);
});

// retrieve APM indices from config
const apmDataAccessConfig = this.initContext.config.get<APMDataAccessConfig>();
const apmIndicesFromConfigFile = apmDataAccessConfig.indices;

return {
apmIndicesFromConfigFile,
getApmIndices: async (savedObjectsClient: SavedObjectsClientContract) => {
const apmIndicesFromSavedObject = await getApmIndicesSavedObject(savedObjectsClient);
return { ...apmIndicesFromConfigFile, ...apmIndicesFromSavedObject };
},
};
return {};
}

public stop() {}
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/apm_data_access/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import { APMIndices } from '.';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ApmDataAccessPluginSetup {}
export interface ApmDataAccessPluginStart {
export interface ApmDataAccessPluginSetup {
apmIndicesFromConfigFile: APMIndices;
getApmIndices: (soClient: SavedObjectsClientContract) => Promise<APMIndices>;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ApmDataAccessPluginStart {}

0 comments on commit a8c4e9c

Please sign in to comment.