Skip to content

Commit 9672791

Browse files
[8.19] [APM][ML] Limit environment name length when creating ML job (#225973) (#226151)
# Backport This will backport the following commits from `main` to `8.19`: - [[APM][ML] Limit environment name length when creating ML job (#225973)](#225973) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Milosz Marcinkowski","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-07-02T10:38:04Z","message":"[APM][ML] Limit environment name length when creating ML job (#225973)\n\nCloses #225627\n\nLimit environment name when creating ML job due to 64 characters\nconstraint. Otherwise, ML job fails when creating from APM settings.","sha":"b289a29ea0e46b8ecc45e9d05e2e65d3e4725c03","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","backport:prev-minor","backport:prev-major","Team:obs-ux-infra_services","v9.2.0"],"title":"[APM][ML] Limit environment name length when creating ML job","number":225973,"url":"https://github.com/elastic/kibana/pull/225973","mergeCommit":{"message":"[APM][ML] Limit environment name length when creating ML job (#225973)\n\nCloses #225627\n\nLimit environment name when creating ML job due to 64 characters\nconstraint. Otherwise, ML job fails when creating from APM settings.","sha":"b289a29ea0e46b8ecc45e9d05e2e65d3e4725c03"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/225973","number":225973,"mergeCommit":{"message":"[APM][ML] Limit environment name length when creating ML job (#225973)\n\nCloses #225627\n\nLimit environment name when creating ML job due to 64 characters\nconstraint. Otherwise, ML job fails when creating from APM settings.","sha":"b289a29ea0e46b8ecc45e9d05e2e65d3e4725c03"}}]}] BACKPORT--> Co-authored-by: Milosz Marcinkowski <[email protected]>
1 parent b1278cc commit 9672791

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

x-pack/solutions/observability/plugins/apm/ftr_e2e/cypress/e2e/settings/anomaly_detection.cy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ const getAbleToModifyCase = () => {
7575
});
7676

7777
it('should show error if api call crashes when modifying settings', () => {
78+
cy.intercept('POST', '/internal/apm/settings/anomaly-detection/jobs', {
79+
statusCode: 500,
80+
});
81+
7882
const { rangeFrom, rangeTo } = timeRange;
7983
const TEST_ENV =
8084
'Synthtrace: case scenario TEST-with-a-really-long-name ' + new Date().toISOString();

x-pack/solutions/observability/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { APM_ML_JOB_GROUP, ML_MODULE_ID_APM_TRANSACTION } from './constants';
2323
import { getAnomalyDetectionJobs } from './get_anomaly_detection_jobs';
2424

2525
const DEFAULT_TIMEOUT = '60s';
26+
const ENV_MAX_LENGTH = 40;
2627

2728
export async function createAnomalyDetectionJobs({
2829
mlClient,
@@ -107,10 +108,11 @@ async function createAnomalyDetectionJob({
107108
}) {
108109
return withApmSpan('create_anomaly_detection_job', async () => {
109110
const randomToken = uuidv4().substr(-4);
111+
const sanitizedEnvironment = snakeCase(environment).slice(0, ENV_MAX_LENGTH); // limit env name due to ML job ID length constraints (up to 64 chars in total)
110112

111113
const anomalyDetectionJob = mlClient.modules.setup({
112114
moduleId: ML_MODULE_ID_APM_TRANSACTION,
113-
prefix: `${APM_ML_JOB_GROUP}-${snakeCase(environment)}-${randomToken}-`,
115+
prefix: `${APM_ML_JOB_GROUP}-${sanitizedEnvironment}-${randomToken}-`,
114116
groups: [APM_ML_JOB_GROUP],
115117
indexPatternName: apmMetricIndex,
116118
applyToAllSpaces: true,

x-pack/solutions/observability/test/apm_api_integration/tests/settings/anomaly_detection/write_user.spec.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export default function apiTest({ getService }: FtrProviderContext) {
5858
});
5959

6060
describe('when calling create endpoint', () => {
61+
beforeEach(async () => {
62+
const res = await getJobs({ user });
63+
const jobIds = res.body.jobs.map((job: any) => job.jobId);
64+
await deleteJobs(jobIds);
65+
});
66+
6167
it('creates two jobs', async () => {
6268
await createJobs(['production', 'staging'], { user });
6369

@@ -69,19 +75,22 @@ export default function apiTest({ getService }: FtrProviderContext) {
6975
});
7076
});
7177

72-
describe('with existing ML jobs', () => {
73-
before(async () => {
74-
await createJobs(['production', 'staging'], { user });
75-
});
76-
it('skips duplicate job creation', async () => {
77-
await createJobs(['production', 'test'], { user });
78+
it('creates job with long environment name', async () => {
79+
const longEnvironmentName =
80+
'Production: This Is A Deliberately Long Environment Name To Test Limits - 1234567890';
81+
const { status } = await createJobs([longEnvironmentName], { user });
82+
expect(status).to.be(200);
83+
});
84+
85+
it('skips duplicate job creation', async () => {
86+
await createJobs(['production', 'staging'], { user });
87+
await createJobs(['production', 'test'], { user });
7888

79-
const { body } = await getJobs({ user });
80-
expect(countBy(body.jobs, 'environment')).to.eql({
81-
production: 1,
82-
staging: 1,
83-
test: 1,
84-
});
89+
const { body } = await getJobs({ user });
90+
expect(countBy(body.jobs, 'environment')).to.eql({
91+
production: 1,
92+
staging: 1,
93+
test: 1,
8594
});
8695
});
8796
});

0 commit comments

Comments
 (0)