Skip to content

Commit

Permalink
[APM] Guard searches with range/additional queries (#92112) (#92363)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Dario Gieselaar <dario.gieselaar@elastic.co>
  • Loading branch information
kibanamachine and dgieselaar authored Feb 23, 2021
1 parent 05eb021 commit c796559
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 24 deletions.

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 @@ -9,16 +9,16 @@
},
"hits": {
"total": {
"value": 10000,
"value": 1,
"relation": "gte"
},
"max_score": null,
"hits": []
},
"aggregations": {
"first_seen": {
"value": 1.5281138E12,
"value_as_string": "2018-06-04T12:00:00.000Z"
}
"hits": [
{
"_source": {
"@timestamp": "2018-06-04T12:00:00.000Z"
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { isNumber } from 'lodash';
import { isFiniteNumber } from '../../../../common/utils/is_finite_number';
import { ESFilter } from '../../../../../../typings/elasticsearch';
import { Annotation, AnnotationType } from '../../../../common/annotations';
import {
Expand Down Expand Up @@ -85,25 +85,23 @@ export async function getDerivedServiceAnnotations({
],
},
body: {
size: 0,
size: 1,
query: {
bool: {
filter: [...filter, { term: { [SERVICE_VERSION]: version } }],
},
},
aggs: {
first_seen: {
min: {
field: '@timestamp',
},
},
sort: {
'@timestamp': 'desc',
},
},
});

const firstSeen = response.aggregations?.first_seen.value;
const firstSeen = new Date(
response.hits.hits[0]._source['@timestamp']
).getTime();

if (!isNumber(firstSeen)) {
if (!isFiniteNumber(firstSeen)) {
throw new Error(
'First seen for version was unexpectedly undefined or null.'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ describe('getServiceAnnotations', () => {
{
id: '8.0.0',
text: '8.0.0',
'@timestamp': 1.5281138e12,
'@timestamp': new Date('2018-06-04T12:00:00.000Z').getTime(),
type: 'version',
},
{
id: '7.5.0',
text: '7.5.0',
'@timestamp': 1.5281138e12,
'@timestamp': new Date('2018-06-04T12:00:00.000Z').getTime(),
type: 'version',
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
* 2.0.
*/

import { rangeQuery } from '../../../../common/utils/queries';
import { ProcessorEvent } from '../../../../common/processor_event';
import { OBSERVER_VERSION_MAJOR } from '../../../../common/elasticsearch_fieldnames';
import { Setup } from '../../helpers/setup_request';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';
import { withApmSpan } from '../../../utils/with_apm_span';

// returns true if 6.x data is found
export async function getLegacyDataStatus(setup: Setup) {
export async function getLegacyDataStatus(setup: Setup & SetupTimeRange) {
return withApmSpan('get_legacy_data_status', async () => {
const { apmEventClient } = setup;
const { apmEventClient, start, end } = setup;

const params = {
terminateAfter: 1,
Expand All @@ -24,7 +25,10 @@ export async function getLegacyDataStatus(setup: Setup) {
size: 0,
query: {
bool: {
filter: [{ range: { [OBSERVER_VERSION_MAJOR]: { lt: 7 } } }],
filter: [
{ range: { [OBSERVER_VERSION_MAJOR]: { lt: 7 } } },
...rangeQuery(start, end),
],
},
},
},
Expand Down

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

9 changes: 9 additions & 0 deletions x-pack/plugins/apm/server/lib/transactions/breakdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ export function getTransactionBreakdown({
...rangeQuery(start, end),
...environmentQuery(environment),
...esFilter,
{
bool: {
should: [
{ exists: { field: SPAN_SELF_TIME_SUM } },
{ exists: { field: TRANSACTION_BREAKDOWN_COUNT } },
],
minimum_should_match: 1,
},
},
];

if (transactionName) {
Expand Down

0 comments on commit c796559

Please sign in to comment.