Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML][AIOps] Telemetry: track analysis endpoint usage #166988

Merged
merged 13 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions x-pack/packages/ml/response_stream/client/fetch_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import startsWith from 'lodash/startsWith';
import type { Reducer, ReducerAction } from 'react';

import type { HttpSetup } from '@kbn/core/public';
import type { HttpSetup, HttpFetchOptions } from '@kbn/core/public';

type GeneratorError = string | null;

Expand Down Expand Up @@ -42,7 +42,8 @@ export async function* fetchStream<B extends object, R extends Reducer<any, any>
apiVersion: string | undefined,
abortCtrl: React.MutableRefObject<AbortController>,
body?: B,
ndjson = true
ndjson = true,
headers?: HttpFetchOptions['headers']
): AsyncGenerator<[GeneratorError, ReducerAction<R> | Array<ReducerAction<R>> | undefined]> {
let stream: Readonly<Response> | undefined;

Expand All @@ -52,6 +53,7 @@ export async function* fetchStream<B extends object, R extends Reducer<any, any>
version: apiVersion,
asResponse: true,
rawResponse: true,
headers,
...(body && Object.keys(body).length > 0 ? { body: JSON.stringify(body) } : {}),
});

Expand Down
8 changes: 5 additions & 3 deletions x-pack/packages/ml/response_stream/client/use_fetch_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from 'react';
import useThrottle from 'react-use/lib/useThrottle';

import type { HttpSetup } from '@kbn/core/public';
import type { HttpSetup, HttpFetchOptions } from '@kbn/core/public';
import { isPopulatedObject } from '@kbn/ml-is-populated-object';

