Skip to content

Commit

Permalink
[APM] Use transaction metrics for transaction error rate
Browse files Browse the repository at this point in the history
Closes #77716.
  • Loading branch information
dgieselaar committed Sep 21, 2020
1 parent 8347fd0 commit b02100c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ async function getErrorStats({
setup,
serviceName,
environment,
searchAggregatedTransactions,
}: {
setup: Options['setup'];
serviceName: string;
environment?: string;
searchAggregatedTransactions: boolean;
}) {
const setupWithBlankUiFilters = {
...setup,
Expand All @@ -108,6 +110,7 @@ async function getErrorStats({
const { noHits, average } = await getErrorRate({
setup: setupWithBlankUiFilters,
serviceName,
searchAggregatedTransactions,
});
return { avgErrorRate: noHits ? null : average };
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
EVENT_OUTCOME,
} from '../../../../common/elasticsearch_fieldnames';
import { mergeProjection } from '../../../projections/util/merge_projection';
import { ProcessorEvent } from '../../../../common/processor_event';
import {
ServicesItemsSetup,
ServicesItemsProjection,
Expand Down Expand Up @@ -258,19 +257,33 @@ export const getTransactionRates = async ({
export const getTransactionErrorRates = async ({
setup,
projection,
searchAggregatedTransactions,
}: AggregationParams) => {
const { apmEventClient, start, end } = setup;

const outcomes = {
terms: {
field: EVENT_OUTCOME,
},
aggs: {
count: {
value_count: {
field: getTransactionDurationFieldForAggregatedTransactions(
searchAggregatedTransactions
),
},
},
},
};

const response = await apmEventClient.search(
mergeProjection(projection, {
apm: {
events: [ProcessorEvent.transaction],
events: [
getProcessorEventForAggregatedTransactions(
searchAggregatedTransactions
),
],
},
body: {
size: 0,
Expand Down Expand Up @@ -319,11 +332,11 @@ export const getTransactionErrorRates = async ({
const successfulTransactions =
outcomeResponse.buckets.find(
(bucket) => bucket.key === EventOutcome.success
)?.doc_count ?? 0;
)?.count.value ?? 0;
const failedTransactions =
outcomeResponse.buckets.find(
(bucket) => bucket.key === EventOutcome.failure
)?.doc_count ?? 0;
)?.count.value ?? 0;

return failedTransactions / (successfulTransactions + failedTransactions);
}
Expand Down
58 changes: 42 additions & 16 deletions x-pack/plugins/apm/server/lib/transaction_groups/get_error_rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,30 @@ import {
SERVICE_NAME,
EVENT_OUTCOME,
} from '../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../common/processor_event';
import { rangeFilter } from '../../../common/utils/range_filter';
import {
Setup,
SetupTimeRange,
SetupUIFilters,
} from '../helpers/setup_request';
import { getBucketSize } from '../helpers/get_bucket_size';
import {
getProcessorEventForAggregatedTransactions,
getTransactionDurationFieldForAggregatedTransactions,
} from '../helpers/aggregated_transactions';

export async function getErrorRate({
serviceName,
transactionType,
transactionName,
setup,
searchAggregatedTransactions,
}: {
serviceName: string;
transactionType?: string;
transactionName?: string;
setup: Setup & SetupTimeRange & SetupUIFilters;
searchAggregatedTransactions: boolean;
}) {
const { start, end, uiFiltersES, apmEventClient } = setup;

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

const params = {
apm: {
events: [ProcessorEvent.transaction],
events: [
getProcessorEventForAggregatedTransactions(
searchAggregatedTransactions
),
],
},
body: {
size: 0,
Expand All @@ -67,8 +76,19 @@ export async function getErrorRate({
extended_bounds: { min: start, max: end },
},
aggs: {
erroneous_transactions: {
filter: { term: { [EVENT_OUTCOME]: EventOutcome.failure } },
[EVENT_OUTCOME]: {
terms: {
field: EVENT_OUTCOME,
},
aggs: {
count: {
value_count: {
field: getTransactionDurationFieldForAggregatedTransactions(
searchAggregatedTransactions
),
},
},
},
},
},
},
Expand All @@ -81,18 +101,24 @@ export async function getErrorRate({
const noHits = resp.hits.total.value === 0;

const erroneousTransactionsRate =
resp.aggregations?.total_transactions.buckets.map(
({
key,
doc_count: totalTransactions,
erroneous_transactions: erroneousTransactions,
}) => {
return {
x: key,
y: erroneousTransactions.doc_count / totalTransactions,
};
}
) || [];
resp.aggregations?.total_transactions.buckets.map((bucket) => {
const successful =
bucket[EVENT_OUTCOME].buckets.find(
(eventOutcomeBucket) =>
eventOutcomeBucket.key === EventOutcome.success
)?.count.value ?? 0;

const failed =
bucket[EVENT_OUTCOME].buckets.find(
(eventOutcomeBucket) =>
eventOutcomeBucket.key === EventOutcome.failure
)?.count.value ?? 0;

return {
x: bucket.key,
y: failed / (successful + failed),
};
}) || [];

const average = mean(
erroneousTransactionsRate
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/apm/server/routes/transaction_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,17 @@ export const transactionGroupsErrorRateRoute = createRoute(() => ({
const { params } = context;
const { serviceName } = params.path;
const { transactionType, transactionName } = params.query;

const searchAggregatedTransactions = await getSearchAggregatedTransactions(
setup
);

return getErrorRate({
serviceName,
transactionType,
transactionName,
setup,
searchAggregatedTransactions,
});
},
}));

0 comments on commit b02100c

Please sign in to comment.