Skip to content

Commit

Permalink
[APM] Fix service maps load failure when user doesn't have ML permiss…
Browse files Browse the repository at this point in the history
…ions (#70138)

* checks for statusCode 403 when fetching ML jobs, and returns empty list of jobs instead of throwing

* Catches any error thrown when retrieving service anomalies and logs them
to the console before responding service maps data without anomalies.

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
ogupte and elasticmachine authored Jun 30, 2020
1 parent 43a9e0d commit 61a3188
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function getApmAnomalyDetectionJobs(
const { jobs } = await ml.anomalyDetectors.jobs(APM_ML_JOB_GROUP_NAME);
return jobs;
} catch (error) {
if (error.statusCode === 404) {
if (error.statusCode === 404 || error.statusCode === 403) {
return [];
}
throw error;
Expand Down Expand Up @@ -131,7 +131,7 @@ export async function getServiceAnomalies(
};

const response = (await ml.mlSystem.mlSearch(params)) as {
aggregations: {
aggregations?: {
jobs: {
buckets: Array<{
key: string;
Expand All @@ -151,6 +151,11 @@ export async function getServiceAnomalies(
};
};
};

if (!response.aggregations) {
return [];
}

const anomalyScores = response.aggregations.jobs.buckets.map((jobBucket) => {
const jobId = jobBucket.key;
const bucketSource = jobBucket.top_score_hits.hits.hits?.[0]?._source;
Expand Down
15 changes: 12 additions & 3 deletions x-pack/plugins/apm/server/lib/service_map/get_service_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { chunk } from 'lodash';
import { Logger } from 'kibana/server';
import {
AGENT_NAME,
SERVICE_ENVIRONMENT,
Expand All @@ -26,6 +27,7 @@ export interface IEnvOptions {
setup: Setup & SetupTimeRange;
serviceName?: string;
environment?: string;
logger: Logger;
}

async function getConnectionData({
Expand Down Expand Up @@ -138,6 +140,7 @@ export type ServicesResponse = PromiseReturnType<typeof getServicesData>;
export type ServiceMapAPIResponse = PromiseReturnType<typeof getServiceMap>;

export async function getServiceMap(options: IEnvOptions) {
const { logger } = options;
const [connectionData, servicesData] = await Promise.all([
getConnectionData(options),
getServicesData(options),
Expand All @@ -150,12 +153,18 @@ export async function getServiceMap(options: IEnvOptions) {
(serviceData) => serviceData[SERVICE_NAME]
);

// Get related service anomalies
const serviceAnomalies = await getServiceAnomalies(options, serviceNames);
let anomalies: ServiceAnomalies = [];
try {
// Get related service anomalies
anomalies = await getServiceAnomalies(options, serviceNames);
} catch (error) {
logger.warn('Unabled to retrieve anomaly detection data for service maps.');
logger.error(error);
}

return transformServiceMapResponses({
...connectionData,
anomalies: serviceAnomalies,
anomalies,
services: servicesData,
});
}
7 changes: 6 additions & 1 deletion x-pack/plugins/apm/server/routes/service_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export const serviceMapRoute = createRoute(() => ({
const {
query: { serviceName, environment },
} = context.params;
return getServiceMap({ setup, serviceName, environment });
return getServiceMap({
setup,
serviceName,
environment,
logger: context.logger,
});
},
}));

Expand Down

0 comments on commit 61a3188

Please sign in to comment.