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] Service map fix missing ML health indicators for camelCased services #68979

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -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',
});
});
});
21 changes: 17 additions & 4 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 serviceByGroupNameMap = new Map(
serviceNames.map((serviceName) => [
encodeForMlApi(serviceName),
serviceName,
])
);
const apmJobGroups = mlJob.groups.filter(
(groupName) => groupName !== APM_ML_JOB_GROUP_NAME
);
if (apmJobGroups.length === mlJob.groups.length) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this would read better as:

  const hasApmGroup = mlJob.groups.some(
    (groupName) => groupName !== APM_ML_JOB_GROUP_NAME
  );
  if (!hasApmGroup) {
    // ML job missing "apm" group
    return;
  }

// ML job missing "apm" group name
return;
}
const [serviceName] = intersection(apmJobGroups, serviceNames);
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);
Copy link
Member

Choose a reason for hiding this comment

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

nit

Suggested change
const serviceGroupName = encodeForMlApi(serviceName);
const encodedServiceName = encodeForMlApi(serviceName);

const [transactionType] = apmJobGroups.filter(
(groupName) => groupName !== serviceName
(groupName) => groupName !== serviceGroupName
);
if (!transactionType) {
// APM ML job transaction type was not found.
Expand Down