Skip to content

Commit 106d0f2

Browse files
committed
Return to using record result type on anomaly queries. These are the
same values used in the anomaly explorer and it includes actual & typical values which greatly improve performance of the previous query.
1 parent bd1ca55 commit 106d0f2

4 files changed

Lines changed: 24 additions & 53 deletions

File tree

x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ const ANOMALY_DETECTION_NO_DATA_TEXT = i18n.translate(
123123
}
124124
);
125125

126+
function getDisplayedAnomalyScore(score: number) {
127+
if (score > 0 && score < 1) {
128+
return '<1';
129+
}
130+
return asInteger(score);
131+
}
132+
126133
export function Contents({
127134
selectedNodeData,
128135
isService,
@@ -180,7 +187,7 @@ export function Contents({
180187
</EuiFlexItem>
181188
<EuiFlexItem grow={false}>
182189
<div>
183-
{asInteger(anomalyScore)}
190+
{getDisplayedAnomalyScore(anomalyScore)}
184191
{anomalyDescription && (
185192
<SubduedText>&nbsp;({anomalyDescription})</SubduedText>
186193
)}

x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export async function getServiceAnomalies(
6262
query: {
6363
bool: {
6464
filter: [
65-
{ term: { result_type: 'bucket' } },
65+
{ term: { result_type: 'record' } },
6666
{
6767
terms: {
6868
job_id: apmJobIds,
@@ -82,8 +82,8 @@ export async function getServiceAnomalies(
8282
aggs: {
8383
top_score_hits: {
8484
top_hits: {
85-
sort: [{ anomaly_score: { order: 'desc' as const } }],
86-
_source: ['anomaly_score', 'timestamp'],
85+
sort: [{ record_score: { order: 'desc' as const } }],
86+
_source: ['record_score', 'timestamp', 'typical', 'actual'],
8787
size: 1,
8888
},
8989
},
@@ -101,7 +101,12 @@ export async function getServiceAnomalies(
101101
top_score_hits: {
102102
hits: {
103103
hits: Array<{
104-
_source: { anomaly_score: number; timestamp: number };
104+
_source: {
105+
record_score: number;
106+
timestamp: number;
107+
typical: number[];
108+
actual: number[];
109+
};
105110
}>;
106111
};
107112
};
@@ -114,52 +119,11 @@ export async function getServiceAnomalies(
114119
const bucketSource = jobBucket.top_score_hits.hits.hits?.[0]?._source;
115120
return {
116121
jobId,
117-
anomalyScore: bucketSource.anomaly_score,
122+
anomalyScore: bucketSource.record_score,
118123
timestamp: bucketSource.timestamp,
124+
typical: bucketSource.typical[0],
125+
actual: bucketSource.actual[0],
119126
};
120127
});
121-
const anomalyModelValuePromises = anomalyScores.map(
122-
({ jobId, timestamp }) => {
123-
return (async () => {
124-
try {
125-
const modelPlotResponse = await ml.mlSystem.mlAnomalySearch({
126-
body: {
127-
size: 1,
128-
query: {
129-
bool: {
130-
filter: [
131-
{ term: { result_type: 'model_plot' } },
132-
{ term: { job_id: jobId } },
133-
{ term: { timestamp } },
134-
],
135-
},
136-
},
137-
_source: ['actual', 'model_median'],
138-
},
139-
});
140-
if (modelPlotResponse.hits.hits.length === 0) {
141-
return;
142-
}
143-
const { actual, model_median } = modelPlotResponse.hits.hits[0]
144-
._source as {
145-
actual: number;
146-
model_median: number;
147-
};
148-
return { jobId, actual, model_median };
149-
} catch (error) {
150-
return;
151-
}
152-
})();
153-
}
154-
);
155-
156-
const anomalyModelValues = (
157-
await Promise.all(anomalyModelValuePromises)
158-
).filter(<T>(value: T | undefined): value is T => value !== undefined);
159-
160-
return leftJoin(
161-
apmMlJobCategories,
162-
'jobId',
163-
leftJoin(anomalyScores, 'jobId', anomalyModelValues)
164-
);
128+
return leftJoin(apmMlJobCategories, 'jobId', anomalyScores);
165129
}

x-pack/plugins/apm/server/lib/service_map/ml_helpers.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('addAnomaliesDataToNodes', () => {
3030
anomalyScore: 50,
3131
timestamp: 1591351200000,
3232
actual: 2000,
33-
model_median: 1000,
33+
typical: 1000,
3434
},
3535
{
3636
jobId: 'opbeans-java-request-high_mean_response_time',
@@ -39,7 +39,7 @@ describe('addAnomaliesDataToNodes', () => {
3939
anomalyScore: 100,
4040
timestamp: 1591351200000,
4141
actual: 9000,
42-
model_median: 3000,
42+
typical: 3000,
4343
},
4444
];
4545

x-pack/plugins/apm/server/lib/service_map/ml_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function addAnomaliesDataToNodes(
3030
acc[anomalyJob.serviceName] = {
3131
anomaly_score: anomalyJob.anomalyScore,
3232
actual_value: anomalyJob.actual,
33-
typical_value: anomalyJob.model_median,
33+
typical_value: anomalyJob.typical,
3434
ml_job_id: anomalyJob.jobId,
3535
};
3636
}

0 commit comments

Comments
 (0)