Skip to content

Commit

Permalink
Refactor telemetry initialization and update in inventory plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
iblancof committed Oct 23, 2024
1 parent baf1270 commit a4bdbb2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { useEntityManager } from '../../hooks/use_entity_manager';
import { Welcome } from '../entity_enablement/welcome_modal';
import { useInventoryAbortableAsync } from '../../hooks/use_inventory_abortable_async';
import { EmptyState } from '../empty_states/empty_state';
import { eemEnabled$ } from '../../analytics/register_eem_enabled_context';
import { useIsLoadingComplete } from '../../hooks/use_is_loading_complete';

const pageTitle = (
Expand Down Expand Up @@ -51,7 +50,7 @@ export function InventoryPageTemplate({ children }: { children: React.ReactNode
} = useEntityManager();

const handleSuccess = () => {
eemEnabled$.next({ eem_enabled: true });
telemetry.updateEemEnabled(true);
refresh();
toggleWelcomedModal();
};
Expand Down
57 changes: 47 additions & 10 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,7 @@ import type {
InventorySetupDependencies,
InventoryStartDependencies,
} from './types';
import { registerEemEnabledContext } from './analytics/register_eem_enabled_context';
import { TelemetryClient } from './services/telemetry/telemetry_client';

export class InventoryPlugin
implements
Expand All @@ -50,6 +50,38 @@ export class InventoryPlugin
this.kibanaVersion = context.env.packageInfo.version;
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 @@ -59,17 +91,26 @@ export class InventoryPlugin
'observability:entityCentricExperience',
true
);

this.telemetry.setup({
analytics: coreSetup.analytics,
});

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

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) =>
space.disabledFeatures.includes(INVENTORY_APP_ID) ||
!coreStart.application.capabilities.inventory.show
)
map((space) => this.isInventoryDisabledByUserOrSpace(space, coreStart))
);
}

Expand All @@ -80,7 +121,6 @@ export class InventoryPlugin
const sections$ = hideInventory$.pipe(
map((hideInventory) => {
if (isEntityCentricExperienceSettingEnabled && !hideInventory) {
registerEemEnabledContext(coreSetup.analytics, pluginsSetup.entityManager);
return [
{
label: '',
Expand All @@ -107,9 +147,6 @@ export class InventoryPlugin

pluginsSetup.observabilityShared.navigation.registerSections(sections$);

this.telemetry.setup({ analytics: coreSetup.analytics });
const telemetry = this.telemetry.start();

const isCloudEnv = !!pluginsSetup.cloud?.isCloudEnabled;
const isServerlessEnv = pluginsSetup.cloud?.isServerlessEnabled || this.isServerlessEnv;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,49 @@
*/

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

import { BehaviorSubject } from 'rxjs';
import { EntityManagerPublicPluginSetup } from '@kbn/entityManager-plugin/public';
import {
type ITelemetryClient,
TelemetryEventTypes,
type InventoryAddDataParams,
type EntityInventoryViewedParams,
type EntityInventorySearchQuerySubmittedParams,
type EntityViewClickedParams,
EntityInventoryEntityTypeFilteredParams,
type EntityInventoryEntityTypeFilteredParams,
} from './types';

export class TelemetryClient implements ITelemetryClient {
constructor(private analytics: AnalyticsServiceSetup) {}
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 });
};

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,7 +5,11 @@
* 2.0.
*/
import type { AnalyticsServiceSetup } from '@kbn/core-analytics-browser';
import type { TelemetryServiceSetupParams, ITelemetryClient, TelemetryEventParams } from './types';
import type {
TelemetryServiceSetupParams,
TelemetryEventParams,
TelemetryServiceStartParams,
} from './types';
import { inventoryTelemetryEventBasedTypes } from './telemetry_events';
import { TelemetryClient } from './telemetry_client';

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

public start(): ITelemetryClient {
public start({ entityManager }: TelemetryServiceStartParams): 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);
return new TelemetryClient(this.analytics, entityManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
*/

import type { AnalyticsServiceSetup, RootSchema } from '@kbn/core/public';
import { EntityManagerPublicPluginSetup } from '@kbn/entityManager-plugin/public';
import type { EntityType } from '../../../common/entities';

export interface TelemetryServiceSetupParams {
analytics: AnalyticsServiceSetup;
}

export interface TelemetryServiceStartParams {
entityManager: EntityManagerPublicPluginSetup;
}

export interface InventoryAddDataParams {
view: 'add_data_button' | 'empty_state';
journey?: 'add_data' | 'associate_existing_service_logs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import type { InventoryAPIClient } from '../api';
import type { ITelemetryClient } from './telemetry/types';
import { TelemetryClient } from './telemetry/telemetry_client';

export interface InventoryServices {
inventoryAPIClient: InventoryAPIClient;
telemetry: ITelemetryClient;
telemetry: TelemetryClient;
}

0 comments on commit a4bdbb2

Please sign in to comment.