Skip to content

Commit

Permalink
[Discover] merge search clients metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaanj committed Apr 26, 2022
1 parent 4f1a917 commit 7d03945
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 23 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/rule_task_instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const ruleStateSchema = t.partial({

const ruleExecutionMetricsSchema = t.partial({
numSearches: t.number,
numSearchSourceSearches: t.number,
totalSearchDurationMs: t.number,
esSearchDurationMs: t.number,
});
Expand Down
24 changes: 24 additions & 0 deletions x-pack/plugins/alerting/server/lib/merge_search_metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { RuleExecutionMetrics } from '../types';
import type { ScopedClusterClientMetrics } from './wrap_scoped_cluster_client';
import type { SearchSourceClientMetrics } from './wrap_search_source_fetch';

export const mergeSearchMetrics = (
scopedClusterClientMetrics: ScopedClusterClientMetrics,
searchSourceMetrics: SearchSourceClientMetrics
): RuleExecutionMetrics => {
return {
numSearches: scopedClusterClientMetrics.numSearches,
numSearchSourceSearches: searchSourceMetrics.numSearchSourceSearches,
totalSearchDurationMs:
scopedClusterClientMetrics.totalSearchDurationMs + searchSourceMetrics.totalSearchDurationMs,
esSearchDurationMs:
scopedClusterClientMetrics.esSearchDurationMs + searchSourceMetrics.esSearchDurationMs,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type {
AggregationsAggregate,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { IScopedClusterClient, ElasticsearchClient, Logger } from '@kbn/core/server';
import { RuleExecutionMetrics } from '../types';
import { LogSearchMetricsOpts, RuleInfo } from './types';

export type LogSearchMetricsFn = (metrics: LogSearchMetricsOpts) => void;
Expand All @@ -41,6 +40,12 @@ type WrapScopedClusterClientOpts = WrapScopedClusterClientFactoryOpts & {
logMetricsFn: LogSearchMetricsFn;
};

export interface ScopedClusterClientMetrics {
esSearchDurationMs: number;
totalSearchDurationMs: number;
numSearches: number;
}

export function createWrappedScopedClusterClientFactory(opts: WrapScopedClusterClientFactoryOpts) {
let numSearches: number = 0;
let esSearchDurationMs: number = 0;
Expand All @@ -56,7 +61,7 @@ export function createWrappedScopedClusterClientFactory(opts: WrapScopedClusterC

return {
client: () => wrappedClient,
getMetrics: (): RuleExecutionMetrics => {
getMetrics: (): ScopedClusterClientMetrics => {
return {
esSearchDurationMs,
totalSearchDurationMs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('wrapSearchSourceFetch', () => {
expect(searchSourceInstanceMock.fetch).toHaveBeenCalledTimes(3);

const stats = getMetrics();
expect(stats.numSearches).toEqual(3);
expect(stats.numSearchSourceSearches).toEqual(3);
expect(stats.esSearchDurationMs).toEqual(999);

expect(logger.debug).toHaveBeenCalledWith(
Expand Down
15 changes: 10 additions & 5 deletions x-pack/plugins/alerting/server/lib/wrap_search_source_fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

import { Logger } from '@kbn/core/server';
import { ISearchSource } from '@kbn/data-plugin/common';
import { RuleExecutionMetrics } from '../types';
import { LogSearchMetricsOpts, RuleInfo } from './types';

export interface SearchSourceClientMetrics {
esSearchDurationMs: number;
totalSearchDurationMs: number;
numSearchSourceSearches: number;
}

export function wrapSearchSourceFetch({
logger,
rule,
Expand All @@ -19,12 +24,12 @@ export function wrapSearchSourceFetch({
rule: RuleInfo;
abortController: AbortController;
}) {
let numSearches: number = 0;
let numSearchSourceSearches: number = 0;
let esSearchDurationMs: number = 0;
let totalSearchDurationMs: number = 0;

function logMetrics(metrics: LogSearchMetricsOpts) {
numSearches++;
numSearchSourceSearches++;
esSearchDurationMs += metrics.esSearchDuration;
totalSearchDurationMs += metrics.totalSearchDuration;
}
Expand All @@ -48,10 +53,10 @@ export function wrapSearchSourceFetch({
throw e;
}
},
getMetrics: (): RuleExecutionMetrics => ({
getMetrics: (): SearchSourceClientMetrics => ({
esSearchDurationMs,
totalSearchDurationMs,
numSearches,
numSearchSourceSearches,
}),
};
}
14 changes: 7 additions & 7 deletions x-pack/plugins/alerting/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('Task Runner', () => {
expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z');
expect(logger.debug).nthCalledWith(
2,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}'
);

const eventLogger = taskRunnerFactoryInitializerParams.eventLogger;
Expand Down Expand Up @@ -320,7 +320,7 @@ describe('Task Runner', () => {
);
expect(logger.debug).nthCalledWith(
3,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":1,"numberOfGeneratedActions":1,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":1,"numberOfGeneratedActions":1,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
);

const eventLogger = customTaskRunnerFactoryInitializerParams.eventLogger;
Expand Down Expand Up @@ -428,7 +428,7 @@ describe('Task Runner', () => {
);
expect(logger.debug).nthCalledWith(
4,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
);

const eventLogger = taskRunnerFactoryInitializerParams.eventLogger;
Expand Down Expand Up @@ -593,7 +593,7 @@ describe('Task Runner', () => {
);
expect(logger.debug).nthCalledWith(
4,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":1,"numberOfGeneratedActions":1,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":1,"numberOfGeneratedActions":1,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
);
expect(mockUsageCounter.incrementCounter).not.toHaveBeenCalled();
}
Expand Down Expand Up @@ -1121,7 +1121,7 @@ describe('Task Runner', () => {
);
expect(logger.debug).nthCalledWith(
4,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":2,"numberOfGeneratedActions":2,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":2,"numberOfGeneratedActions":2,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
);

const eventLogger = customTaskRunnerFactoryInitializerParams.eventLogger;
Expand Down Expand Up @@ -1262,7 +1262,7 @@ describe('Task Runner', () => {
);
expect(logger.debug).nthCalledWith(
4,
`ruleExecutionStatus for test:${alertId}: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":2,"numberOfGeneratedActions":2,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}`
'ruleExecutionStatus for test:e558aaad-fd81-46d2-96fc-3bd8fc3dc03f: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":2,"numberOfGeneratedActions":2,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
);

const eventLogger = customTaskRunnerFactoryInitializerParams.eventLogger;
Expand Down Expand Up @@ -2489,7 +2489,7 @@ describe('Task Runner', () => {
expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z');
expect(logger.debug).nthCalledWith(
2,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}'
);

const eventLogger = taskRunnerFactoryInitializerParams.eventLogger;
Expand Down
11 changes: 5 additions & 6 deletions x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
import { createWrappedScopedClusterClientFactory } from '../lib/wrap_scoped_cluster_client';
import { wrapSearchSourceFetch } from '../lib/wrap_search_source_fetch';
import { AlertExecutionStore } from '../lib/alert_execution_store';
import { mergeSearchMetrics } from '../lib/merge_search_metrics';

const FALLBACK_RETRY_INTERVAL = '5m';
const CONNECTIVITY_RETRY_INTERVAL = '5m';
Expand Down Expand Up @@ -461,12 +462,10 @@ export class TaskRunner<
name: rule.name,
};

const scopedClusterClientMetrics = wrappedScopedClusterClient.getMetrics();
const searchSourceMetrics = wrappedSearchSourceFetch.getMetrics();

const searchMetrics = !!scopedClusterClientMetrics.numSearches
? scopedClusterClientMetrics
: searchSourceMetrics;
const searchMetrics = mergeSearchMetrics(
wrappedScopedClusterClient.getMetrics(),
wrappedSearchSourceFetch.getMetrics()
);

// Cleanup alerts that are no longer scheduling actions to avoid over populating the alertInstances object
const alertsWithScheduledActions = pickBy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ describe('Task Runner Cancel', () => {
);
expect(logger.debug).nthCalledWith(
7,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":0,"numberOfGeneratedActions":0,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
);

const eventLogger = taskRunnerFactoryInitializerParams.eventLogger;
Expand Down Expand Up @@ -683,7 +683,7 @@ describe('Task Runner Cancel', () => {
);
expect(logger.debug).nthCalledWith(
6,
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"esSearchDurationMs":33,"totalSearchDurationMs":23423},"numberOfTriggeredActions":1,"numberOfGeneratedActions":1,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
'ruleExecutionStatus for test:1: {"metrics":{"numSearches":3,"numSearchSourceSearches":0,"totalSearchDurationMs":23423,"esSearchDurationMs":33},"numberOfTriggeredActions":1,"numberOfGeneratedActions":1,"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}'
);

const eventLogger = taskRunnerFactoryInitializerParams.eventLogger;
Expand Down

0 comments on commit 7d03945

Please sign in to comment.