-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ML] Fractions API fix and jest unit tests for APM Latency Correlations. #103907
Merged
walterra
merged 8 commits into
elastic:master
from
walterra:ml-apm-correlations-unit-tests
Jul 5, 2021
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bcbaf25
unit tests for APM correlations.
walterra 700bd03
remove leading 0 from fractions array.
walterra 2642aab
unit tests for fetch functions.
walterra f42dda2
remove track_total_hits from field candidates query.
walterra 16ef560
simpliy aggs check code.
walterra 02a1fd2
remove parenthesis.
walterra 44d5cc5
restructure tests.
walterra 30c609e
Merge branch 'master' into ml-apm-correlations-unit-tests
walterra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
x-pack/plugins/apm/server/lib/search_strategies/correlations/get_query_with_params.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { getQueryWithParams } from './get_query_with_params'; | ||
|
||
describe('correlations', () => { | ||
describe('getQueryWithParams()', () => { | ||
it('returns the most basic query filtering on processor.event=transaction', () => { | ||
const query = getQueryWithParams({ params: { index: 'apm-*' } }); | ||
expect(query).toEqual({ | ||
bool: { | ||
filter: [{ term: { 'processor.event': 'transaction' } }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a query considering additional params', () => { | ||
const query = getQueryWithParams({ | ||
params: { | ||
index: 'apm-*', | ||
serviceName: 'actualServiceName', | ||
transactionName: 'actualTransactionName', | ||
start: '01-01-2021', | ||
end: '31-01-2021', | ||
environment: 'dev', | ||
percentileThresholdValue: 75, | ||
}, | ||
}); | ||
expect(query).toEqual({ | ||
bool: { | ||
filter: [ | ||
{ term: { 'processor.event': 'transaction' } }, | ||
{ | ||
term: { | ||
'service.name': 'actualServiceName', | ||
}, | ||
}, | ||
{ | ||
term: { | ||
'transaction.name': 'actualTransactionName', | ||
}, | ||
}, | ||
{ | ||
range: { | ||
'@timestamp': { | ||
gte: '01-01-2021', | ||
lte: '31-01-2021', | ||
}, | ||
}, | ||
}, | ||
{ | ||
term: { | ||
'service.environment': 'dev', | ||
}, | ||
}, | ||
{ | ||
range: { | ||
'transaction.duration.us': { | ||
gte: 75, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a query considering a custom field/value pair', () => { | ||
const query = getQueryWithParams({ | ||
params: { index: 'apm-*' }, | ||
fieldName: 'actualFieldName', | ||
fieldValue: 'actualFieldValue', | ||
}); | ||
expect(query).toEqual({ | ||
bool: { | ||
filter: [ | ||
{ term: { 'processor.event': 'transaction' } }, | ||
{ | ||
term: { | ||
actualFieldName: 'actualFieldValue', | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
x-pack/plugins/apm/server/lib/search_strategies/correlations/query_correlation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { estypes } from '@elastic/elasticsearch'; | ||
|
||
import type { ElasticsearchClient } from 'src/core/server'; | ||
|
||
import { | ||
fetchTransactionDurationCorrelation, | ||
getTransactionDurationCorrelationRequest, | ||
BucketCorrelation, | ||
} from './query_correlation'; | ||
|
||
const params = { index: 'apm-*' }; | ||
const expectations = [1, 3, 5]; | ||
const ranges = [{ to: 1 }, { from: 1, to: 3 }, { from: 3, to: 5 }, { from: 5 }]; | ||
const fractions = [1, 2, 4, 5]; | ||
const totalDocCount = 1234; | ||
|
||
describe('query_correlation', () => { | ||
describe('getTransactionDurationCorrelationRequest()', () => { | ||
it('applies options to the returned query with aggregations for correlations and k-test', () => { | ||
const query = getTransactionDurationCorrelationRequest( | ||
params, | ||
expectations, | ||
ranges, | ||
fractions, | ||
totalDocCount | ||
); | ||
|
||
expect(query.index).toBe(params.index); | ||
|
||
expect(query?.body?.aggs?.latency_ranges?.range?.field).toBe( | ||
'transaction.duration.us' | ||
); | ||
expect(query?.body?.aggs?.latency_ranges?.range?.ranges).toEqual(ranges); | ||
|
||
expect( | ||
(query?.body?.aggs?.transaction_duration_correlation as { | ||
bucket_correlation: BucketCorrelation; | ||
})?.bucket_correlation.function.count_correlation.indicator | ||
).toEqual({ | ||
fractions, | ||
expectations, | ||
doc_count: totalDocCount, | ||
}); | ||
|
||
expect( | ||
(query?.body?.aggs?.ks_test as any)?.bucket_count_ks_test?.fractions | ||
).toEqual(fractions); | ||
}); | ||
}); | ||
|
||
describe('fetchTransactionDurationCorrelation()', () => { | ||
it('returns the data from the aggregations', async () => { | ||
const latencyRangesBuckets = [{ to: 1 }, { from: 1, to: 2 }, { from: 2 }]; | ||
const transactionDurationCorrelationValue = 0.45; | ||
const KsTestLess = 0.01; | ||
|
||
const esClientSearchMock = jest.fn((req: estypes.SearchRequest): { | ||
body: estypes.SearchResponse; | ||
} => { | ||
return { | ||
body: ({ | ||
aggregations: { | ||
latency_ranges: { | ||
buckets: latencyRangesBuckets, | ||
}, | ||
transaction_duration_correlation: { | ||
value: transactionDurationCorrelationValue, | ||
}, | ||
ks_test: { less: KsTestLess }, | ||
}, | ||
} as unknown) as estypes.SearchResponse, | ||
}; | ||
}); | ||
|
||
const esClientMock = ({ | ||
search: esClientSearchMock, | ||
} as unknown) as ElasticsearchClient; | ||
|
||
const resp = await fetchTransactionDurationCorrelation( | ||
esClientMock, | ||
params, | ||
expectations, | ||
ranges, | ||
fractions, | ||
totalDocCount | ||
); | ||
|
||
expect(resp).toEqual({ | ||
correlation: transactionDurationCorrelationValue, | ||
ksTest: KsTestLess, | ||
ranges: latencyRangesBuckets, | ||
}); | ||
expect(esClientSearchMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, updated in 02a1fd2.