Skip to content

Commit

Permalink
Clean up action variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Sep 17, 2020
1 parent 6f114ff commit 8dc074d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 73 deletions.
38 changes: 38 additions & 0 deletions x-pack/plugins/apm/server/lib/alerts/action_variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { i18n } from '@kbn/i18n';

export const apmActionVariables = {
serviceName: {
description: i18n.translate(
'xpack.apm.alerts.action_variables.serviceName',
{ defaultMessage: 'The service the alert is created for' }
),
name: 'serviceName',
},
transactionType: {
description: i18n.translate(
'xpack.apm.alerts.action_variables.transactionType',
{ defaultMessage: 'The transaction type the alert is created for' }
),
name: 'transactionType',
},
environment: {
description: i18n.translate(
'xpack.apm.alerts.action_variables.environment',
{ defaultMessage: 'The transaction type the alert is created for' }
),
name: 'environment',
},
triggerValue: {
description: i18n.translate(
'xpack.apm.alerts.action_variables.triggerValue',
{ defaultMessage: 'The value that triggered the alert' }
),
name: 'triggerValue',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { AlertingPlugin } from '../../../../alerts/server';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { APMConfig } from '../..';
import { apmActionVariables } from './action_variables';

interface RegisterAlertParams {
alerts: AlertingPlugin['setup'];
Expand Down Expand Up @@ -51,15 +52,9 @@ export function registerErrorCountAlertType({
},
actionVariables: {
context: [
{
description: i18n.translate(
'xpack.apm.registerErrorCountAlertType.variables.serviceName',
{
defaultMessage: 'Service name',
}
),
name: 'serviceName',
},
apmActionVariables.serviceName,
apmActionVariables.environment,
apmActionVariables.triggerValue,
],
},
producer: 'apm',
Expand Down Expand Up @@ -100,14 +95,16 @@ export function registerErrorCountAlertType({
ESSearchRequest
> = await services.callCluster('search', searchParams);

const value = response.hits.total.value;
const errorCount = response.hits.total.value;

if (value && value > alertParams.threshold) {
if (errorCount > alertParams.threshold) {
const alertInstance = services.alertInstanceFactory(
AlertType.ErrorCount
);
alertInstance.scheduleActions(alertTypeConfig.defaultActionGroupId, {
serviceName: alertParams.serviceName,
environment: alertParams.environment,
triggerValue: errorCount,
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { AlertingPlugin } from '../../../../alerts/server';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { APMConfig } from '../..';
import { getEnvironmentUiFilterES } from '../helpers/convert_ui_filters/get_environment_ui_filter_es';
import { apmActionVariables } from './action_variables';

interface RegisterAlertParams {
alerts: AlertingPlugin['setup'];
Expand Down Expand Up @@ -57,24 +58,10 @@ export function registerTransactionDurationAlertType({
},
actionVariables: {
context: [
{
description: i18n.translate(
'xpack.apm.registerTransactionDurationAlertType.variables.serviceName',
{
defaultMessage: 'Service name',
}
),
name: 'serviceName',
},
{
description: i18n.translate(
'xpack.apm.registerTransactionDurationAlertType.variables.transactionType',
{
defaultMessage: 'Transaction type',
}
),
name: 'transactionType',
},
apmActionVariables.serviceName,
apmActionVariables.transactionType,
apmActionVariables.environment,
apmActionVariables.triggerValue,
],
},
producer: 'apm',
Expand Down Expand Up @@ -134,15 +121,21 @@ export function registerTransactionDurationAlertType({

const { agg } = response.aggregations;

const value = 'values' in agg ? Object.values(agg.values)[0] : agg?.value;
const transactionDuration =
'values' in agg ? Object.values(agg.values)[0] : agg?.value;

if (value && value > alertParams.threshold * 1000) {
if (
transactionDuration &&
transactionDuration > alertParams.threshold * 1000
) {
const alertInstance = services.alertInstanceFactory(
AlertType.TransactionDuration
);
alertInstance.scheduleActions(alertTypeConfig.defaultActionGroupId, {
transactionType: alertParams.transactionType,
serviceName: alertParams.serviceName,
environment: alertParams.environment,
triggerValue: transactionDuration,
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import { schema } from '@kbn/config-schema';
import { Observable } from 'rxjs';
import { i18n } from '@kbn/i18n';
import { KibanaRequest } from '../../../../../../src/core/server';
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types';
import { AlertingPlugin } from '../../../../alerts/server';
import { APMConfig } from '../..';
import { MlPluginSetup } from '../../../../ml/server';
import { getMLJobIds } from '../service_map/get_service_anomalies';
import { apmActionVariables } from './action_variables';

interface RegisterAlertParams {
alerts: AlertingPlugin['setup'];
Expand Down Expand Up @@ -47,24 +47,10 @@ export function registerTransactionDurationAnomalyAlertType({
},
actionVariables: {
context: [
{
description: i18n.translate(
'xpack.apm.registerTransactionDurationAnomalyAlertType.variables.serviceName',
{
defaultMessage: 'Service name',
}
),
name: 'serviceName',
},
{
description: i18n.translate(
'xpack.apm.registerTransactionDurationAnomalyAlertType.variables.transactionType',
{
defaultMessage: 'Transaction type',
}
),
name: 'transactionType',
},
apmActionVariables.serviceName,
apmActionVariables.transactionType,
apmActionVariables.environment,
apmActionVariables.triggerValue,
],
},
producer: 'apm',
Expand Down Expand Up @@ -131,6 +117,9 @@ export function registerTransactionDurationAnomalyAlertType({
);
alertInstance.scheduleActions(alertTypeConfig.defaultActionGroupId, {
serviceName: alertParams.serviceName,
transactionType: alertParams.transactionType,
environment: alertParams.environment,
triggerValue: hitCount,
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { schema } from '@kbn/config-schema';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import { EventOutcome } from '../../../common/event_outcome';
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types';
import { ESSearchResponse } from '../../../typings/elasticsearch';
Expand All @@ -21,6 +20,7 @@ import { AlertingPlugin } from '../../../../alerts/server';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { APMConfig } from '../..';
import { getEnvironmentUiFilterES } from '../helpers/convert_ui_filters/get_environment_ui_filter_es';
import { apmActionVariables } from './action_variables';

interface RegisterAlertParams {
alerts: AlertingPlugin['setup'];
Expand Down Expand Up @@ -52,27 +52,10 @@ export function registerTransactionErrorRateAlertType({
},
actionVariables: {
context: [
{
description: i18n.translate(
'xpack.apm.registerTransactionErrorRateAlertType.variables.serviceName',
{ defaultMessage: 'The service the alert is created for' }
),
name: 'serviceName',
},
{
description: i18n.translate(
'xpack.apm.registerTransactionErrorRateAlertType.variables.transactionType',
{ defaultMessage: 'The transaction type the alert is created for' }
),
name: 'transactionType',
},
{
description: i18n.translate(
'xpack.apm.registerTransactionErrorRateAlertType.variables.transactionErrorRate',
{ defaultMessage: 'The error rate that triggered the alert' }
),
name: 'transactionErrorRate',
},
apmActionVariables.transactionType,
apmActionVariables.serviceName,
apmActionVariables.environment,
apmActionVariables.triggerValue,
],
},
producer: 'apm',
Expand Down Expand Up @@ -136,7 +119,8 @@ export function registerTransactionErrorRateAlertType({
alertInstance.scheduleActions(alertTypeConfig.defaultActionGroupId, {
serviceName: alertParams.serviceName,
transactionType: alertParams.transactionType,
transactionErrorRate,
environment: alertParams.environment,
triggerValue: transactionErrorRate,
});
}
},
Expand Down

0 comments on commit 8dc074d

Please sign in to comment.