Skip to content

Commit

Permalink
Return to using record result type on anomaly queries. These are the
Browse files Browse the repository at this point in the history
same values used in the anomaly explorer and it includes actual &
typical values which greatly improve performance of the previous query.
  • Loading branch information
ogupte committed Jun 9, 2020
1 parent bd1ca55 commit 106d0f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ const ANOMALY_DETECTION_NO_DATA_TEXT = i18n.translate(
}
);

function getDisplayedAnomalyScore(score: number) {
if (score > 0 && score < 1) {
return '<1';
}
return asInteger(score);
}

export function Contents({
selectedNodeData,
isService,
Expand Down Expand Up @@ -180,7 +187,7 @@ export function Contents({
</EuiFlexItem>
<EuiFlexItem grow={false}>
<div>
{asInteger(anomalyScore)}
{getDisplayedAnomalyScore(anomalyScore)}
{anomalyDescription && (
<SubduedText>&nbsp;({anomalyDescription})</SubduedText>
)}
Expand Down
62 changes: 13 additions & 49 deletions x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function getServiceAnomalies(
query: {
bool: {
filter: [
{ term: { result_type: 'bucket' } },
{ term: { result_type: 'record' } },
{
terms: {
job_id: apmJobIds,
Expand All @@ -82,8 +82,8 @@ export async function getServiceAnomalies(
aggs: {
top_score_hits: {
top_hits: {
sort: [{ anomaly_score: { order: 'desc' as const } }],
_source: ['anomaly_score', 'timestamp'],
sort: [{ record_score: { order: 'desc' as const } }],
_source: ['record_score', 'timestamp', 'typical', 'actual'],
size: 1,
},
},
Expand All @@ -101,7 +101,12 @@ export async function getServiceAnomalies(
top_score_hits: {
hits: {
hits: Array<{
_source: { anomaly_score: number; timestamp: number };
_source: {
record_score: number;
timestamp: number;
typical: number[];
actual: number[];
};
}>;
};
};
Expand All @@ -114,52 +119,11 @@ export async function getServiceAnomalies(
const bucketSource = jobBucket.top_score_hits.hits.hits?.[0]?._source;
return {
jobId,
anomalyScore: bucketSource.anomaly_score,
anomalyScore: bucketSource.record_score,
timestamp: bucketSource.timestamp,
typical: bucketSource.typical[0],
actual: bucketSource.actual[0],
};
});
const anomalyModelValuePromises = anomalyScores.map(
({ jobId, timestamp }) => {
return (async () => {
try {
const modelPlotResponse = await ml.mlSystem.mlAnomalySearch({
body: {
size: 1,
query: {
bool: {
filter: [
{ term: { result_type: 'model_plot' } },
{ term: { job_id: jobId } },
{ term: { timestamp } },
],
},
},
_source: ['actual', 'model_median'],
},
});
if (modelPlotResponse.hits.hits.length === 0) {
return;
}
const { actual, model_median } = modelPlotResponse.hits.hits[0]
._source as {
actual: number;
model_median: number;
};
return { jobId, actual, model_median };
} catch (error) {
return;
}
})();
}
);

const anomalyModelValues = (
await Promise.all(anomalyModelValuePromises)
).filter(<T>(value: T | undefined): value is T => value !== undefined);

return leftJoin(
apmMlJobCategories,
'jobId',
leftJoin(anomalyScores, 'jobId', anomalyModelValues)
);
return leftJoin(apmMlJobCategories, 'jobId', anomalyScores);
}
4 changes: 2 additions & 2 deletions x-pack/plugins/apm/server/lib/service_map/ml_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('addAnomaliesDataToNodes', () => {
anomalyScore: 50,
timestamp: 1591351200000,
actual: 2000,
model_median: 1000,
typical: 1000,
},
{
jobId: 'opbeans-java-request-high_mean_response_time',
Expand All @@ -39,7 +39,7 @@ describe('addAnomaliesDataToNodes', () => {
anomalyScore: 100,
timestamp: 1591351200000,
actual: 9000,
model_median: 3000,
typical: 3000,
},
];

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/server/lib/service_map/ml_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function addAnomaliesDataToNodes(
acc[anomalyJob.serviceName] = {
anomaly_score: anomalyJob.anomalyScore,
actual_value: anomalyJob.actual,
typical_value: anomalyJob.model_median,
typical_value: anomalyJob.typical,
ml_job_id: anomalyJob.jobId,
};
}
Expand Down

0 comments on commit 106d0f2

Please sign in to comment.