From 4e9479da97f7cdec451012229da2b3647695114b Mon Sep 17 00:00:00 2001 From: Kerry Gallagher Date: Thu, 22 Oct 2020 17:51:14 +0100 Subject: [PATCH] Query adjustments for getClientMetrics --- .../__snapshots__/queries.test.ts.snap | 11 +--------- .../lib/rum_client/get_client_metrics.ts | 13 ++++-------- .../projections/rum_page_load_transactions.ts | 20 ++++++++++++------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/rum_client/__snapshots__/queries.test.ts.snap b/x-pack/plugins/apm/server/lib/rum_client/__snapshots__/queries.test.ts.snap index 1fafa080824439..f17034402e32ca 100644 --- a/x-pack/plugins/apm/server/lib/rum_client/__snapshots__/queries.test.ts.snap +++ b/x-pack/plugins/apm/server/lib/rum_client/__snapshots__/queries.test.ts.snap @@ -31,11 +31,6 @@ Object { ], }, }, - "pageViews": Object { - "value_count": Object { - "field": "transaction.duration.us", - }, - }, }, "query": Object { "bool": Object { @@ -54,11 +49,6 @@ Object { "transaction.type": "page-load", }, }, - Object { - "exists": Object { - "field": "transaction.marks.navigationTiming.fetchStart", - }, - }, Object { "term": Object { "service.environment": "test", @@ -68,6 +58,7 @@ Object { }, }, "size": 0, + "track_total_hits": true, }, } `; diff --git a/x-pack/plugins/apm/server/lib/rum_client/get_client_metrics.ts b/x-pack/plugins/apm/server/lib/rum_client/get_client_metrics.ts index 6d596246d6af92..ee6ba4a7d20262 100644 --- a/x-pack/plugins/apm/server/lib/rum_client/get_client_metrics.ts +++ b/x-pack/plugins/apm/server/lib/rum_client/get_client_metrics.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { TRANSACTION_DURATION } from '../../../common/elasticsearch_fieldnames'; import { getRumPageLoadTransactionsProjection } from '../../projections/rum_page_load_transactions'; import { mergeProjection } from '../../projections/util/merge_projection'; import { Setup, SetupTimeRange } from '../helpers/setup_request'; @@ -25,17 +24,14 @@ export async function getClientMetrics({ const projection = getRumPageLoadTransactionsProjection({ setup, urlQuery, + checkFetchStartFieldExists: false, }); const params = mergeProjection(projection, { body: { size: 0, + track_total_hits: true, aggs: { - pageViews: { - value_count: { - field: TRANSACTION_DURATION, - }, - }, backEnd: { percentiles: { field: TRANSACTION_TIME_TO_FIRST_BYTE, @@ -59,15 +55,14 @@ export async function getClientMetrics({ }); const { apmEventClient } = setup; - const response = await apmEventClient.search(params); - const { backEnd, domInteractive, pageViews } = response.aggregations!; + const { backEnd, domInteractive } = response.aggregations!; const pkey = percentile.toFixed(1); // Divide by 1000 to convert ms into seconds return { - pageViews, + pageViews: { value: response.hits.total.value ?? 0 }, backEnd: { value: backEnd.values[pkey] || 0 }, frontEnd: { value: (domInteractive.values[pkey] || 0) - (backEnd.values[pkey] || 0), diff --git a/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts b/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts index 530ca36b5dc9f3..4c665d0d8c5d02 100644 --- a/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts +++ b/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts @@ -17,9 +17,11 @@ import { TRANSACTION_PAGE_LOAD } from '../../common/transaction_types'; export function getRumPageLoadTransactionsProjection({ setup, urlQuery, + checkFetchStartFieldExists = true, }: { setup: Setup & SetupTimeRange; urlQuery?: string; + checkFetchStartFieldExists?: boolean; }) { const { start, end, esFilter } = setup; @@ -27,13 +29,17 @@ export function getRumPageLoadTransactionsProjection({ filter: [ { range: rangeFilter(start, end) }, { term: { [TRANSACTION_TYPE]: TRANSACTION_PAGE_LOAD } }, - { - // Adding this filter to cater for some inconsistent rum data - // not available on aggregated transactions - exists: { - field: 'transaction.marks.navigationTiming.fetchStart', - }, - }, + ...(checkFetchStartFieldExists + ? [ + { + // Adding this filter to cater for some inconsistent rum data + // not available on aggregated transactions + exists: { + field: 'transaction.marks.navigationTiming.fetchStart', + }, + }, + ] + : []), ...(urlQuery ? [ {