Skip to content

Commit

Permalink
Remove registerContextProvider in Inventory plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
iblancof committed Oct 24, 2024
1 parent d64b3c3 commit 7bc59bd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function InventoryPageTemplate({ children }: { children: React.ReactNode
} = useEntityManager();

const handleSuccess = () => {
telemetry.updateEemEnabled(true);
refresh();
toggleWelcomedModal();
};
Expand Down
46 changes: 6 additions & 40 deletions x-pack/plugins/observability_solution/inventory/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type {
InventorySetupDependencies,
InventoryStartDependencies,
} from './types';
import { TelemetryClient } from './services/telemetry/telemetry_client';

export class InventoryPlugin
implements
Expand All @@ -51,37 +50,6 @@ export class InventoryPlugin
this.isServerlessEnv = context.env.packageInfo.buildFlavor === 'serverless';
}

private async initializeTelemetry(
coreStart: CoreStart,
pluginsStart: InventoryStartDependencies,
telemetry: TelemetryClient
) {
const shouldHideInventory = await this.shouldHideInventory(pluginsStart, coreStart);

if (!shouldHideInventory) {
telemetry.initialize();
}
}

private async shouldHideInventory(
pluginsStart: InventoryStartDependencies,
coreStart: CoreStart
) {
if (pluginsStart.spaces) {
const space = await pluginsStart.spaces.getActiveSpace();
return this.isInventoryDisabledByUserOrSpace(space, coreStart);
}

return !coreStart.application.capabilities.inventory.show;
}

private isInventoryDisabledByUserOrSpace(space: any, coreStart: CoreStart): boolean {
return (
space.disabledFeatures.includes(INVENTORY_APP_ID) ||
!coreStart.application.capabilities.inventory.show
);
}

setup(
coreSetup: CoreSetup<InventoryStartDependencies, InventoryPublicStart>,
pluginsSetup: InventorySetupDependencies
Expand All @@ -96,21 +64,19 @@ export class InventoryPlugin
analytics: coreSetup.analytics,
});

const telemetry = this.telemetry.start({
entityManager: pluginsSetup.entityManager,
});
const telemetry = this.telemetry.start();

const getStartServices = coreSetup.getStartServices();

getStartServices.then(([coreStart, pluginsStart]) => {
this.initializeTelemetry(coreStart, pluginsStart, telemetry);
});

const hideInventory$ = from(getStartServices).pipe(
mergeMap(([coreStart, pluginsStart]) => {
if (pluginsStart.spaces) {
return from(pluginsStart.spaces.getActiveSpace()).pipe(
map((space) => this.isInventoryDisabledByUserOrSpace(space, coreStart))
map(
(space) =>
space.disabledFeatures.includes(INVENTORY_APP_ID) ||
!coreStart.application.capabilities.inventory.show
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import { AnalyticsServiceSetup } from '@kbn/core-analytics-browser';

import { BehaviorSubject } from 'rxjs';
import { EntityManagerPublicPluginSetup } from '@kbn/entityManager-plugin/public';
import {
type ITelemetryClient,
TelemetryEventTypes,
Expand All @@ -20,35 +18,7 @@ import {
} from './types';

export class TelemetryClient implements ITelemetryClient {
private eemEnabled$: BehaviorSubject<{ eem_enabled: boolean }>;

constructor(
private analytics: AnalyticsServiceSetup,
private entityManager: EntityManagerPublicPluginSetup
) {
this.eemEnabled$ = new BehaviorSubject<{ eem_enabled: boolean }>({ eem_enabled: false });
}

public initialize = () => {
this.entityManager.entityClient.isManagedEntityDiscoveryEnabled().then(({ enabled }) => {
this.updateEemEnabled(enabled);

this.analytics.registerContextProvider({
name: 'eem_enabled',
context$: this.eemEnabled$,
schema: {
eem_enabled: {
type: 'boolean',
_meta: { description: 'Whether EEM is enabled or not.' },
},
},
});
});
};

public updateEemEnabled = (enabled: boolean) => {
this.eemEnabled$.next({ eem_enabled: enabled });
};
constructor(private analytics: AnalyticsServiceSetup) {}

public reportInventoryAddData = (params: InventoryAddDataParams) => {
this.analytics.reportEvent(TelemetryEventTypes.INVENTORY_ADD_DATA_CLICKED, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
* 2.0.
*/
import type { AnalyticsServiceSetup } from '@kbn/core-analytics-browser';
import type {
TelemetryServiceSetupParams,
TelemetryEventParams,
TelemetryServiceStartParams,
} from './types';
import type { TelemetryServiceSetupParams, TelemetryEventParams } from './types';
import { inventoryTelemetryEventBasedTypes } from './telemetry_events';
import { TelemetryClient } from './telemetry_client';

Expand All @@ -27,13 +23,13 @@ export class TelemetryService {
);
}

public start({ entityManager }: TelemetryServiceStartParams): TelemetryClient {
public start(): TelemetryClient {
if (!this.analytics) {
throw new Error(
'The TelemetryService.setup() method has not been invoked, be sure to call it during the plugin setup.'
);
}

return new TelemetryClient(this.analytics, entityManager);
return new TelemetryClient(this.analytics);
}
}

0 comments on commit 7bc59bd

Please sign in to comment.