Fix long tasks query (#79099)

This commit is contained in:
Shahzad 2020-10-01 22:49:31 +02:00 committed by GitHub
parent 3078908093
commit 8406e04a42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 140 deletions

View file

@ -167,27 +167,42 @@ exports[`rum client dashboard queries fetches long task metrics 1`] = `
Object {
"apm": Object {
"events": Array [
"span",
"transaction",
],
},
"body": Object {
"aggs": Object {
"transIds": Object {
"aggs": Object {
"longestLongTask": Object {
"max": Object {
"field": "span.duration.us",
},
},
"sumLongTask": Object {
"sum": Object {
"field": "span.duration.us",
},
"longTaskCount": Object {
"percentiles": Object {
"field": "transaction.experience.longtask.count",
"hdr": Object {
"number_of_significant_value_digits": 3,
},
"percents": Array [
50,
],
},
"terms": Object {
"field": "transaction.id",
"size": 1000,
},
"longTaskMax": Object {
"percentiles": Object {
"field": "transaction.experience.longtask.max",
"hdr": Object {
"number_of_significant_value_digits": 3,
},
"percents": Array [
50,
],
},
},
"longTaskSum": Object {
"percentiles": Object {
"field": "transaction.experience.longtask.sum",
"hdr": Object {
"number_of_significant_value_digits": 3,
},
"percents": Array [
50,
],
},
},
},
@ -205,7 +220,12 @@ Object {
},
Object {
"term": Object {
"span.type": "longtask",
"transaction.type": "page-load",
},
},
Object {
"exists": Object {
"field": "transaction.marks.navigationTiming.fetchStart",
},
},
Object {

View file

@ -4,99 +4,26 @@
* 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;
}) {
const projection = getRumLongTasksProjection({
setup,
});
const params = mergeProjection(projection, {
body: {
size: 0,
aggs: {
transIds: {
terms: {
field: 'transaction.id',
size: 1000,
},
aggs: {
sumLongTask: {
sum: {
field: SPAN_DURATION,
},
},
longestLongTask: {
max: {
field: SPAN_DURATION,
},
},
},
},
},
},
});
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;
(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!;
}
}
});
return {
noOfLongTasks,
sumOfLongTasks,
longestLongTask,
};
}
async function filterPageLoadTransactions({
setup,
urlQuery,
transactionIds,
}: {
setup: Setup & SetupTimeRange & SetupUIFilters;
urlQuery?: string;
transactionIds: string[];
percentile?: number;
}) {
const projection = getRumPageLoadTransactionsProjection({
setup,
@ -105,25 +32,51 @@ async function filterPageLoadTransactions({
const params = mergeProjection(projection, {
body: {
size: transactionIds.length,
query: {
bool: {
must: [
{
terms: {
[TRANSACTION_ID]: transactionIds,
},
size: 0,
aggs: {
longTaskSum: {
percentiles: {
field: LONG_TASK_SUM_FIELD,
percents: [percentile],
hdr: {
number_of_significant_value_digits: 3,
},
],
filter: [...projection.body.query.bool.filter],
},
},
longTaskCount: {
percentiles: {
field: LONG_TASK_COUNT_FIELD,
percents: [percentile],
hdr: {
number_of_significant_value_digits: 3,
},
},
},
longTaskMax: {
percentiles: {
field: LONG_TASK_MAX_FIELD,
percents: [percentile],
hdr: {
number_of_significant_value_digits: 3,
},
},
},
},
_source: [TRANSACTION_ID],
},
});
const { apmEventClient } = setup;
const response = await apmEventClient.search(params);
return response.hits.hits.map((hit) => (hit._source as any).transaction.id)!;
const pkey = percentile.toFixed(1);
const { longTaskSum, longTaskCount, longTaskMax } =
response.aggregations ?? {};
return {
noOfLongTasks: longTaskCount?.values[pkey] ?? 0,
sumOfLongTasks: longTaskSum?.values[pkey] ?? 0,
longestLongTask: longTaskMax?.values[pkey] ?? 0,
};
}

View file

@ -10,7 +10,6 @@ import {
SetupUIFilters,
} from '../../server/lib/helpers/setup_request';
import {
SPAN_TYPE,
AGENT_NAME,
TRANSACTION_TYPE,
SERVICE_LANGUAGE_NAME,
@ -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,
}: {

View file

@ -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,
});
},
}));

View file

@ -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,
}
`);
});