Skip to content
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

[APM] Fix service maps not loading when there are no APM ML jobs #69240

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,33 @@ import { leftJoin } from '../../../common/utils/left_join';
import { Job as AnomalyDetectionJob } from '../../../../ml/server';
import { PromiseReturnType } from '../../../typings/common';
import { IEnvOptions } from './get_service_map';
import { Setup } from '../helpers/setup_request';
import {
APM_ML_JOB_GROUP_NAME,
encodeForMlApi,
} from '../../../common/ml_job_constants';

async function getApmAnomalyDetectionJobs(
setup: Setup
): Promise<AnomalyDetectionJob[]> {
const { ml } = setup;

if (!ml) {
return [];
}
try {
const { jobs } = await ml.anomalyDetectors.jobs(APM_ML_JOB_GROUP_NAME);
return jobs;
} catch (error) {
if (error.statusCode === 404) {
return [];
}
throw error;
}
}

type ApmMlJobCategory = NonNullable<ReturnType<typeof getApmMlJobCategory>>;

export const getApmMlJobCategory = (
mlJob: AnomalyDetectionJob,
serviceNames: string[]
Expand Down Expand Up @@ -62,7 +83,10 @@ export async function getServiceAnomalies(
return [];
}

const { jobs: apmMlJobs } = await ml.anomalyDetectors.jobs('apm');
const apmMlJobs = await getApmAnomalyDetectionJobs(options.setup);
if (apmMlJobs.length === 0) {
return [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for readability's sake, should we set apmMlJobs to [] and return (outside of the try/catch) if apmMlJobs.length === 0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit, I found it more readable to return from the catch but either works.

}
const apmMlJobCategories = apmMlJobs
.map((job) => getApmMlJobCategory(job, serviceNames))
.filter(
Expand Down