Skip to content

Commit

Permalink
[7.x] [APM] Add ignore_unavailable to avoid querying closed indices (
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Dec 8, 2020
1 parent 3b72a4f commit 8b76370
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
21 changes: 21 additions & 0 deletions x-pack/plugins/apm/server/lib/alerts/alerting_es_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import {
ESSearchRequest,
ESSearchResponse,
} from '../../../../../typings/elasticsearch';
import { AlertServices } from '../../../../alerts/server';

export function alertingEsClient<TParams extends ESSearchRequest>(
services: AlertServices,
params: TParams
): Promise<ESSearchResponse<unknown, TParams>> {
return services.callCluster('search', {
...params,
ignore_unavailable: true,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { isEmpty } from 'lodash';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { APMConfig } from '../..';
import { ESSearchResponse } from '../../../../../typings/elasticsearch';
import { AlertingPlugin } from '../../../../alerts/server';
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types';
import {
Expand All @@ -21,6 +20,7 @@ import { ProcessorEvent } from '../../../common/processor_event';
import { getEnvironmentUiFilterES } from '../helpers/convert_ui_filters/get_environment_ui_filter_es';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';

interface RegisterAlertParams {
alerts: AlertingPlugin['setup'];
Expand Down Expand Up @@ -110,11 +110,7 @@ export function registerErrorCountAlertType({
},
};

const response: ESSearchResponse<
unknown,
typeof searchParams
> = await services.callCluster('search', searchParams);

const response = await alertingEsClient(services, searchParams);
const errorCount = response.hits.total.value;

if (errorCount > alertParams.threshold) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { schema } from '@kbn/config-schema';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { APMConfig } from '../..';
import { ESSearchResponse } from '../../../../../typings/elasticsearch';
import { AlertingPlugin } from '../../../../alerts/server';
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types';
import {
Expand All @@ -23,6 +22,7 @@ import { getDurationFormatter } from '../../../common/utils/formatters';
import { getEnvironmentUiFilterES } from '../helpers/convert_ui_filters/get_environment_ui_filter_es';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';

interface RegisterAlertParams {
alerts: AlertingPlugin['setup'];
Expand Down Expand Up @@ -120,10 +120,7 @@ export function registerTransactionDurationAlertType({
},
};

const response: ESSearchResponse<
unknown,
typeof searchParams
> = await services.callCluster('search', searchParams);
const response = await alertingEsClient(services, searchParams);

if (!response.aggregations) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Transaction error rate alert', () => {
},
},
aggregations: {
erroneous_transactions: {
failed_transactions: {
doc_count: 2,
},
services: {
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('Transaction error rate alert', () => {
},
},
aggregations: {
erroneous_transactions: {
failed_transactions: {
doc_count: 2,
},
services: {
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('Transaction error rate alert', () => {
},
},
aggregations: {
erroneous_transactions: {
failed_transactions: {
doc_count: 2,
},
services: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { isEmpty } from 'lodash';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { APMConfig } from '../..';
import { ESSearchResponse } from '../../../../../typings/elasticsearch';
import { AlertingPlugin } from '../../../../alerts/server';
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types';
import {
Expand All @@ -25,6 +24,7 @@ import { asDecimalOrInteger } from '../../../common/utils/formatters';
import { getEnvironmentUiFilterES } from '../helpers/convert_ui_filters/get_environment_ui_filter_es';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';

interface RegisterAlertParams {
alerts: AlertingPlugin['setup'];
Expand Down Expand Up @@ -106,7 +106,7 @@ export function registerTransactionErrorRateAlertType({
},
},
aggs: {
erroneous_transactions: {
failed_transactions: {
filter: { term: { [EVENT_OUTCOME]: EventOutcome.failure } },
},
services: {
Expand All @@ -132,20 +132,16 @@ export function registerTransactionErrorRateAlertType({
},
};

const response: ESSearchResponse<
unknown,
typeof searchParams
> = await services.callCluster('search', searchParams);

const response = await alertingEsClient(services, searchParams);
if (!response.aggregations) {
return;
}

const errornousTransactionsCount =
response.aggregations.erroneous_transactions.doc_count;
const failedTransactionCount =
response.aggregations.failed_transactions.doc_count;
const totalTransactionCount = response.hits.total.value;
const transactionErrorRate =
(errornousTransactionsCount / totalTransactionCount) * 100;
(failedTransactionCount / totalTransactionCount) * 100;

if (transactionErrorRate > alertParams.threshold) {
function scheduleAction({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function createApmEventClient({
params: {
...withPossibleLegacyDataFilter,
ignore_throttled: !includeFrozen,
ignore_unavailable: true,
},
request,
debug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe('setupRequest', () => {
},
},
},
ignore_unavailable: true,
ignore_throttled: true,
});
});
Expand Down

0 comments on commit 8b76370

Please sign in to comment.