Skip to content

Commit 36da4e1

Browse files
authored
[7.x] [APM] Use transaction metrics for error rate (#78009) (#78117)
Closes #77716.
1 parent 30896ac commit 36da4e1

File tree

5 files changed

+82
-20
lines changed

5 files changed

+82
-20
lines changed

x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ async function getErrorStats({
9696
setup,
9797
serviceName,
9898
environment,
99+
searchAggregatedTransactions,
99100
}: {
100101
setup: Options['setup'];
101102
serviceName: string;
102103
environment?: string;
104+
searchAggregatedTransactions: boolean;
103105
}) {
104106
const setupWithBlankUiFilters = {
105107
...setup,
@@ -108,6 +110,7 @@ async function getErrorStats({
108110
const { noHits, average } = await getErrorRate({
109111
setup: setupWithBlankUiFilters,
110112
serviceName,
113+
searchAggregatedTransactions,
111114
});
112115
return { avgErrorRate: noHits ? null : average };
113116
}

x-pack/plugins/apm/server/lib/services/__snapshots__/queries.test.ts.snap

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/apm/server/lib/services/get_services/get_services_items_stats.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
EVENT_OUTCOME,
1515
} from '../../../../common/elasticsearch_fieldnames';
1616
import { mergeProjection } from '../../../projections/util/merge_projection';
17-
import { ProcessorEvent } from '../../../../common/processor_event';
1817
import {
1918
ServicesItemsSetup,
2019
ServicesItemsProjection,
@@ -258,19 +257,33 @@ export const getTransactionRates = async ({
258257
export const getTransactionErrorRates = async ({
259258
setup,
260259
projection,
260+
searchAggregatedTransactions,
261261
}: AggregationParams) => {
262262
const { apmEventClient, start, end } = setup;
263263

264264
const outcomes = {
265265
terms: {
266266
field: EVENT_OUTCOME,
267267
},
268+
aggs: {
269+
count: {
270+
value_count: {
271+
field: getTransactionDurationFieldForAggregatedTransactions(
272+
searchAggregatedTransactions
273+
),
274+
},
275+
},
276+
},
268277
};
269278

270279
const response = await apmEventClient.search(
271280
mergeProjection(projection, {
272281
apm: {
273-
events: [ProcessorEvent.transaction],
282+
events: [
283+
getProcessorEventForAggregatedTransactions(
284+
searchAggregatedTransactions
285+
),
286+
],
274287
},
275288
body: {
276289
size: 0,
@@ -319,11 +332,11 @@ export const getTransactionErrorRates = async ({
319332
const successfulTransactions =
320333
outcomeResponse.buckets.find(
321334
(bucket) => bucket.key === EventOutcome.success
322-
)?.doc_count ?? 0;
335+
)?.count.value ?? 0;
323336
const failedTransactions =
324337
outcomeResponse.buckets.find(
325338
(bucket) => bucket.key === EventOutcome.failure
326-
)?.doc_count ?? 0;
339+
)?.count.value ?? 0;
327340

328341
return failedTransactions / (successfulTransactions + failedTransactions);
329342
}

x-pack/plugins/apm/server/lib/transaction_groups/get_error_rate.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,30 @@ import {
1111
SERVICE_NAME,
1212
EVENT_OUTCOME,
1313
} from '../../../common/elasticsearch_fieldnames';
14-
import { ProcessorEvent } from '../../../common/processor_event';
1514
import { rangeFilter } from '../../../common/utils/range_filter';
1615
import {
1716
Setup,
1817
SetupTimeRange,
1918
SetupUIFilters,
2019
} from '../helpers/setup_request';
2120
import { getBucketSize } from '../helpers/get_bucket_size';
21+
import {
22+
getProcessorEventForAggregatedTransactions,
23+
getTransactionDurationFieldForAggregatedTransactions,
24+
} from '../helpers/aggregated_transactions';
2225

2326
export async function getErrorRate({
2427
serviceName,
2528
transactionType,
2629
transactionName,
2730
setup,
31+
searchAggregatedTransactions,
2832
}: {
2933
serviceName: string;
3034
transactionType?: string;
3135
transactionName?: string;
3236
setup: Setup & SetupTimeRange & SetupUIFilters;
37+
searchAggregatedTransactions: boolean;
3338
}) {
3439
const { start, end, uiFiltersES, apmEventClient } = setup;
3540

@@ -53,7 +58,11 @@ export async function getErrorRate({
5358

5459
const params = {
5560
apm: {
56-
events: [ProcessorEvent.transaction],
61+
events: [
62+
getProcessorEventForAggregatedTransactions(
63+
searchAggregatedTransactions
64+
),
65+
],
5766
},
5867
body: {
5968
size: 0,
@@ -67,8 +76,19 @@ export async function getErrorRate({
6776
extended_bounds: { min: start, max: end },
6877
},
6978
aggs: {
70-
erroneous_transactions: {
71-
filter: { term: { [EVENT_OUTCOME]: EventOutcome.failure } },
79+
[EVENT_OUTCOME]: {
80+
terms: {
81+
field: EVENT_OUTCOME,
82+
},
83+
aggs: {
84+
count: {
85+
value_count: {
86+
field: getTransactionDurationFieldForAggregatedTransactions(
87+
searchAggregatedTransactions
88+
),
89+
},
90+
},
91+
},
7292
},
7393
},
7494
},
@@ -81,18 +101,24 @@ export async function getErrorRate({
81101
const noHits = resp.hits.total.value === 0;
82102

83103
const erroneousTransactionsRate =
84-
resp.aggregations?.total_transactions.buckets.map(
85-
({
86-
key,
87-
doc_count: totalTransactions,
88-
erroneous_transactions: erroneousTransactions,
89-
}) => {
90-
return {
91-
x: key,
92-
y: erroneousTransactions.doc_count / totalTransactions,
93-
};
94-
}
95-
) || [];
104+
resp.aggregations?.total_transactions.buckets.map((bucket) => {
105+
const successful =
106+
bucket[EVENT_OUTCOME].buckets.find(
107+
(eventOutcomeBucket) =>
108+
eventOutcomeBucket.key === EventOutcome.success
109+
)?.count.value ?? 0;
110+
111+
const failed =
112+
bucket[EVENT_OUTCOME].buckets.find(
113+
(eventOutcomeBucket) =>
114+
eventOutcomeBucket.key === EventOutcome.failure
115+
)?.count.value ?? 0;
116+
117+
return {
118+
x: bucket.key,
119+
y: failed / (successful + failed),
120+
};
121+
}) || [];
96122

97123
const average = mean(
98124
erroneousTransactionsRate

x-pack/plugins/apm/server/routes/transaction_groups.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,17 @@ export const transactionGroupsErrorRateRoute = createRoute(() => ({
210210
const { params } = context;
211211
const { serviceName } = params.path;
212212
const { transactionType, transactionName } = params.query;
213+
214+
const searchAggregatedTransactions = await getSearchAggregatedTransactions(
215+
setup
216+
);
217+
213218
return getErrorRate({
214219
serviceName,
215220
transactionType,
216221
transactionName,
217222
setup,
223+
searchAggregatedTransactions,
218224
});
219225
},
220226
}));

0 commit comments

Comments
 (0)