Skip to content

Commit

Permalink
Add host limit telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Apr 25, 2023
1 parent b0c7c2d commit 2224bf6
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const configSchema = schema.object({
'Host Entry Clicked', // Worst-case scenario once per second - AT RISK,
'Host Flyout Filter Removed', // Worst-case scenario once per second - AT RISK,
'Host Flyout Filter Added', // Worst-case scenario once per second - AT RISK,
'Host View Total Host Count Retrieved', // Worst-case scenario 1 every 2 seconds
],
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const HOSTS_CHART: Omit<Props, 'loading' | 'value'> = {
toolTip: i18n.translate('xpack.infra.hostsViewPage.metricTrend.hostCount.tooltip', {
defaultMessage: 'The number of hosts returned by your current search criteria.',
}),
['data-test-subj']: 'hostsView-metricsTrend-hosts',
['data-test-subj']: 'hostsViewKPIGridHostsCountTile',
};

export const HostsTile = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const KPIGrid = () => {
direction="row"
gutterSize="s"
style={{ flexGrow: 0 }}
data-test-subj="hostsView-metricsTrend"
data-test-subj="hostsViewKPIGrid"
>
<EuiFlexItem>
<HostsTile />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const Tile = ({
hasShadow={false}
paddingSize={error ? 'm' : 'none'}
style={{ minHeight: MIN_HEIGHT }}
data-test-subj={`hostsView-metricsTrend-${type}`}
data-test-subj={`hostsViewKPIGrid${type}Tile`}
>
{error ? (
<EuiFlexGroup
Expand Down Expand Up @@ -149,7 +149,7 @@ export const Tile = ({
anchorClassName="eui-fullWidth"
>
<LensWrapper
id={`hostViewKPIChart-${type}`}
id={`hostsViewKPIGrid${type}Tile`}
attributes={afterLoadedState.attributes}
style={{ height: MIN_HEIGHT }}
extraActions={[extraActionOptions.openInLens]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ const options: EuiButtonGroupOptionProps[] = HOST_LIMIT_OPTIONS.map((option) =>
id: buildId(option),
label: `${option}`,
value: option,
'data-test-subj': `hostsViewLimitSelection${option}button`,
'data-test-subj': `hostsViewLimitSelection${option}Button`,
}));
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import { useCallback, useEffect } from 'react';
import { catchError, map, Observable, of, startWith } from 'rxjs';
import createContainer from 'constate';
import type { QueryDslQueryContainer, SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana';
import { decodeOrThrow } from '../../../../../common/runtime_types';
import { useDataSearch, useLatestPartialDataSearchResponse } from '../../../../utils/data_search';
import { useMetricsDataViewContext } from './use_data_view';
import { useUnifiedSearchContext } from './use_unified_search';

export const useHostCount = () => {
const { dataView, metricAlias } = useMetricsDataViewContext();
const {
services: { telemetry },
} = useKibanaContextForPlugin();
const { buildQuery, getParsedDateRange } = useUnifiedSearchContext();

const { search: fetchHostCount, requests$ } = useDataSearch({
Expand Down Expand Up @@ -81,6 +85,14 @@ export const useHostCount = () => {
fetchHostCount();
}, [fetchHostCount]);

useEffect(() => {
if (latestResponseData) {
telemetry.reportHostsViewTotalHostCountRetrieved({
total: latestResponseData.count.value,
});
}
}, [latestResponseData, telemetry]);

return {
errors: latestResponseErrors,
isRequestRunning,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
InfraAssetMetricType,
} from '../../../../../common/http_api';
import { useHostFlyoutOpen } from './use_host_flyout_open_url_state';
import { Sorting, useHostsTableProperties } from './use_hosts_table_url_state';
import { Sorting, useHostsTableUrlState } from './use_hosts_table_url_state';
import { useHostsViewContext } from './use_hosts_view';
import { useUnifiedSearchContext } from './use_unified_search';

Expand Down Expand Up @@ -166,7 +166,7 @@ const toggleDialogActionLabel = i18n.translate(
export const useHostsTable = () => {
const { hostNodes } = useHostsViewContext();
const { searchCriteria } = useUnifiedSearchContext();
const [{ pagination, sorting }, setProperties] = useHostsTableProperties();
const [{ pagination, sorting }, setProperties] = useHostsTableUrlState();
const {
services: { telemetry },
} = useKibanaContextForPlugin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const reducer = (prevState: TableProperties, params: Payload) => {
};
};

export const useHostsTableProperties = (): [TableProperties, TablePropertiesUpdater] => {
export const useHostsTableUrlState = (): [TableProperties, TablePropertiesUpdater] => {
const [localStoragePageSize, setLocalStoragePageSize] = useLocalStorage<number>(
LOCAL_STORAGE_PAGE_SIZE_KEY,
DEFAULT_PAGE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import {
const buildQuerySubmittedPayload = (
hostState: HostsState & { parsedDateRange: StringDateRangeTimestamp }
) => {
const { panelFilters, filters, parsedDateRange, query: queryObj } = hostState;
const { panelFilters, filters, parsedDateRange, query: queryObj, limit } = hostState;

return {
control_filters: panelFilters.map((filter) => JSON.stringify(filter)),
filters: filters.map((filter) => JSON.stringify(filter)),
interval: telemetryTimeRangeFormatter(parsedDateRange.to - parsedDateRange.from),
query: queryObj.query,
limit,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export const createTelemetryClientMock = (): jest.Mocked<ITelemetryClient> => ({
reportHostsViewQuerySubmitted: jest.fn(),
reportHostFlyoutFilterRemoved: jest.fn(),
reportHostFlyoutFilterAdded: jest.fn(),
reportHostsViewTotalHostCountRetrieved: jest.fn(),
});
10 changes: 10 additions & 0 deletions x-pack/plugins/infra/public/services/telemetry/telemetry_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AnalyticsServiceSetup } from '@kbn/core-analytics-server';
import {
HostEntryClickedParams,
HostFlyoutFilterActionParams,
HostsViewQueryHostsCountRetrievedParams,
HostsViewQuerySubmittedParams,
InfraTelemetryEventTypes,
ITelemetryClient,
Expand Down Expand Up @@ -50,4 +51,13 @@ export class TelemetryClient implements ITelemetryClient {
public reportHostsViewQuerySubmitted = (params: HostsViewQuerySubmittedParams) => {
this.analytics.reportEvent(InfraTelemetryEventTypes.HOSTS_VIEW_QUERY_SUBMITTED, params);
};

public reportHostsViewTotalHostCountRetrieved(
params: HostsViewQueryHostsCountRetrievedParams
): void {
this.analytics.reportEvent(
InfraTelemetryEventTypes.HOST_VIEW_TOTAL_HOST_COUNT_RETRIEVED,
params
);
}
}
22 changes: 22 additions & 0 deletions x-pack/plugins/infra/public/services/telemetry/telemetry_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ const hostsViewQuerySubmittedEvent: InfraTelemetryEvent = {
optional: false,
},
},
limit: {
type: 'integer',
_meta: {
description: 'Selected host limit',
optional: false,
},
},
},
};

Expand Down Expand Up @@ -78,6 +85,7 @@ const hostFlyoutRemoveFilter: InfraTelemetryEvent = {
},
},
};

const hostFlyoutAddFilter: InfraTelemetryEvent = {
eventType: InfraTelemetryEventTypes.HOST_FLYOUT_FILTER_ADDED,
schema: {
Expand All @@ -91,9 +99,23 @@ const hostFlyoutAddFilter: InfraTelemetryEvent = {
},
};

const hostViewTotalHostCountRetrieved: InfraTelemetryEvent = {
eventType: InfraTelemetryEventTypes.HOST_VIEW_TOTAL_HOST_COUNT_RETRIEVED,
schema: {
total: {
type: 'integer',
_meta: {
description: 'Total number of hosts retrieved.',
optional: false,
},
},
},
};

export const infraTelemetryEvents = [
hostsViewQuerySubmittedEvent,
hostsEntryClickedEvent,
hostFlyoutRemoveFilter,
hostFlyoutAddFilter,
hostViewTotalHostCountRetrieved,
];
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('TelemetryService', () => {
filters: [],
interval: 'interval(now-1h)',
query: '',
limit: 100,
});

expect(setupParams.analytics.reportEvent).toHaveBeenCalledTimes(1);
Expand All @@ -117,6 +118,7 @@ describe('TelemetryService', () => {
filters: [],
interval: 'interval(now-1h)',
query: '',
limit: 100,
}
);
});
Expand Down Expand Up @@ -161,4 +163,24 @@ describe('TelemetryService', () => {
);
});
});

describe('#reportHostsViewTotalHostCountRetrieved', () => {
it('should report Host Flyout Filter Added click with field name', async () => {
const setupParams = getSetupParams();
service.setup(setupParams);
const telemetry = service.start();

telemetry.reportHostsViewTotalHostCountRetrieved({
total: 300,
});

expect(setupParams.analytics.reportEvent).toHaveBeenCalledTimes(1);
expect(setupParams.analytics.reportEvent).toHaveBeenCalledWith(
InfraTelemetryEventTypes.HOST_VIEW_TOTAL_HOST_COUNT_RETRIEVED,
{
total: 300,
}
);
});
});
});
14 changes: 13 additions & 1 deletion x-pack/plugins/infra/public/services/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export enum InfraTelemetryEventTypes {
HOSTS_ENTRY_CLICKED = 'Host Entry Clicked',
HOST_FLYOUT_FILTER_REMOVED = 'Host Flyout Filter Removed',
HOST_FLYOUT_FILTER_ADDED = 'Host Flyout Filter Added',
HOST_VIEW_TOTAL_HOST_COUNT_RETRIEVED = 'Host View Total Host Count Retrieved',
}

export interface HostsViewQuerySubmittedParams {
control_filters: string[];
filters: string[];
interval: string;
query: string | { [key: string]: any };
limit: number;
}

export interface HostEntryClickedParams {
Expand All @@ -35,15 +37,21 @@ export interface HostFlyoutFilterActionParams {
field_name: string;
}

export interface HostsViewQueryHostsCountRetrievedParams {
total: number;
}

export type InfraTelemetryEventParams =
| HostsViewQuerySubmittedParams
| HostEntryClickedParams
| HostFlyoutFilterActionParams;
| HostFlyoutFilterActionParams
| HostsViewQueryHostsCountRetrievedParams;

export interface ITelemetryClient {
reportHostEntryClicked(params: HostEntryClickedParams): void;
reportHostFlyoutFilterRemoved(params: HostFlyoutFilterActionParams): void;
reportHostFlyoutFilterAdded(params: HostFlyoutFilterActionParams): void;
reportHostsViewTotalHostCountRetrieved(params: HostsViewQueryHostsCountRetrievedParams): void;
reportHostsViewQuerySubmitted(params: HostsViewQuerySubmittedParams): void;
}

Expand All @@ -63,4 +71,8 @@ export type InfraTelemetryEvent =
| {
eventType: InfraTelemetryEventTypes.HOSTS_ENTRY_CLICKED;
schema: RootSchema<HostEntryClickedParams>;
}
| {
eventType: InfraTelemetryEventTypes.HOST_VIEW_TOTAL_HOST_COUNT_RETRIEVED;
schema: RootSchema<HostsViewQueryHostsCountRetrievedParams>;
};

0 comments on commit 2224bf6

Please sign in to comment.