Skip to content

Commit

Permalink
adds telemetry tracking to log rate analysis and log pattern analysis…
Browse files Browse the repository at this point in the history
… routes
  • Loading branch information
alvarezmelissa87 committed Sep 21, 2023
1 parent 840e121 commit a71ecb5
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 11 deletions.
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
8 changes: 8 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,11 @@ 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 LOG_RATE_ANALYSIS = 'AIOpsLogRateAnalysis';

export const LOG_PATTERN_ANALYSIS = 'AIOpsLogPatternAnalysis';

export const CHANGE_POINT_DETECTION = 'AIOpsChangePointDetection';

export const AIOPS_DEFAULT_SOURCE = 'mlAIOpsLabs';
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from '../log_rate_analysis_results';
import type { GroupTableItem } from '../../log_rate_analysis_results_table/types';
import { useLogRateAnalysisResultsTableRowContext } from '../../log_rate_analysis_results_table/log_rate_analysis_results_table_row_provider';
import { AIOPS_DEFAULT_SOURCE } from '../../../../common/constants';

const DEFAULT_SEARCH_QUERY = { match_all: {} };

Expand Down Expand Up @@ -64,6 +65,8 @@ export interface LogRateAnalysisContentProps {
barHighlightColorOverride?: string;
/** Optional callback that exposes data of the completed analysis */
onAnalysisCompleted?: (d: LogRateAnalysisResultsData) => void;
/** Optional identifyer to indicate the plugin utilizing the component */
source?: string;
}

export const LogRateAnalysisContent: FC<LogRateAnalysisContentProps> = ({
Expand All @@ -76,6 +79,7 @@ export const LogRateAnalysisContent: FC<LogRateAnalysisContentProps> = ({
barColorOverride,
barHighlightColorOverride,
onAnalysisCompleted,
source = AIOPS_DEFAULT_SOURCE,
}) => {
const [windowParameters, setWindowParameters] = useState<WindowParameters | undefined>();
const [initialAnalysisStart, setInitialAnalysisStart] = useState<
Expand Down Expand Up @@ -172,6 +176,7 @@ export const LogRateAnalysisContent: FC<LogRateAnalysisContentProps> = ({
barColorOverride={barColorOverride}
barHighlightColorOverride={barHighlightColorOverride}
onAnalysisCompleted={onAnalysisCompleted}
source={source}
/>
)}
{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 */
isServerless?: boolean;
/** Optional identifyer to indicate the plugin utilizing the component */
source?: string;
}

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

Expand Down Expand Up @@ -104,6 +107,7 @@ export const LogRateAnalysisContentWrapper: FC<LogRateAnalysisContentWrapperProp
barColorOverride={barColorOverride}
barHighlightColorOverride={barHighlightColorOverride}
onAnalysisCompleted={onAnalysisCompleted}
source={source}
/>
</DatePickerContextProvider>
</StorageContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ interface LogRateAnalysisResultsProps {
barHighlightColorOverride?: string;
/** Optional callback that exposes data of the completed analysis */
onAnalysisCompleted?: (d: LogRateAnalysisResultsData) => void;
/** Optional identifyer to indicate the plugin utilizing the component */
source?: string;
}

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

Expand Down Expand Up @@ -198,7 +201,8 @@ export const LogRateAnalysisResults: FC<LogRateAnalysisResultsProps> = ({
overrides,
sampleProbability,
},
{ reducer: streamReducer, initialState }
{ reducer: streamReducer, initialState },
{ source }
);

const { significantTerms } = data;
Expand Down
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: source,
counterType: analysisType,
incrementBy: 1,
});
}
}
8 changes: 6 additions & 2 deletions x-pack/plugins/aiops/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { Subscription } from 'rxjs';

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin, Logger } from '@kbn/core/server';
import type { DataRequestHandlerContext } from '@kbn/data-plugin/server';
import type { UsageCounter } from '@kbn/usage-collection-plugin/server';

