Skip to content

Commit

Permalink
[APM] Service map fix missing ML health indicators for camelCased ser…
Browse files Browse the repository at this point in the history
…vices (#68979) (#69023)

* Closes #68978 by encoding service names before comparing them to ML job group names.

* Code tweak for readbility

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
ogupte and elasticmachine committed Jun 12, 2020
1 parent fe3086b commit 008eae4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 { getApmMlJobCategory } from './get_service_anomalies';
import { Job as AnomalyDetectionJob } from '../../../../ml/server';

describe('getApmMlJobCategory', () => {
it('should match service names with different casings', () => {
const mlJob = {
job_id: 'testservice-request-high_mean_response_time',
groups: ['apm', 'testservice', 'request'],
} as AnomalyDetectionJob;
const serviceNames = ['testService'];
const apmMlJobCategory = getApmMlJobCategory(mlJob, serviceNames);

expect(apmMlJobCategory).toEqual({
jobId: 'testservice-request-high_mean_response_time',
serviceName: 'testService',
transactionType: 'request',
});
});

it('should match service names with spaces', () => {
const mlJob = {
job_id: 'test_service-request-high_mean_response_time',
groups: ['apm', 'test_service', 'request'],
} as AnomalyDetectionJob;
const serviceNames = ['Test Service'];
const apmMlJobCategory = getApmMlJobCategory(mlJob, serviceNames);

expect(apmMlJobCategory).toEqual({
jobId: 'test_service-request-high_mean_response_time',
serviceName: 'Test Service',
transactionType: 'request',
});
});
});
27 changes: 20 additions & 7 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 @@ -8,27 +8,40 @@ 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 { APM_ML_JOB_GROUP_NAME } from '../../../common/ml_job_constants';
import {
APM_ML_JOB_GROUP_NAME,
encodeForMlApi,
} from '../../../common/ml_job_constants';

type ApmMlJobCategory = NonNullable<ReturnType<typeof getApmMlJobCategory>>;
const getApmMlJobCategory = (
export const getApmMlJobCategory = (
mlJob: AnomalyDetectionJob,
serviceNames: string[]
) => {
const apmJobGroups = mlJob.groups.filter(
(groupName) => groupName !== APM_ML_JOB_GROUP_NAME
const serviceByGroupNameMap = new Map(
serviceNames.map((serviceName) => [
encodeForMlApi(serviceName),
serviceName,
])
);
if (apmJobGroups.length === mlJob.groups.length) {
if (!mlJob.groups.includes(APM_ML_JOB_GROUP_NAME)) {
// ML job missing "apm" group name
return;
}
const [serviceName] = intersection(apmJobGroups, serviceNames);
const apmJobGroups = mlJob.groups.filter(
(groupName) => groupName !== APM_ML_JOB_GROUP_NAME
);
const apmJobServiceNames = apmJobGroups.map(
(groupName) => serviceByGroupNameMap.get(groupName) || groupName
);
const [serviceName] = intersection(apmJobServiceNames, serviceNames);
if (!serviceName) {
// APM ML job service was not found
return;
}
const serviceGroupName = encodeForMlApi(serviceName);
const [transactionType] = apmJobGroups.filter(
(groupName) => groupName !== serviceName
(groupName) => groupName !== serviceGroupName
);
if (!transactionType) {
// APM ML job transaction type was not found.
Expand Down

0 comments on commit 008eae4

Please sign in to comment.