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

[APM] Scope APM alert creation to environment #65681

Merged
merged 8 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ interface Props {

export function AlertingFlyout(props: Props) {
const { addFlyoutVisible, setAddFlyoutVisibility, alertType } = props;

return alertType ? (
<AlertAdd
addFlyoutVisible={addFlyoutVisible}
setAddFlyoutVisibility={setAddFlyoutVisibility}
consumer="apm"
alertTypeId={alertType}
canChangeTrigger={false}
/>
) : null;
return (
alertType && (
<AlertAdd
addFlyoutVisible={addFlyoutVisible}
setAddFlyoutVisibility={setAddFlyoutVisibility}
consumer="apm"
alertTypeId={alertType}
canChangeTrigger={false}
/>
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { EuiSelect } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import React from 'react';
import { useFetcher } from '../../../hooks/useFetcher';
import { useLocation } from '../../../hooks/useLocation';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { history } from '../../../utils/history';
Expand All @@ -16,6 +15,7 @@ import {
ENVIRONMENT_ALL,
ENVIRONMENT_NOT_DEFINED
} from '../../../../common/environment_filter_values';
import { useEnvironments, ALL_OPTION } from '../../../hooks/useEnvironments';

function updateEnvironmentUrl(
location: ReturnType<typeof useLocation>,
Expand All @@ -32,13 +32,6 @@ function updateEnvironmentUrl(
});
}

const ALL_OPTION = {
value: ENVIRONMENT_ALL,
text: i18n.translate('xpack.apm.filter.environment.allLabel', {
defaultMessage: 'All'
})
};

const NOT_DEFINED_OPTION = {
value: ENVIRONMENT_NOT_DEFINED,
text: i18n.translate('xpack.apm.filter.environment.notDefinedLabel', {
Expand Down Expand Up @@ -74,27 +67,10 @@ function getOptions(environments: string[]) {

export const EnvironmentFilter: React.FC = () => {
const location = useLocation();
const { urlParams, uiFilters } = useUrlParams();
const { start, end, serviceName } = urlParams;
const { uiFilters } = useUrlParams();

const { environment } = uiFilters;
const { data: environments = [], status = 'loading' } = useFetcher(
callApmApi => {
if (start && end) {
return callApmApi({
pathname: '/api/apm/ui_filters/environments',
params: {
query: {
start,
end,
serviceName
}
}
});
}
},
[start, end, serviceName]
);
const { environments, status = 'loading' } = useEnvironments();

return (
<EuiSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import React from 'react';
import { EuiFieldNumber } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { isFinite } from 'lodash';
import { EuiSelect } from '@elastic/eui';
import { ForLastExpression } from '../../../../../triggers_actions_ui/public';
import { ALERT_TYPES_CONFIG } from '../../../../common/alert_types';
import { ServiceAlertTrigger } from '../ServiceAlertTrigger';
import { PopoverExpression } from '../ServiceAlertTrigger/PopoverExpression';
import { useEnvironments, ALL_OPTION } from '../../../hooks/useEnvironments';

export interface ErrorRateAlertTriggerParams {
windowSize: number;
windowUnit: string;
threshold: number;
environment: string;
}

interface Props {
Expand All @@ -27,10 +30,13 @@ interface Props {
export function ErrorRateAlertTrigger(props: Props) {
const { setAlertParams, setAlertProperty, alertParams } = props;

const { environmentOptions } = useEnvironments();

const defaults = {
threshold: 25,
windowSize: 1,
windowUnit: 'm'
windowUnit: 'm',
environment: ALL_OPTION.value
};

const params = {
Expand All @@ -41,6 +47,28 @@ export function ErrorRateAlertTrigger(props: Props) {
const threshold = isFinite(params.threshold) ? params.threshold : '';

const fields = [
<PopoverExpression
value={
params.environment === ALL_OPTION.value
? ALL_OPTION.text
: params.environment
}
title={i18n.translate('xpack.apm.errorRateAlertTrigger.environment', {
defaultMessage: 'Environment'
})}
>
<EuiSelect
value={params.environment}
options={environmentOptions}
onChange={e =>
setAlertParams(
'environment',
e.target.value as ErrorRateAlertTriggerParams['environment']
)
}
compressed
/>
</PopoverExpression>,
<PopoverExpression
title={i18n.translate('xpack.apm.errorRateAlertTrigger.isAbove', {
defaultMessage: 'is above'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React from 'react';
import { map } from 'lodash';
import { EuiFieldNumber, EuiSelect } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { map } from 'lodash';
import React from 'react';
import { ForLastExpression } from '../../../../../triggers_actions_ui/public';
import {
TRANSACTION_ALERT_AGGREGATION_TYPES,
ALERT_TYPES_CONFIG
ALERT_TYPES_CONFIG,
TRANSACTION_ALERT_AGGREGATION_TYPES
} from '../../../../common/alert_types';
import { ServiceAlertTrigger } from '../ServiceAlertTrigger';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { ALL_OPTION, useEnvironments } from '../../../hooks/useEnvironments';
import { useServiceTransactionTypes } from '../../../hooks/useServiceTransactionTypes';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { ServiceAlertTrigger } from '../ServiceAlertTrigger';
import { PopoverExpression } from '../ServiceAlertTrigger/PopoverExpression';

interface Params {
Expand All @@ -24,6 +25,7 @@ interface Params {
aggregationType: 'avg' | '95th' | '99th';
serviceName: string;
transactionType: string;
environment: string;
}

interface Props {
Expand All @@ -38,6 +40,7 @@ export function TransactionDurationAlertTrigger(props: Props) {
const { urlParams } = useUrlParams();

const transactionTypes = useServiceTransactionTypes(urlParams);
const { environmentOptions } = useEnvironments();

if (!transactionTypes.length) {
return null;
Expand All @@ -48,7 +51,8 @@ export function TransactionDurationAlertTrigger(props: Props) {
aggregationType: 'avg',
windowSize: 5,
windowUnit: 'm',
transactionType: transactionTypes[0]
transactionType: transactionTypes[0],
environment: ALL_OPTION.value
};

const params = {
Expand All @@ -57,6 +61,28 @@ export function TransactionDurationAlertTrigger(props: Props) {
};

const fields = [
<PopoverExpression
value={
params.environment === ALL_OPTION.value
? ALL_OPTION.text
: params.environment
}
title={i18n.translate(
'xpack.apm.transactionDurationAlertTrigger.environment',
{
defaultMessage: 'Environment'
}
)}
>
<EuiSelect
value={params.environment}
options={environmentOptions}
onChange={e =>
setAlertParams('environment', e.target.value as Params['environment'])
}
compressed
/>
</PopoverExpression>,
<PopoverExpression
value={params.transactionType}
title={i18n.translate('xpack.apm.transactionDurationAlertTrigger.type', {
Expand Down
61 changes: 61 additions & 0 deletions x-pack/plugins/apm/public/hooks/useEnvironments.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 { useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { useFetcher } from './useFetcher';
import {
ENVIRONMENT_NOT_DEFINED,
ENVIRONMENT_ALL
} from '../../common/environment_filter_values';
import { useUrlParams } from './useUrlParams';

export const ALL_OPTION = {
value: ENVIRONMENT_ALL,
text: i18n.translate('xpack.apm.environment.allLabel', {
defaultMessage: 'All'
})
};

function getEnvironmentOptions(environments: string[]) {
const environmentOptions = environments
.filter(env => env !== ENVIRONMENT_NOT_DEFINED)
.map(environment => ({
value: environment,
text: environment
}));

return [ALL_OPTION, ...environmentOptions];
}

export function useEnvironments() {
const { urlParams } = useUrlParams();
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
const { serviceName, start, end } = urlParams;
const { data: environments = [], status = 'loading' } = useFetcher(
callApmApi => {
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
if (start && end) {
return callApmApi({
pathname: '/api/apm/ui_filters/environments',
params: {
query: {
start,
end,
serviceName
}
}
});
}
},
[start, end, serviceName]
);

const environmentOptions = useMemo(
() => getEnvironmentOptions(environments),
[environments]
);

return { environments, status, environmentOptions };
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import { schema, TypeOf } from '@kbn/config-schema';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import { ENVIRONMENT_ALL } from '../../../common/environment_filter_values';
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types';
import {
ESSearchResponse,
ESSearchRequest
} from '../../../typings/elasticsearch';
import {
PROCESSOR_EVENT,
SERVICE_NAME
SERVICE_NAME,
SERVICE_ENVIRONMENT
} from '../../../common/elasticsearch_fieldnames';
import { AlertingPlugin } from '../../../../alerting/server';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
Expand All @@ -30,7 +32,8 @@ const paramsSchema = schema.object({
serviceName: schema.string(),
windowSize: schema.number(),
windowUnit: schema.string(),
threshold: schema.number()
threshold: schema.number(),
environment: schema.string()
});

const alertTypeConfig = ALERT_TYPES_CONFIG[AlertType.ErrorRate];
Expand Down Expand Up @@ -70,6 +73,11 @@ export function registerErrorRateAlertType({
savedObjectsClient: services.savedObjectsClient
});

const environmentTerm =
alertParams.environment === ENVIRONMENT_ALL
? []
: [{ term: { [SERVICE_ENVIRONMENT]: alertParams.environment } }];

const searchParams = {
index: indices['apm_oss.errorIndices'],
size: 0,
Expand All @@ -93,7 +101,8 @@ export function registerErrorRateAlertType({
term: {
[SERVICE_NAME]: alertParams.serviceName
}
}
},
...environmentTerm
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { schema, TypeOf } from '@kbn/config-schema';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import { ENVIRONMENT_ALL } from '../../../common/environment_filter_values';
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types';
import { ESSearchResponse } from '../../../typings/elasticsearch';
import {
PROCESSOR_EVENT,
SERVICE_NAME,
TRANSACTION_TYPE,
TRANSACTION_DURATION
TRANSACTION_DURATION,
SERVICE_ENVIRONMENT
} from '../../../common/elasticsearch_fieldnames';
import { AlertingPlugin } from '../../../../alerting/server';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
Expand All @@ -35,7 +37,8 @@ const paramsSchema = schema.object({
schema.literal('avg'),
schema.literal('95th'),
schema.literal('99th')
])
]),
environment: schema.string()
});

const alertTypeConfig = ALERT_TYPES_CONFIG[AlertType.TransactionDuration];
Expand Down Expand Up @@ -84,6 +87,11 @@ export function registerTransactionDurationAlertType({
savedObjectsClient: services.savedObjectsClient
});

const environmentTerm =
alertParams.environment === ENVIRONMENT_ALL
? []
: [{ term: { [SERVICE_ENVIRONMENT]: alertParams.environment } }];

const searchParams = {
index: indices['apm_oss.transactionIndices'],
size: 0,
Expand Down Expand Up @@ -112,7 +120,8 @@ export function registerTransactionDurationAlertType({
term: {
[TRANSACTION_TYPE]: alertParams.transactionType
}
}
},
...environmentTerm
]
}
},
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -4219,7 +4219,6 @@
"xpack.apm.fetcher.error.status": "エラー",
"xpack.apm.fetcher.error.title": "リソースの取得中にエラーが発生しました",
"xpack.apm.fetcher.error.url": "URL",
"xpack.apm.filter.environment.allLabel": "すべて",
"xpack.apm.filter.environment.label": "環境",
"xpack.apm.filter.environment.notDefinedLabel": "未定義",
"xpack.apm.filter.environment.selectEnvironmentLabel": "環境を選択",
Expand Down
Loading