Skip to content

Commit

Permalink
[APM] Query transaction metrics for alerts (#108167)
Browse files Browse the repository at this point in the history
Closes #78122.
  • Loading branch information
dgieselaar authored Aug 16, 2021
1 parent a80243a commit cd3cc29
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ALERT_REASON as ALERT_REASON_NON_TYPED,
// @ts-expect-error
} from '@kbn/rule-data-utils/target_node/technical_field_names';
import { SearchAggregatedTransactionSetting } from '../../../common/aggregated_transactions';
import { asDuration } from '../../../../observability/common/utils/formatters';
import { createLifecycleRuleTypeFactory } from '../../../../rule_registry/server';
import {
Expand All @@ -44,6 +45,7 @@ import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';
import { RegisterRuleDependencies } from './register_apm_alerts';
import { getDocumentTypeFilterForAggregatedTransactions } from '../helpers/aggregated_transactions';

const ALERT_EVALUATION_THRESHOLD: typeof ALERT_EVALUATION_THRESHOLD_TYPED = ALERT_EVALUATION_THRESHOLD_NON_TYPED;
const ALERT_EVALUATION_VALUE: typeof ALERT_EVALUATION_VALUE_TYPED = ALERT_EVALUATION_VALUE_NON_TYPED;
Expand Down Expand Up @@ -105,8 +107,19 @@ export function registerTransactionDurationAlertType({
savedObjectsClient: services.savedObjectsClient,
});

// only query transaction events when set to 'never',
// to prevent (likely) unnecessary blocking request
// in rule execution
const searchAggregatedTransactions =
config['xpack.apm.searchAggregatedTransactions'] !==
SearchAggregatedTransactionSetting.never;

const index = searchAggregatedTransactions
? indices['apm_oss.metricsIndices']
: indices['apm_oss.transactionIndices'];

const searchParams = {
index: indices['apm_oss.transactionIndices'],
index,
body: {
size: 0,
query: {
Expand All @@ -119,9 +132,9 @@ export function registerTransactionDurationAlertType({
},
},
},
{
term: { [PROCESSOR_EVENT]: ProcessorEvent.transaction },
},
...getDocumentTypeFilterForAggregatedTransactions(
searchAggregatedTransactions
),
{ term: { [SERVICE_NAME]: alertParams.serviceName } },
{
term: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';
import { RegisterRuleDependencies } from './register_apm_alerts';
import { SearchAggregatedTransactionSetting } from '../../../common/aggregated_transactions';
import { getDocumentTypeFilterForAggregatedTransactions } from '../helpers/aggregated_transactions';
import { asPercent } from '../../../../observability/common/utils/formatters';

const ALERT_EVALUATION_THRESHOLD: typeof ALERT_EVALUATION_THRESHOLD_TYPED = ALERT_EVALUATION_THRESHOLD_NON_TYPED;
Expand Down Expand Up @@ -102,9 +104,20 @@ export function registerTransactionErrorRateAlertType({
savedObjectsClient: services.savedObjectsClient,
});

// only query transaction events when set to 'never',
// to prevent (likely) unnecessary blocking request
// in rule execution
const searchAggregatedTransactions =
config['xpack.apm.searchAggregatedTransactions'] !==
SearchAggregatedTransactionSetting.never;

const index = searchAggregatedTransactions
? indices['apm_oss.metricsIndices']
: indices['apm_oss.transactionIndices'];

const searchParams = {
index: indices['apm_oss.transactionIndices'],
size: 1,
index,
size: 0,
body: {
query: {
bool: {
Expand All @@ -116,7 +129,9 @@ export function registerTransactionErrorRateAlertType({
},
},
},
{ term: { [PROCESSOR_EVENT]: ProcessorEvent.transaction } },
...getDocumentTypeFilterForAggregatedTransactions(
searchAggregatedTransactions
),
{
terms: {
[EVENT_OUTCOME]: [
Expand Down
33 changes: 18 additions & 15 deletions x-pack/test/apm_api_integration/tests/alerts/rule_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export default function ApiTest({ getService }: FtrProviderContext) {
const INDEXING_DELAY = 5000;

const ALERTS_INDEX_TARGET = '.kibana-alerts-observability.apm.alerts*';
const APM_TRANSACTION_INDEX_NAME = 'apm-8.0.0-transaction';
const APM_METRIC_INDEX_NAME = 'apm-8.0.0-transaction';

const createTransactionEvent = (override: Record<string, any>) => {
const createTransactionMetric = (override: Record<string, any>) => {
const now = Date.now();

const time = now - INDEXING_DELAY;
Expand All @@ -61,12 +61,15 @@ export default function ApiTest({ getService }: FtrProviderContext) {
},
transaction: {
duration: {
us: 1000000,
histogram: {
values: [1000000],
counts: [1],
},
},
type: 'request',
},
processor: {
event: 'transaction',
event: 'metric',
},
observer: {
version_major: 7,
Expand Down Expand Up @@ -141,7 +144,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {

before(async () => {
await es.indices.create({
index: APM_TRANSACTION_INDEX_NAME,
index: APM_METRIC_INDEX_NAME,
body: {
mappings: {
dynamic: 'strict',
Expand Down Expand Up @@ -184,8 +187,8 @@ export default function ApiTest({ getService }: FtrProviderContext) {
},
duration: {
properties: {
us: {
type: 'long',
histogram: {
type: 'histogram',
},
},
},
Expand Down Expand Up @@ -252,7 +255,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

await es.indices.delete({
index: APM_TRANSACTION_INDEX_NAME,
index: APM_METRIC_INDEX_NAME,
});
});

Expand Down Expand Up @@ -281,8 +284,8 @@ export default function ApiTest({ getService }: FtrProviderContext) {
expect(beforeDataResponse.body.hits.hits.length).to.be(0);

await es.index({
index: APM_TRANSACTION_INDEX_NAME,
body: createTransactionEvent({
index: APM_METRIC_INDEX_NAME,
body: createTransactionMetric({
event: {
outcome: 'success',
},
Expand Down Expand Up @@ -310,8 +313,8 @@ export default function ApiTest({ getService }: FtrProviderContext) {
expect(afterInitialDataResponse.body.hits.hits.length).to.be(0);

await es.index({
index: APM_TRANSACTION_INDEX_NAME,
body: createTransactionEvent({
index: APM_METRIC_INDEX_NAME,
body: createTransactionMetric({
event: {
outcome: 'failure',
},
Expand Down Expand Up @@ -410,16 +413,16 @@ export default function ApiTest({ getService }: FtrProviderContext) {
`);

await es.bulk({
index: APM_TRANSACTION_INDEX_NAME,
index: APM_METRIC_INDEX_NAME,
body: [
{ index: {} },
createTransactionEvent({
createTransactionMetric({
event: {
outcome: 'success',
},
}),
{ index: {} },
createTransactionEvent({
createTransactionMetric({
event: {
outcome: 'success',
},
Expand Down

0 comments on commit cd3cc29

Please sign in to comment.