-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APM] Anomaly detection integration with transaction duration chart (#…
…71230) * Reintroduces the previous anomaly detection ML integration back into the transaction duration chart in the service details screen. Support the latest APM anoamly detection by environment jobs. * PR feedback * Code improvements from PR feedback * handle errors thrown when fetching ml job for current environment Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
f0d744e
commit f0c9915
Showing
11 changed files
with
390 additions
and
23 deletions.
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
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
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
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
93 changes: 93 additions & 0 deletions
93
x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/fetcher.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,93 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { Logger } from 'kibana/server'; | ||
import { PromiseReturnType } from '../../../../../../observability/typings/common'; | ||
import { Setup, SetupTimeRange } from '../../../helpers/setup_request'; | ||
|
||
export type ESResponse = Exclude< | ||
PromiseReturnType<typeof anomalySeriesFetcher>, | ||
undefined | ||
>; | ||
|
||
export async function anomalySeriesFetcher({ | ||
serviceName, | ||
transactionType, | ||
intervalString, | ||
mlBucketSize, | ||
setup, | ||
jobId, | ||
logger, | ||
}: { | ||
serviceName: string; | ||
transactionType: string; | ||
intervalString: string; | ||
mlBucketSize: number; | ||
setup: Setup & SetupTimeRange; | ||
jobId: string; | ||
logger: Logger; | ||
}) { | ||
const { ml, start, end } = setup; | ||
if (!ml) { | ||
return; | ||
} | ||
|
||
// move the start back with one bucket size, to ensure to get anomaly data in the beginning | ||
// this is required because ML has a minimum bucket size (default is 900s) so if our buckets are smaller, we might have several null buckets in the beginning | ||
const newStart = start - mlBucketSize * 1000; | ||
|
||
const params = { | ||
body: { | ||
size: 0, | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ term: { job_id: jobId } }, | ||
{ exists: { field: 'bucket_span' } }, | ||
{ term: { result_type: 'model_plot' } }, | ||
{ term: { partition_field_value: serviceName } }, | ||
{ term: { by_field_value: transactionType } }, | ||
{ | ||
range: { | ||
timestamp: { gte: newStart, lte: end, format: 'epoch_millis' }, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
ml_avg_response_times: { | ||
date_histogram: { | ||
field: 'timestamp', | ||
fixed_interval: intervalString, | ||
min_doc_count: 0, | ||
extended_bounds: { min: newStart, max: end }, | ||
}, | ||
aggs: { | ||
anomaly_score: { max: { field: 'anomaly_score' } }, | ||
lower: { min: { field: 'model_lower' } }, | ||
upper: { max: { field: 'model_upper' } }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
try { | ||
const response = await ml.mlSystem.mlAnomalySearch(params); | ||
return response; | ||
} catch (err) { | ||
const isHttpError = 'statusCode' in err; | ||
if (isHttpError) { | ||
logger.info( | ||
`Status code "${err.statusCode}" while retrieving ML anomalies for APM` | ||
); | ||
return; | ||
} | ||
logger.error('An error occurred while retrieving ML anomalies for APM'); | ||
logger.error(err); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/get_ml_bucket_size.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,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { Logger } from 'kibana/server'; | ||
import { Setup, SetupTimeRange } from '../../../helpers/setup_request'; | ||
|
||
interface IOptions { | ||
setup: Setup & SetupTimeRange; | ||
jobId: string; | ||
logger: Logger; | ||
} | ||
|
||
interface ESResponse { | ||
bucket_span: number; | ||
} | ||
|
||
export async function getMlBucketSize({ | ||
setup, | ||
jobId, | ||
logger, | ||
}: IOptions): Promise<number | undefined> { | ||
const { ml, start, end } = setup; | ||
if (!ml) { | ||
return; | ||
} | ||
|
||
const params = { | ||
body: { | ||
_source: 'bucket_span', | ||
size: 1, | ||
terminateAfter: 1, | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ term: { job_id: jobId } }, | ||
{ exists: { field: 'bucket_span' } }, | ||
{ | ||
range: { | ||
timestamp: { gte: start, lte: end, format: 'epoch_millis' }, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
try { | ||
const resp = await ml.mlSystem.mlAnomalySearch<ESResponse>(params); | ||
return resp.hits.hits[0]?._source.bucket_span; | ||
} catch (err) { | ||
const isHttpError = 'statusCode' in err; | ||
if (isHttpError) { | ||
return; | ||
} | ||
logger.error(err); | ||
} | ||
} |
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.