import { CASES_ATTACHMENT_CHANGE_POINT_CHART } from '../common/constants';
import { PLUGIN_ID } from '../common';
import { isActiveLicense } from './lib/license';
import {
AiopsLicense,
Expand All @@ -28,6 +30,7 @@ export class AiopsPlugin
{
private readonly logger: Logger;
private licenseSubscription: Subscription | null = null;
private usageCounter?: UsageCounter;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
Expand All @@ -38,6 +41,7 @@ export class AiopsPlugin
plugins: AiopsPluginSetupDeps
) {
this.logger.debug('aiops: Setup');
this.usageCounter = plugins.usageCollection?.createUsageCounter(PLUGIN_ID);

// Subscribe to license changes and store the current license in `currentLicense`.
// This way we can pass on license changes to the route factory having always
Expand All @@ -51,8 +55,8 @@ export class AiopsPlugin

// Register server side APIs
core.getStartServices().then(([coreStart, depsStart]) => {
defineLogRateAnalysisRoute(router, aiopsLicense, this.logger, coreStart);
defineLogCategorizationRoutes(router, aiopsLicense);
defineLogRateAnalysisRoute(router, aiopsLicense, this.logger, coreStart, this.usageCounter);
defineLogCategorizationRoutes(router, aiopsLicense, this.usageCounter);
});

if (plugins.cases) {
Expand Down
9 changes: 8 additions & 1 deletion x-pack/plugins/aiops/server/routes/log_categorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import type { IRouter } from '@kbn/core/server';
import type { DataRequestHandlerContext } from '@kbn/data-plugin/server';
import { categorizationExamplesProvider } from '@kbn/ml-category-validator';
import type { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { categorizationFieldValidationSchema } from '../../common/api/log_categorization/schema';
import { AIOPS_API_ENDPOINT } from '../../common/api';
import type { AiopsLicense } from '../types';
import { wrapError } from './error_wrapper';
import { trackAIOpsRouteUsage } from '../lib/track_route_usage';
import { LOG_PATTERN_ANALYSIS } from '../../common/constants';

export const defineLogCategorizationRoutes = (
router: IRouter<DataRequestHandlerContext>,
license: AiopsLicense
license: AiopsLicense,
usageCounter?: UsageCounter
) => {
router.versioned
.post({
Expand All @@ -32,6 +36,9 @@ export const defineLogCategorizationRoutes = (
},
},
async (context, request, response) => {
const { headers } = request;
trackAIOpsRouteUsage(LOG_PATTERN_ANALYSIS, headers.source, usageCounter);

if (!license.isActivePlatinumLicense) {
return response.forbidden();
}
Expand Down
10 changes: 8 additions & 2 deletions x-pack/plugins/aiops/server/routes/log_rate_analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import type {
} from '@kbn/ml-agg-utils';
import { fetchHistogramsForFields } from '@kbn/ml-agg-utils';
import { createExecutionContext } from '@kbn/ml-route-utils';
import type { UsageCounter } from '@kbn/usage-collection-plugin/server';

import { RANDOM_SAMPLER_SEED } from '../../common/constants';
import { RANDOM_SAMPLER_SEED, LOG_RATE_ANALYSIS } from '../../common/constants';
import {
addSignificantTermsAction,
addSignificantTermsGroupAction,
Expand Down Expand Up @@ -52,6 +53,7 @@ import { fetchFrequentItemSets } from './queries/fetch_frequent_item_sets';
import { getHistogramQuery } from './queries/get_histogram_query';
import { getGroupFilter } from './queries/get_group_filter';
import { getSignificantTermGroups } from './queries/get_significant_term_groups';
import { trackAIOpsRouteUsage } from '../lib/track_route_usage';

// 10s ping frequency to keep the stream alive.
const PING_FREQUENCY = 10000;
Expand All @@ -67,7 +69,8 @@ export const defineLogRateAnalysisRoute = (
router: IRouter<DataRequestHandlerContext>,
license: AiopsLicense,
logger: Logger,
coreStart: CoreStart
coreStart: CoreStart,
usageCounter?: UsageCounter
) => {
router.versioned
.post({
Expand All @@ -85,6 +88,9 @@ export const defineLogRateAnalysisRoute = (
},
},
async (context, request, response) => {
const { headers } = request;
trackAIOpsRouteUsage(LOG_RATE_ANALYSIS, headers.source, usageCounter);

if (!license.isActivePlatinumLicense) {
return response.forbidden();
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/aiops/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import type { PluginSetup, PluginStart } from '@kbn/data-plugin/server';
import type { LicensingPluginStart } from '@kbn/licensing-plugin/server';
import type { CasesSetup } from '@kbn/cases-plugin/server';
import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';

export interface AiopsPluginSetupDeps {
data: PluginSetup;
licensing: LicensingPluginStart;
cases?: CasesSetup;
usageCollection?: UsageCollectionSetup;
}

export interface AiopsPluginStartDeps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export const LogRateAnalysis: FC<AlertDetailsLogRateAnalysisSectionProps> = ({ r
</EuiFlexItem>
<EuiFlexItem>
<LogRateAnalysisContent
source="observability_alerts"
dataView={dataView}
timeRange={timeRange}
esSearchQuery={esSearchQuery}
Expand Down

0 comments on commit a71ecb5

Please sign in to comment.