import { fetchStream } from './fetch_stream';
Expand Down Expand Up @@ -64,7 +64,8 @@ export function useFetchStream<B extends object, R extends Reducer<any, any>>(
endpoint: string,
apiVersion?: string,
body?: B,
customReducer?: FetchStreamCustomReducer<R>
customReducer?: FetchStreamCustomReducer<R>,
headers?: HttpFetchOptions['headers']
) {
const [errors, setErrors] = useState<string[]>([]);
const [isCancelled, setIsCancelled] = useState(false);
Expand Down Expand Up @@ -104,7 +105,8 @@ export function useFetchStream<B extends object, R extends Reducer<any, any>>(
apiVersion,
abortCtrl,
body,
customReducer !== undefined
customReducer !== undefined,
headers
)) {
if (fetchStreamError !== null) {
addError(fetchStreamError);
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/aiops/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export const RANDOM_SAMPLER_SEED = 3867412;
export const CASES_ATTACHMENT_CHANGE_POINT_CHART = 'aiopsChangePointChart';

export const EMBEDDABLE_CHANGE_POINT_CHART_TYPE = 'aiopsChangePointChart' as const;

export const AIOPS_TELEMETRY_ID = {
AIOPS_DEFAULT_SOURCE: 'ml_aiops_labs',
AIOPS_ANALYSIS_RUN_ORIGIN: 'aiops-analysis-run-origin',
} as const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the continued nitpicking 😅 — looking at the attributes now they serve different purposes so we probably shouldn't combine them all into one enum. Suggest to break out the run ones.

If I read the code right AIOPS_ANALYSIS_RUN_ORIGIN is now the header name? Suggest to prefix with kbn- then. We now also have a mix of source and origin in the naming. Related to that, if you align this, maybe we can also make the prop name source more specific, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header name can't begin with 'kbn' - I got an error. But good call on renaming the prop to match up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed prop name to 'embeddingOrigin' in c973e76

3 changes: 2 additions & 1 deletion x-pack/plugins/aiops/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"unifiedSearch"
],
"optionalPlugins": [
"cases"
"cases",
"usageCollection"
],
"requiredBundles": [
"fieldFormats",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Storage } from '@kbn/kibana-utils-plugin/public';
import { DatePickerContextProvider, type DatePickerDependencies } from '@kbn/ml-date-picker';
import { UI_SETTINGS } from '@kbn/data-plugin/common';

import { AIOPS_TELEMETRY_ID } from '../../../common/constants';
import { DataSourceContext } from '../../hooks/use_data_source';
import type { AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import { AIOPS_STORAGE_KEYS } from '../../types/storage';
Expand Down Expand Up @@ -65,7 +66,7 @@ export const LogCategorizationAppState: FC<LogCategorizationAppStateProps> = ({
<DataSourceContext.Provider value={{ dataView, savedSearch }}>
<StorageContextProvider storage={localStorage} storageKeys={AIOPS_STORAGE_KEYS}>
<DatePickerContextProvider {...datePickerDeps}>
<LogCategorizationPage />
<LogCategorizationPage embeddingOrigin={AIOPS_TELEMETRY_ID.AIOPS_DEFAULT_SOURCE} />
</DatePickerContextProvider>
</StorageContextProvider>
</DataSourceContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { buildEmptyFilter, Filter } from '@kbn/es-query';

import { usePageUrlState } from '@kbn/ml-url-state';
import type { FieldValidationResults } from '@kbn/ml-category-validator';
import { AIOPS_TELEMETRY_ID } from '../../../common/constants';
import { useData } from '../../hooks/use_data';
import { useSearch } from '../../hooks/use_search';
import { useCategorizeRequest } from './use_categorize_request';
Expand All @@ -45,6 +46,8 @@ export interface LogCategorizationPageProps {
savedSearch: SavedSearch | null;
selectedField: DataViewField;
onClose: () => void;
/** Identifier to indicate the plugin utilizing the component */
embeddingOrigin: string;
}

const BAR_TARGET = 20;
Expand All @@ -54,6 +57,7 @@ export const LogCategorizationFlyout: FC<LogCategorizationPageProps> = ({
savedSearch,
selectedField,
onClose,
embeddingOrigin,
}) => {
const {
notifications: { toasts },
Expand Down Expand Up @@ -147,7 +151,8 @@ export const LogCategorizationFlyout: FC<LogCategorizationPageProps> = ({
timeField,
earliest,
latest,
searchQuery
searchQuery,
{ [AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin }
),
runCategorizeRequest(
index,
Expand Down Expand Up @@ -189,6 +194,7 @@ export const LogCategorizationFlyout: FC<LogCategorizationPageProps> = ({
runCategorizeRequest,
intervalMs,
toasts,
embeddingOrigin,
]);

const onAddFilter = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { usePageUrlState, useUrlState } from '@kbn/ml-url-state';

import type { FieldValidationResults } from '@kbn/ml-category-validator';
import type { SearchQueryLanguage } from '@kbn/ml-query-utils';
import { AIOPS_TELEMETRY_ID } from '../../../common/constants';
import { useDataSource } from '../../hooks/use_data_source';
import { useData } from '../../hooks/use_data';
import { useSearch } from '../../hooks/use_search';
Expand All @@ -51,7 +52,12 @@ import { FieldValidationCallout } from './category_validation_callout';
const BAR_TARGET = 20;
const DEFAULT_SELECTED_FIELD = 'message';

export const LogCategorizationPage: FC = () => {
interface LogCategorizationPageProps {
/** Identifier to indicate the plugin utilizing the component */
embeddingOrigin: string;
}

export const LogCategorizationPage: FC<LogCategorizationPageProps> = ({ embeddingOrigin }) => {
const {
notifications: { toasts },
} = useAiopsAppContext();
Expand Down Expand Up @@ -206,7 +212,10 @@ export const LogCategorizationPage: FC = () => {

try {
const [validationResult, categorizationResult] = await Promise.all([
runValidateFieldRequest(index, selectedField, timeField, earliest, latest, searchQuery),
runValidateFieldRequest(index, selectedField, timeField, earliest, latest, searchQuery, {
[AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
}),

runCategorizeRequest(
index,
selectedField,
Expand Down Expand Up @@ -243,6 +252,7 @@ export const LogCategorizationPage: FC = () => {
runCategorizeRequest,
intervalMs,
toasts,
embeddingOrigin,
]);

useEffect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function showCategorizeFlyout(
savedSearch={null}
selectedField={field}
onClose={onFlyoutClose}
embeddingOrigin={'discover_run_pattern_analysis'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I don't think hardcoding the ID here is sufficient. This flyout could be (in the future) opened from other places in kibana.
To get the real origin of the action, you'll need to have the value passed into this component as a prop and set by the ui action that has trigged it.
The context that is passed to the action's execute function does have an originatingApp variable which I added just in case when this was first written.
This has the value discover when called from Discover.

/>
</StorageContextProvider>
</DatePickerContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useRef, useCallback } from 'react';

import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import type { FieldValidationResults } from '@kbn/ml-category-validator';
import type { HttpFetchOptions } from '@kbn/core/public';
import { AIOPS_API_ENDPOINT } from '../../../common/api';
import { useAiopsAppContext } from '../../hooks/use_aiops_app_context';
import { createCategorizeQuery } from './use_categorize_request';
Expand All @@ -24,7 +25,8 @@ export function useValidateFieldRequest() {
timeField: string,
start: number | undefined,
end: number | undefined,
queryIn: QueryDslQueryContainer
queryIn: QueryDslQueryContainer,
headers?: HttpFetchOptions['headers']
) => {
const query = createCategorizeQuery(queryIn, timeField, start, end);
const resp = await http.post<FieldValidationResults>(
Expand All @@ -45,6 +47,7 @@ export function useValidateFieldRequest() {
indicesOptions: undefined,
includeExamples: false,
}),
headers,
version: '1',
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export interface LogRateAnalysisContentProps {
barHighlightColorOverride?: string;
/** Optional callback that exposes data of the completed analysis */
onAnalysisCompleted?: (d: LogRateAnalysisResultsData) => void;
/** Identifier to indicate the plugin utilizing the component */
embeddingOrigin: string;
}

export const LogRateAnalysisContent: FC<LogRateAnalysisContentProps> = ({
Expand All @@ -76,6 +78,7 @@ export const LogRateAnalysisContent: FC<LogRateAnalysisContentProps> = ({
barColorOverride,
barHighlightColorOverride,
onAnalysisCompleted,
embeddingOrigin,
}) => {
const [windowParameters, setWindowParameters] = useState<WindowParameters | undefined>();
const [initialAnalysisStart, setInitialAnalysisStart] = useState<
Expand Down Expand Up @@ -172,6 +175,7 @@ export const LogRateAnalysisContent: FC<LogRateAnalysisContentProps> = ({
barColorOverride={barColorOverride}
barHighlightColorOverride={barHighlightColorOverride}
onAnalysisCompleted={onAnalysisCompleted}
embeddingOrigin={embeddingOrigin}
/>
)}
{windowParameters === undefined && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export interface LogRateAnalysisContentWrapperProps {
onAnalysisCompleted?: (d: LogRateAnalysisResultsData) => void;
/** Optional flag to indicate whether kibana is running in serverless */
showFrozenDataTierChoice?: boolean;
/** Identifier to indicate the plugin utilizing the component */
embeddingOrigin: string;
}

export const LogRateAnalysisContentWrapper: FC<LogRateAnalysisContentWrapperProps> = ({
Expand All @@ -73,6 +75,7 @@ export const LogRateAnalysisContentWrapper: FC<LogRateAnalysisContentWrapperProp
barHighlightColorOverride,
onAnalysisCompleted,
showFrozenDataTierChoice = true,
embeddingOrigin,
}) => {
if (!dataView) return null;

Expand Down Expand Up @@ -105,6 +108,7 @@ export const LogRateAnalysisContentWrapper: FC<LogRateAnalysisContentWrapperProp
barColorOverride={barColorOverride}
barHighlightColorOverride={barHighlightColorOverride}
onAnalysisCompleted={onAnalysisCompleted}
embeddingOrigin={embeddingOrigin}
/>
</DatePickerContextProvider>
</StorageContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getDefaultAiOpsListState,
type AiOpsPageUrlState,
} from '../../application/utils/url_state';
import { AIOPS_TELEMETRY_ID } from '../../../common/constants';

import { SearchPanel } from '../search_panel';
import { useLogRateAnalysisResultsTableRowContext } from '../log_rate_analysis_results_table/log_rate_analysis_results_table_row_provider';
Expand Down Expand Up @@ -151,6 +152,7 @@ export const LogRateAnalysisPage: FC<Props> = ({ stickyHistogram }) => {
setGlobalState={setGlobalState}
esSearchQuery={searchQuery}
stickyHistogram={stickyHistogram}
embeddingOrigin={AIOPS_TELEMETRY_ID.AIOPS_DEFAULT_SOURCE}
/>
</EuiFlexGroup>
</EuiPageSection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { SignificantTerm, SignificantTermGroup } from '@kbn/ml-agg-utils';
import { useAiopsAppContext } from '../../hooks/use_aiops_app_context';
import { initialState, streamReducer } from '../../../common/api/stream_reducer';
import type { AiopsApiLogRateAnalysis } from '../../../common/api';
import { AIOPS_TELEMETRY_ID } from '../../../common/constants';
import {
getGroupTableItems,
LogRateAnalysisResultsTable,
Expand Down Expand Up @@ -113,6 +114,8 @@ interface LogRateAnalysisResultsProps {
barHighlightColorOverride?: string;
/** Optional callback that exposes data of the completed analysis */
onAnalysisCompleted?: (d: LogRateAnalysisResultsData) => void;
/** Identifier to indicate the plugin utilizing the component */
embeddingOrigin: string;
}

export const LogRateAnalysisResults: FC<LogRateAnalysisResultsProps> = ({
Expand All @@ -129,6 +132,7 @@ export const LogRateAnalysisResults: FC<LogRateAnalysisResultsProps> = ({
barColorOverride,
barHighlightColorOverride,
onAnalysisCompleted,
embeddingOrigin,
}) => {
const { http } = useAiopsAppContext();

Expand Down Expand Up @@ -198,7 +202,8 @@ export const LogRateAnalysisResults: FC<LogRateAnalysisResultsProps> = ({
overrides,
sampleProbability,
},
{ reducer: streamReducer, initialState }
{ reducer: streamReducer, initialState },
{ [AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin }
);

const { significantTerms } = data;
Expand Down
32 changes: 32 additions & 0 deletions x-pack/plugins/aiops/server/lib/track_route_usage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { usageCountersServiceMock } from '@kbn/usage-collection-plugin/server/usage_counters/usage_counters_service.mock';
import { trackAIOpsRouteUsage } from './track_route_usage';

describe('trackAIOpsRouteUsage', () => {
it('should call `usageCounter.incrementCounter`', () => {
const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract();
const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test');

trackAIOpsRouteUsage('test_type', 'test_source', mockUsageCounter);
expect(mockUsageCounter.incrementCounter).toHaveBeenCalledWith({
counterName: 'test_type',
counterType: 'run_via_test_source',
incrementBy: 1,
});
});

it('should do nothing if no usage counter is provided', () => {
let err;
try {
trackAIOpsRouteUsage('test', undefined);
} catch (e) {
err = e;
}
expect(err).toBeUndefined();
});
});
22 changes: 22 additions & 0 deletions x-pack/plugins/aiops/server/lib/track_route_usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { UsageCounter } from '@kbn/usage-collection-plugin/server';

export function trackAIOpsRouteUsage(
analysisType: string,
source?: string | string[],
usageCounter?: UsageCounter
) {
if (usageCounter && typeof source === 'string') {
usageCounter.incrementCounter({
counterName: analysisType,
counterType: `run_via_${source}`,
incrementBy: 1,
});
}
}
Loading