Skip to content

Commit

Permalink
[APM] Encode spaces when creating ML job (#63683)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar authored Apr 16, 2020
1 parent 6652045 commit 5413cee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export class MachineLearningFlyout extends Component<Props, State> {
};

public addErrorToast = () => {
const core = this.context;
const { core } = this.context;

const { urlParams } = this.props;
const { serviceName } = urlParams;

Expand Down
10 changes: 7 additions & 3 deletions x-pack/legacy/plugins/apm/public/services/rest/ml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
} from '../../../../../../plugins/apm/common/elasticsearch_fieldnames';
import {
getMlJobId,
getMlPrefix
getMlPrefix,
encodeForMlApi
} from '../../../../../../plugins/apm/common/ml_job_constants';
import { callApi } from './callApi';
import { ESFilter } from '../../../../../../plugins/apm/typings/elasticsearch';
Expand Down Expand Up @@ -53,13 +54,16 @@ export async function startMLJob({
http: HttpSetup;
}) {
const transactionIndices = await getTransactionIndices(http);
const groups = ['apm', serviceName.toLowerCase()];
const groups = [
'apm',
encodeForMlApi(serviceName),
encodeForMlApi(transactionType)
];
const filter: ESFilter[] = [
{ term: { [SERVICE_NAME]: serviceName } },
{ term: { [PROCESSOR_EVENT]: 'transaction' } },
{ term: { [TRANSACTION_TYPE]: transactionType } }
];
groups.push(transactionType.toLowerCase());
return callApi<StartedMLJobApiResponse>(http, {
method: 'POST',
pathname: `/api/ml/modules/setup/apm_transaction`,
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/apm/common/ml_job_constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('ml_job_constants', () => {
expect(getMlJobId('myServiceName', 'myTransactionType')).toBe(
'myservicename-mytransactiontype-high_mean_response_time'
);
expect(getMlJobId('my service name')).toBe(
'my_service_name-high_mean_response_time'
);
expect(getMlJobId('my service name', 'my transaction type')).toBe(
'my_service_name-my_transaction_type-high_mean_response_time'
);
});

it('getMlIndex', () => {
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/apm/common/ml_job_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

export function getMlPrefix(serviceName: string, transactionType?: string) {
const maybeTransactionType = transactionType ? `${transactionType}-` : '';
return `${serviceName}-${maybeTransactionType}`.toLowerCase();
return encodeForMlApi(`${serviceName}-${maybeTransactionType}`);
}

export function getMlJobId(serviceName: string, transactionType?: string) {
Expand All @@ -16,3 +16,7 @@ export function getMlJobId(serviceName: string, transactionType?: string) {
export function getMlIndex(serviceName: string, transactionType?: string) {
return `.ml-anomalies-${getMlJobId(serviceName, transactionType)}`;
}

export function encodeForMlApi(value: string) {
return value.replace(/\s+/g, '_').toLowerCase();
}

0 comments on commit 5413cee

Please sign in to comment.