Skip to content

Commit

Permalink
[7.x] Fix long tasks query (#79099) (#79212)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Oct 2, 2020
1 parent ac57149 commit ace4db9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 131 deletions.

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

121 changes: 37 additions & 84 deletions x-pack/plugins/apm/server/lib/rum_client/get_long_task_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,60 @@
* you may not use this file except in compliance with the Elastic License.
*/

import {
getRumLongTasksProjection,
getRumPageLoadTransactionsProjection,
} from '../../projections/rum_page_load_transactions';
import { getRumPageLoadTransactionsProjection } from '../../projections/rum_page_load_transactions';
import { mergeProjection } from '../../projections/util/merge_projection';
import {
Setup,
SetupTimeRange,
SetupUIFilters,
} from '../helpers/setup_request';
import {
SPAN_DURATION,
TRANSACTION_ID,
} from '../../../common/elasticsearch_fieldnames';

const LONG_TASK_SUM_FIELD = 'transaction.experience.longtask.sum';
const LONG_TASK_COUNT_FIELD = 'transaction.experience.longtask.count';
const LONG_TASK_MAX_FIELD = 'transaction.experience.longtask.max';

export async function getLongTaskMetrics({
setup,
urlQuery,
percentile = 50,
}: {
setup: Setup & SetupTimeRange & SetupUIFilters;
urlQuery?: string;
percentile?: number;
}) {
const projection = getRumLongTasksProjection({
const projection = getRumPageLoadTransactionsProjection({
setup,
urlQuery,
});

const params = mergeProjection(projection, {
body: {
size: 0,
aggs: {
transIds: {
terms: {
field: 'transaction.id',
size: 1000,
longTaskSum: {
percentiles: {
field: LONG_TASK_SUM_FIELD,
percents: [percentile],
hdr: {
number_of_significant_value_digits: 3,
},
},
aggs: {
sumLongTask: {
sum: {
field: SPAN_DURATION,
},
},
longTaskCount: {
percentiles: {
field: LONG_TASK_COUNT_FIELD,
percents: [percentile],
hdr: {
number_of_significant_value_digits: 3,
},
longestLongTask: {
max: {
field: SPAN_DURATION,
},
},
},
longTaskMax: {
percentiles: {
field: LONG_TASK_MAX_FIELD,
percents: [percentile],
hdr: {
number_of_significant_value_digits: 3,
},
},
},
Expand All @@ -59,71 +68,15 @@ export async function getLongTaskMetrics({
const { apmEventClient } = setup;

const response = await apmEventClient.search(params);
const { transIds } = response.aggregations ?? {};

const validTransactions: string[] = await filterPageLoadTransactions({
setup,
urlQuery,
transactionIds: (transIds?.buckets ?? []).map(
(bucket) => bucket.key as string
),
});
let noOfLongTasks = 0;
let sumOfLongTasks = 0;
let longestLongTask = 0;
const pkey = percentile.toFixed(1);

(transIds?.buckets ?? []).forEach((bucket) => {
if (validTransactions.includes(bucket.key as string)) {
noOfLongTasks += bucket.doc_count;
sumOfLongTasks += bucket.sumLongTask.value ?? 0;
if ((bucket.longestLongTask.value ?? 0) > longestLongTask) {
longestLongTask = bucket.longestLongTask.value!;
}
}
});
const { longTaskSum, longTaskCount, longTaskMax } =
response.aggregations ?? {};

return {
noOfLongTasks,
sumOfLongTasks,
longestLongTask,
noOfLongTasks: longTaskCount?.values[pkey] ?? 0,
sumOfLongTasks: longTaskSum?.values[pkey] ?? 0,
longestLongTask: longTaskMax?.values[pkey] ?? 0,
};
}

async function filterPageLoadTransactions({
setup,
urlQuery,
transactionIds,
}: {
setup: Setup & SetupTimeRange & SetupUIFilters;
urlQuery?: string;
transactionIds: string[];
}) {
const projection = getRumPageLoadTransactionsProjection({
setup,
urlQuery,
});

const params = mergeProjection(projection, {
body: {
size: transactionIds.length,
query: {
bool: {
must: [
{
terms: {
[TRANSACTION_ID]: transactionIds,
},
},
],
filter: [...projection.body.query.bool.filter],
},
},
_source: [TRANSACTION_ID],
},
});

const { apmEventClient } = setup;

const response = await apmEventClient.search(params);
return response.hits.hits.map((hit) => (hit._source as any).transaction.id)!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
SetupUIFilters,
} from '../../server/lib/helpers/setup_request';
import {
SPAN_TYPE,
AGENT_NAME,
TRANSACTION_TYPE,
SERVICE_LANGUAGE_NAME,
Expand Down Expand Up @@ -66,33 +65,6 @@ export function getRumPageLoadTransactionsProjection({
};
}

export function getRumLongTasksProjection({
setup,
}: {
setup: Setup & SetupTimeRange & SetupUIFilters;
}) {
const { start, end, uiFiltersES } = setup;

const bool = {
filter: [
{ range: rangeFilter(start, end) },
{ term: { [SPAN_TYPE]: 'longtask' } },
...uiFiltersES,
],
};

return {
apm: {
events: [ProcessorEvent.span],
},
body: {
query: {
bool,
},
},
};
}

export function getRumErrorsProjection({
setup,
}: {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/apm/server/routes/rum_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ export const rumLongTaskMetrics = createRoute(() => ({
const setup = await setupRequest(context, request);

const {
query: { urlQuery },
query: { urlQuery, percentile },
} = context.params;

return getLongTaskMetrics({
setup,
urlQuery,
percentile: percentile ? Number(percentile) : undefined,
});
},
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export default function rumServicesApiTests({ getService }: FtrProviderContext)

expectSnapshot(response.body).toMatchInline(`
Object {
"longestLongTask": 109000,
"noOfLongTasks": 2,
"sumOfLongTasks": 168000,
"longestLongTask": 0,
"noOfLongTasks": 0,
"sumOfLongTasks": 0,
}
`);
});
Expand Down

0 comments on commit ace4db9

Please sign in to comment.