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] Add transaction name filter in failed transaction rate rule type #155405

Merged
1 change: 1 addition & 0 deletions x-pack/plugins/apm/common/rules/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const transactionErrorRateParamsSchema = schema.object({
windowUnit: schema.string(),
threshold: schema.number(),
transactionType: schema.maybe(schema.string()),
transactionName: schema.maybe(schema.string()),
serviceName: schema.maybe(schema.string()),
environment: schema.string(),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 { Story } from '@storybook/react';
import React, { ComponentType, useState } from 'react';
import { CoreStart } from '@kbn/core/public';
import { createKibanaReactContext } from '@kbn/kibana-react-plugin/public';
import { RuleParams, TransactionErrorRateRuleType } from '.';
import { AggregationType } from '../../../../../common/rules/apm_rule_types';
import { AlertMetadata } from '../../utils/helper';
import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values';

const KibanaReactContext = createKibanaReactContext({
notifications: { toasts: { add: () => {} } },
} as unknown as Partial<CoreStart>);

interface Args {
ruleParams: RuleParams;
metadata?: AlertMetadata;
}

export default {
title: 'alerting/TransactionErrorRateRuleType',
component: TransactionErrorRateRuleType,
decorators: [
(StoryComponent: ComponentType) => {
return (
<KibanaReactContext.Provider>
<div style={{ width: 400 }}>
<StoryComponent />
</div>
</KibanaReactContext.Provider>
);
},
],
};

export const CreatingInApmServiceOverview: Story<Args> = ({
ruleParams,
metadata,
}) => {
const [params, setParams] = useState<RuleParams>(ruleParams);

function setRuleParams(property: string, value: any) {
setParams({ ...params, [property]: value });
}

return (
<TransactionErrorRateRuleType
ruleParams={params}
metadata={metadata}
setRuleParams={setRuleParams}
setRuleProperty={() => {}}
/>
);
};

CreatingInApmServiceOverview.args = {
ruleParams: {
aggregationType: AggregationType.Avg,
environment: 'testEnvironment',
serviceName: 'testServiceName',
threshold: 1500,
transactionType: 'testTransactionType',
transactionName: 'GET /api/customer/:id',
windowSize: 5,
windowUnit: 'm',
},
metadata: {
environment: ENVIRONMENT_ALL.value,
serviceName: undefined,
},
};

export const CreatingInStackManagement: Story<Args> = ({
ruleParams,
metadata,
}) => {
const [params, setParams] = useState<RuleParams>(ruleParams);

function setRuleParams(property: string, value: any) {
setParams({ ...params, [property]: value });
}

return (
<TransactionErrorRateRuleType
ruleParams={params}
metadata={metadata}
setRuleParams={setRuleParams}
setRuleProperty={() => {}}
/>
);
};

CreatingInStackManagement.args = {
ruleParams: {
aggregationType: AggregationType.Avg,
environment: 'testEnvironment',
threshold: 1500,
windowSize: 5,
windowUnit: 'm',
},
metadata: undefined,
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
IsAboveField,
ServiceField,
TransactionTypeField,
TransactionNameField,
} from '../../utils/fields';
import { AlertMetadata, getIntervalAndTimeRange } from '../../utils/helper';
import { ApmRuleParamsContainer } from '../../ui_components/apm_rule_params_container';
Expand All @@ -33,6 +34,7 @@ interface RuleParams {
threshold?: number;
serviceName?: string;
transactionType?: string;
transactionName?: string;
environment?: string;
}

Expand Down Expand Up @@ -78,6 +80,7 @@ export function TransactionErrorRateRuleType(props: Props) {
environment: params.environment,
serviceName: params.serviceName,
transactionType: params.transactionType,
transactionName: params.transactionName,
interval,
start,
end,
Expand All @@ -89,6 +92,7 @@ export function TransactionErrorRateRuleType(props: Props) {
},
[
params.transactionType,
params.transactionName,
params.environment,
params.serviceName,
params.windowSize,
Expand All @@ -102,7 +106,8 @@ export function TransactionErrorRateRuleType(props: Props) {
onChange={(value) => {
if (value !== params.serviceName) {
setRuleParams('serviceName', value);
setRuleParams('transactionType', '');
setRuleParams('transactionType', undefined);
Copy link
Member

@sorenlouv sorenlouv Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this mistakenly set to an empty string? What's the impact of this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm also wondering what this means for eg updating existing rules?

Copy link
Contributor Author

@kpatticha kpatticha Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was added here: ad75d90 to reset the values when changing the service name but imo sending an empty string can lead to misleading results.

As for updating an existing rule, it will remove transactionType param from the list and it won't filter for the transaction.type

However, the biggest impact seems to be in the rule executor. Existing rules may have filter fields with empty strings, and filtering emptry string would yield no results. To address this issue, we need to pass queryEmptyString as false when using termQuery for these fields.

For example,

...termQuery(TRANSACTION_TYPE, ruleParams.transactionType, {
    queryEmptyString: false,
 }),

Fortunately, this issue has been fixed in v8.6, but there was a regression in my previous PR that caused the issue to resurface.

The tests we have in place were not sufficient to detect the regression. 😢

Next
I need to revert the change that removed the queryEmptyString parameter and ensure that empty strings are not filtered out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but imo sending an empty string can lead to misleading results.

Agree, in most cases undefined is better than "" we just need to make sure that old rules with an empty string still works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests we have in place were not sufficient to detect the regression. 😢

Can you fix that? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert the change

I'll start working on the tests

setRuleParams('transactionName', undefined);
setRuleParams('environment', ENVIRONMENT_ALL.value);
}
}}
Expand All @@ -117,6 +122,11 @@ export function TransactionErrorRateRuleType(props: Props) {
onChange={(value) => setRuleParams('environment', value)}
serviceName={params.serviceName}
/>,
<TransactionNameField
currentValue={params.transactionName}
onChange={(value) => setRuleParams('transactionName', value)}
serviceName={params.serviceName}
/>,
<IsAboveField
value={params.threshold}
unit="%"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { rangeQuery, termQuery } from '@kbn/observability-plugin/server';
import {
SERVICE_NAME,
TRANSACTION_TYPE,
TRANSACTION_NAME,
} from '../../../../../common/es_fields/apm';
import { environmentQuery } from '../../../../../common/utils/environment_query';
import { AlertParams } from '../../route';
Expand Down Expand Up @@ -39,8 +40,15 @@ export async function getTransactionErrorRateChartPreview({
apmEventClient: APMEventClient;
alertParams: AlertParams;
}): Promise<TransactionErrorRateChartPreviewResponse> {
const { serviceName, environment, transactionType, interval, start, end } =
alertParams;
const {
serviceName,
environment,
transactionType,
interval,
start,
end,
transactionName,
} = alertParams;

const searchAggregatedTransactions = await getSearchTransactionsEvents({
config,
Expand All @@ -62,6 +70,7 @@ export async function getTransactionErrorRateChartPreview({
filter: [
...termQuery(SERVICE_NAME, serviceName),
...termQuery(TRANSACTION_TYPE, transactionType),
...termQuery(TRANSACTION_NAME, transactionName),
...rangeQuery(start, end),
...environmentQuery(environment),
...getDocumentTypeFilterForTransactions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
SERVICE_ENVIRONMENT,
SERVICE_NAME,
TRANSACTION_TYPE,
TRANSACTION_NAME,
} from '../../../../../common/es_fields/apm';
import { EventOutcome } from '../../../../../common/event_outcome';
import {
Expand Down Expand Up @@ -86,6 +87,7 @@ export function registerTransactionErrorRateRuleType({
apmActionVariables.interval,
apmActionVariables.reason,
apmActionVariables.serviceName,
apmActionVariables.transactionName,
apmActionVariables.threshold,
apmActionVariables.transactionType,
apmActionVariables.triggerValue,
Expand Down Expand Up @@ -144,6 +146,7 @@ export function registerTransactionErrorRateRuleType({
},
...termQuery(SERVICE_NAME, ruleParams.serviceName),
...termQuery(TRANSACTION_TYPE, ruleParams.transactionType),
...termQuery(TRANSACTION_NAME, ruleParams.transactionName),
...environmentQuery(ruleParams.environment),
],
},
Expand Down Expand Up @@ -232,6 +235,7 @@ export function registerTransactionErrorRateRuleType({
serviceName,
transactionType,
environment,
ruleParams.transactionName,
]
.filter((name) => name)
.join('_');
Expand All @@ -255,6 +259,7 @@ export function registerTransactionErrorRateRuleType({
[SERVICE_NAME]: serviceName,
...getEnvironmentEsField(environment),
[TRANSACTION_TYPE]: transactionType,
[TRANSACTION_NAME]: ruleParams.transactionName,
[PROCESSOR_EVENT]: ProcessorEvent.transaction,
[ALERT_EVALUATION_VALUE]: errorRate,
[ALERT_EVALUATION_THRESHOLD]: ruleParams.threshold,
Expand All @@ -272,6 +277,7 @@ export function registerTransactionErrorRateRuleType({
serviceName,
threshold: ruleParams.threshold,
transactionType,
transactionName: ruleParams.transactionName,
triggerValue: asDecimalOrInteger(errorRate),
viewInAppUrl,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,61 @@ export default function ApiTest({ getService }: FtrProviderContext) {
).to.equal(true);
});

it('transaction_error_rate with transaction name', async () => {
const options = {
params: {
query: {
start,
end,
serviceName: 'opbeans-java',
transactionName: 'APIRestController#product',
transactionType: 'request',
environment: 'ENVIRONMENT_ALL',
interval: '5m',
},
},
};

const response = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/rule_types/transaction_error_rate/chart_preview',
...options,
});

expect(response.status).to.be(200);
expect(response.body.errorRateChartPreview[0]).to.eql({
x: 1627974600000,
y: 1,
});
});

it('transaction_error_rate with nonexistent transaction name', async () => {
const options = {
params: {
query: {
start,
end,
serviceName: 'opbeans-java',
transactionName: 'foo',
transactionType: 'request',
environment: 'ENVIRONMENT_ALL',
interval: '5m',
},
},
};

const response = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/rule_types/transaction_error_rate/chart_preview',
...options,
});

expect(response.status).to.be(200);
expect(
response.body.errorRateChartPreview.every(
(item: { x: number; y: number | null }) => item.y === null
)
).to.equal(true);
});

it('error_count (with data)', async () => {
const options = getOptions();
options.params.query.transactionType = undefined;
Expand Down