Skip to content

Commit

Permalink
generic reportEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Oct 21, 2024
1 parent 082a897 commit 919679f
Show file tree
Hide file tree
Showing 72 changed files with 383 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { StartServices } from '../../types';
import { enhanceActionWithTelemetry } from './telemetry';
import { createAction } from '@kbn/ui-actions-plugin/public';
import type { CellActionExecutionContext } from '@kbn/cell-actions';
import { TelemetryEventTypes } from '../../common/lib/telemetry/constants';

const actionId = 'test_action_id';
const displayName = 'test-actions';
Expand All @@ -29,13 +30,13 @@ const context = {

describe('enhanceActionWithTelemetry', () => {
it('calls telemetry report when the action is executed', () => {
const telemetry = { reportCellActionClicked: jest.fn() };
const telemetry = { reportEvent: jest.fn() };
const services = { telemetry } as unknown as StartServices;

const enhancedAction = enhanceActionWithTelemetry(action, services);
enhancedAction.execute(context);

expect(telemetry.reportCellActionClicked).toHaveBeenCalledWith({
expect(telemetry.reportEvent).toHaveBeenCalledWith(TelemetryEventTypes.CellActionClicked, {
displayName,
actionId,
fieldName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { CellAction, CellActionExecutionContext } from '@kbn/cell-actions';
import type { ActionExecutionContext } from '@kbn/ui-actions-plugin/public';
import type { StartServices } from '../../types';
import type { SecurityCellActionExecutionContext } from './types';
import { TelemetryEventTypes } from '../../common/lib/telemetry/constants';

export const enhanceActionWithTelemetry = (
action: CellAction<CellActionExecutionContext>,
Expand All @@ -19,7 +20,7 @@ export const enhanceActionWithTelemetry = (
const enhancedExecute = (
context: ActionExecutionContext<SecurityCellActionExecutionContext>
): Promise<void> => {
telemetry.reportCellActionClicked({
telemetry.reportEvent(TelemetryEventTypes.CellActionClicked, {
actionId: rest.id,
displayName: rest.getDisplayName(context),
fieldName: context.data.map(({ field }) => field.name).join(', '),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import { type AssistantTelemetry } from '@kbn/elastic-assistant';
import { useCallback } from 'react';
import { useKibana } from '../../common/lib/kibana';
import { useBaseConversations } from '../use_conversation_store';

import type {
ReportAssistantInvokedParams,
ReportAssistantMessageSentParams,
ReportAssistantQuickPromptParams,
ReportAssistantSettingToggledParams,
} from '../../common/lib/telemetry';
import { TelemetryEventTypes } from '../../common/lib/telemetry';
export const useAssistantTelemetry = (): AssistantTelemetry => {
const {
services: { telemetry },
Expand All @@ -27,27 +33,30 @@ export const useAssistantTelemetry = (): AssistantTelemetry => {

const reportTelemetry = useCallback(
async ({
fn,
eventType,
params: { conversationId, ...rest },
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
any): Promise<{
fn: keyof AssistantTelemetry;
params: AssistantTelemetry[keyof AssistantTelemetry];
}> =>
fn({
}: {
eventType: TelemetryEventTypes;
params:
| ReportAssistantInvokedParams
| ReportAssistantMessageSentParams
| ReportAssistantQuickPromptParams;
}) =>
telemetry.reportEvent(eventType, {
...rest,
conversationId: await getAnonymizedConversationTitle(conversationId),
}),
[getAnonymizedConversationTitle]
[getAnonymizedConversationTitle, telemetry]
);

return {
reportAssistantInvoked: (params) =>
reportTelemetry({ fn: telemetry.reportAssistantInvoked, params }),
reportAssistantMessageSent: (params) =>
reportTelemetry({ fn: telemetry.reportAssistantMessageSent, params }),
reportAssistantQuickPrompt: (params) =>
reportTelemetry({ fn: telemetry.reportAssistantQuickPrompt, params }),
reportAssistantSettingToggled: (params) => telemetry.reportAssistantSettingToggled(params),
reportAssistantInvoked: (params: ReportAssistantInvokedParams) =>
reportTelemetry({ eventType: TelemetryEventTypes.AssistantInvoked, params }),
reportAssistantMessageSent: (params: ReportAssistantMessageSentParams) =>
reportTelemetry({ eventType: TelemetryEventTypes.AssistantMessageSent, params }),
reportAssistantQuickPrompt: (params: ReportAssistantQuickPromptParams) =>
reportTelemetry({ eventType: TelemetryEventTypes.AssistantQuickPrompt, params }),
reportAssistantSettingToggled: (params: ReportAssistantSettingToggledParams) =>
telemetry.reportEvent(TelemetryEventTypes.AssistantSettingToggled, params),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import * as timelineMarkdownPlugin from '../../common/components/markdown_editor
import { useFetchAlertData } from './use_fetch_alert_data';
import { useUpsellingMessage } from '../../common/hooks/use_upselling';
import { useFetchNotes } from '../../notes/hooks/use_fetch_notes';
import { TelemetryEventTypes } from '../../common/lib/telemetry/constants';

const CaseContainerComponent: React.FC = () => {
const { cases, telemetry } = useKibana().services;
Expand Down Expand Up @@ -63,7 +64,7 @@ const CaseContainerComponent: React.FC = () => {
},
},
});
telemetry.reportDetailsFlyoutOpened({
telemetry.reportEvent(TelemetryEventTypes.DetailsFlyoutOpened, {
location: TimelineId.casePage,
panel: 'right',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { ColumnHeaderOptions, OnRowSelected } from '../../../../../common/t
import { useIsExperimentalFeatureEnabled } from '../../../hooks/use_experimental_features';
import { useTourContext } from '../../guided_onboarding_tour';
import { AlertsCasesTourSteps, SecurityStepId } from '../../guided_onboarding_tour/tour_config';
import { TelemetryEventTypes } from '../../../lib/telemetry/constants';

export type RowActionProps = EuiDataGridCellValueElementProps & {
columnHeaders: ColumnHeaderOptions[];
Expand Down Expand Up @@ -109,7 +110,7 @@ const RowActionComponent = ({
},
},
});
telemetry.reportDetailsFlyoutOpened({
telemetry.reportEvent(TelemetryEventTypes.DetailsFlyoutOpened, {
location: tableId,
panel: 'right',
});
Expand Down Expand Up @@ -137,10 +138,10 @@ const RowActionComponent = ({
},
},
});
telemetry.reportOpenNoteInExpandableFlyoutClicked({
telemetry.reportEvent(TelemetryEventTypes.OpenNoteInExpandableFlyoutClicked, {
location: tableId,
});
telemetry.reportDetailsFlyoutOpened({
telemetry.reportEvent(TelemetryEventTypes.DetailsFlyoutOpened, {
location: tableId,
panel: 'left',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
import type { HostsTableType } from '../../../explore/hosts/store/model';
import type { UsersTableType } from '../../../explore/users/store/model';
import { useGetSecuritySolutionLinkProps, withSecuritySolutionLink } from './link_props';
import { TelemetryEventTypes } from '../../lib/telemetry/constants';

export { useSecuritySolutionLinkProps, type GetSecuritySolutionLinkProps } from './link_props';
export { LinkButton, LinkAnchor } from './helpers';
Expand Down Expand Up @@ -96,7 +97,7 @@ const UserDetailsLinkComponent: React.FC<{

const onClick = useCallback(
(e: SyntheticEvent) => {
telemetry.reportEntityDetailsClicked({ entity: 'user' });
telemetry.reportEvent(TelemetryEventTypes.EntityDetailsClicked, { entity: 'user' });
const callback = onClickParam ?? goToUsersDetails;
callback(e);
},
Expand Down Expand Up @@ -173,7 +174,7 @@ const HostDetailsLinkComponent: React.FC<HostDetailsLinkProps> = ({

const onClick = useCallback(
(e: SyntheticEvent) => {
telemetry.reportEntityDetailsClicked({ entity: 'host' });
telemetry.reportEvent(TelemetryEventTypes.EntityDetailsClicked, { entity: 'host' });

const callback = onClickParam ?? goToHostDetails;
callback(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { TestProviders } from '../../../mock';

import type { SecurityJob } from '../types';
import { createTelemetryServiceMock } from '../../../lib/telemetry/telemetry_service.mock';
import { ML_JOB_TELEMETRY_STATUS } from '../../../lib/telemetry';
import { ML_JOB_TELEMETRY_STATUS, TelemetryEventTypes } from '../../../lib/telemetry';

const wrapper = ({ children }: { children: React.ReactNode }) => (
<TestProviders>{children}</TestProviders>
Expand Down Expand Up @@ -188,14 +188,14 @@ describe('useSecurityJobsHelpers', () => {
await result.current.enableDatafeed(JOB, TIMESTAMP);
});

expect(mockedTelemetry.reportMLJobUpdate).toHaveBeenCalledWith({
expect(mockedTelemetry.reportEvent).toHaveBeenCalledWith(TelemetryEventTypes.MLJobUpdate, {
status: ML_JOB_TELEMETRY_STATUS.moduleInstalled,
isElasticJob: true,
jobId,
moduleId,
});

expect(mockedTelemetry.reportMLJobUpdate).toHaveBeenCalledWith({
expect(mockedTelemetry.reportEvent).toHaveBeenCalledWith(TelemetryEventTypes.MLJobUpdate, {
status: ML_JOB_TELEMETRY_STATUS.started,
isElasticJob: true,
jobId,
Expand All @@ -211,7 +211,7 @@ describe('useSecurityJobsHelpers', () => {
await result.current.enableDatafeed({ ...JOB, isInstalled: true }, TIMESTAMP);
});

expect(mockedTelemetry.reportMLJobUpdate).toHaveBeenCalledWith({
expect(mockedTelemetry.reportEvent).toHaveBeenCalledWith(TelemetryEventTypes.MLJobUpdate, {
status: ML_JOB_TELEMETRY_STATUS.startError,
errorMessage: 'Start job failure - test_error',
isElasticJob: true,
Expand All @@ -228,7 +228,7 @@ describe('useSecurityJobsHelpers', () => {
await result.current.enableDatafeed(JOB, TIMESTAMP);
});

expect(mockedTelemetry.reportMLJobUpdate).toHaveBeenCalledWith({
expect(mockedTelemetry.reportEvent).toHaveBeenCalledWith(TelemetryEventTypes.MLJobUpdate, {
status: ML_JOB_TELEMETRY_STATUS.installationError,
errorMessage: 'Create job failure - test_error',
isElasticJob: true,
Expand Down Expand Up @@ -295,7 +295,7 @@ describe('useSecurityJobsHelpers', () => {
await result.current.disableDatafeed({ ...JOB, isInstalled: true });
});

expect(mockedTelemetry.reportMLJobUpdate).toHaveBeenCalledWith({
expect(mockedTelemetry.reportEvent).toHaveBeenCalledWith(TelemetryEventTypes.MLJobUpdate, {
status: ML_JOB_TELEMETRY_STATUS.stopped,
isElasticJob: true,
jobId,
Expand All @@ -311,7 +311,7 @@ describe('useSecurityJobsHelpers', () => {
await result.current.disableDatafeed({ ...JOB, isInstalled: true });
});

expect(mockedTelemetry.reportMLJobUpdate).toHaveBeenCalledWith({
expect(mockedTelemetry.reportEvent).toHaveBeenCalledWith(TelemetryEventTypes.MLJobUpdate, {
status: ML_JOB_TELEMETRY_STATUS.stopError,
errorMessage: 'Stop job failure - test_error',
isElasticJob: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
METRIC_TYPE,
ML_JOB_TELEMETRY_STATUS,
TELEMETRY_EVENT,
TelemetryEventTypes,
track,
} from '../../../lib/telemetry';

Expand Down Expand Up @@ -43,7 +44,7 @@ export const useEnableDataFeed = () => {
jobIdErrorFilter: [job.id],
groups: job.groups,
});
telemetry.reportMLJobUpdate({
telemetry.reportEvent(TelemetryEventTypes.MLJobUpdate, {
jobId: job.id,
isElasticJob: job.isElasticJob,
moduleId: job.moduleId,
Expand All @@ -52,7 +53,7 @@ export const useEnableDataFeed = () => {
} catch (error) {
setIsLoading(false);
addError(error, { title: i18n.CREATE_JOB_FAILURE });
telemetry.reportMLJobUpdate({
telemetry.reportEvent(TelemetryEventTypes.MLJobUpdate, {
jobId: job.id,
isElasticJob: job.isElasticJob,
moduleId: job.moduleId,
Expand Down Expand Up @@ -82,7 +83,7 @@ export const useEnableDataFeed = () => {
throw new Error(response[datafeedId].error);
}

telemetry.reportMLJobUpdate({
telemetry.reportEvent(TelemetryEventTypes.MLJobUpdate, {
jobId: job.id,
isElasticJob: job.isElasticJob,
status: ML_JOB_TELEMETRY_STATUS.started,
Expand All @@ -92,7 +93,7 @@ export const useEnableDataFeed = () => {
} catch (error) {
track(METRIC_TYPE.COUNT, TELEMETRY_EVENT.JOB_ENABLE_FAILURE);
addError(error, { title: i18n.START_JOB_FAILURE });
telemetry.reportMLJobUpdate({
telemetry.reportEvent(TelemetryEventTypes.MLJobUpdate, {
jobId: job.id,
isElasticJob: job.isElasticJob,
status: ML_JOB_TELEMETRY_STATUS.startError,
Expand Down Expand Up @@ -124,7 +125,7 @@ export const useEnableDataFeed = () => {
throw new Error(response.error);
}

telemetry.reportMLJobUpdate({
telemetry.reportEvent(TelemetryEventTypes.MLJobUpdate, {
jobId: job.id,
isElasticJob: job.isElasticJob,
status: ML_JOB_TELEMETRY_STATUS.stopped,
Expand All @@ -134,7 +135,7 @@ export const useEnableDataFeed = () => {
} catch (error) {
track(METRIC_TYPE.COUNT, TELEMETRY_EVENT.JOB_DISABLE_FAILURE);
addError(error, { title: i18n.STOP_JOB_FAILURE });
telemetry.reportMLJobUpdate({
telemetry.reportEvent(TelemetryEventTypes.MLJobUpdate, {
jobId: job.id,
isElasticJob: job.isElasticJob,
status: ML_JOB_TELEMETRY_STATUS.stopError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ describe('useBreadcrumbsNav', () => {
});

it('should create breadcrumbs onClick handler', () => {
const reportBreadcrumbClickedMock = jest.fn();
const reportEventMock = jest.fn();

(kibanaLib.useKibana as jest.Mock).mockImplementation(() => ({
services: {
telemetry: {
reportBreadcrumbClicked: reportBreadcrumbClickedMock,
reportEvent: reportEventMock,
},
},
}));
Expand All @@ -157,6 +157,6 @@ describe('useBreadcrumbsNav', () => {

expect(event.preventDefault).toHaveBeenCalled();
expect(mockDispatch).toHaveBeenCalled();
expect(reportBreadcrumbClickedMock).toHaveBeenCalled();
expect(reportEventMock).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { timelineActions } from '../../../../timelines/store';
import { TimelineId } from '../../../../../common/types/timeline';
import type { GetSecuritySolutionUrl } from '../../link_to';
import { useGetSecuritySolutionUrl } from '../../link_to';
import type { TelemetryClientStart } from '../../../lib/telemetry';
import { TelemetryEventTypes, type TelemetryServiceStart } from '../../../lib/telemetry';
import { useKibana, useNavigateTo, type NavigateTo } from '../../../lib/kibana';
import { useRouteSpy } from '../../../utils/route/use_route_spy';
import { updateBreadcrumbsNav } from '../../../breadcrumbs';
Expand Down Expand Up @@ -68,7 +68,7 @@ const addOnClicksHandlers = (
breadcrumbs: ChromeBreadcrumb[],
dispatch: Dispatch,
navigateTo: NavigateTo,
telemetry: TelemetryClientStart
telemetry: TelemetryServiceStart
): ChromeBreadcrumb[] =>
breadcrumbs.map((breadcrumb) => ({
...breadcrumb,
Expand All @@ -89,13 +89,13 @@ const createOnClickHandler =
href: string,
dispatch: Dispatch,
navigateTo: NavigateTo,
telemetry: TelemetryClientStart,
telemetry: TelemetryServiceStart,
title: React.ReactNode
) =>
(ev: SyntheticEvent) => {
ev.preventDefault();
if (typeof title === 'string') {
telemetry.reportBreadcrumbClicked({ title });
telemetry.reportEvent(TelemetryEventTypes.BreadcrumbClicked, { title });
}
dispatch(timelineActions.showTimeline({ id: TimelineId.active, show: false }));
navigateTo({ url: href });
Expand Down
Loading

0 comments on commit 919679f

Please sign in to comment.