diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_policy_form/settings_definition/apm_settings.ts b/x-pack/plugins/apm/public/components/fleet_integration/apm_policy_form/settings_definition/apm_settings.ts index ec9f740932376..8adda7b5983f1 100644 --- a/x-pack/plugins/apm/public/components/fleet_integration/apm_policy_form/settings_definition/apm_settings.ts +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_policy_form/settings_definition/apm_settings.ts @@ -78,7 +78,7 @@ export function getApmSettings({ }, { key: 'idle_timeout', - type: 'text', + type: 'duration', labelAppend: OPTIONAL_LABEL, label: i18n.translate( 'xpack.apm.fleet_integration.settings.apm.idleTimeoutLabel', @@ -91,7 +91,7 @@ export function getApmSettings({ }, { key: 'read_timeout', - type: 'text', + type: 'duration', labelAppend: OPTIONAL_LABEL, label: i18n.translate( 'xpack.apm.fleet_integration.settings.apm.readTimeoutLabel', @@ -101,7 +101,7 @@ export function getApmSettings({ }, { key: 'shutdown_timeout', - type: 'text', + type: 'duration', labelAppend: OPTIONAL_LABEL, label: i18n.translate( 'xpack.apm.fleet_integration.settings.apm.shutdownTimeoutLabel', @@ -114,7 +114,7 @@ export function getApmSettings({ }, { key: 'write_timeout', - type: 'text', + type: 'duration', labelAppend: OPTIONAL_LABEL, label: i18n.translate( 'xpack.apm.fleet_integration.settings.apm.writeTimeoutLabel', diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx index 4bce393aebb14..8151215c3cced 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx @@ -64,10 +64,9 @@ import type { } from '../../../../../../common/types/rest_spec'; import type { PackagePolicyEditExtensionComponentProps } from '../../../types'; import { pkgKeyFromPackageInfo, storedPackagePoliciesToAgentInputs } from '../../../services'; - import { EuiButtonWithTooltip } from '../../../../integrations/sections/epm/screens/detail'; -import { hasUpgradeAvailable } from './utils'; +import { fixApmDurationVars, hasUpgradeAvailable } from './utils'; export const EditPackagePolicyPage = memo(() => { const { @@ -232,6 +231,10 @@ export const EditPackagePolicyForm = memo<{ ...basePolicyInputVars, }; } + // Fix duration vars, if it's a migrated setting, and it's a plain old number with no suffix + if (basePackage.name === 'apm') { + newVars = fixApmDurationVars(newVars); + } return { ...restOfInput, streams: streams.map((stream: any) => { diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.test.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.test.ts new file mode 100644 index 0000000000000..ab2a8238e7b37 --- /dev/null +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.test.ts @@ -0,0 +1,50 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { fixApmDurationVars } from './fix_apm_duration_vars'; + +describe('Edit Package Policy - fixApmDurationVars()', () => { + const mockApmVars = { + idle_timeout: { + type: 'text', + value: '45', + }, + read_timeout: { + type: 'text', + value: '3600', + }, + shutdown_timeout: { + type: 'text', + value: '30s', + }, + max_header_bytes: { + type: 'integer', + value: 3, + }, + url: { + type: 'text', + value: 'http://localhost:8200', + }, + }; + describe('when the APM var is a duration var', () => { + it('adds duration unit suffix to APM duration vars ', () => { + const newVars = fixApmDurationVars(mockApmVars); + expect(newVars.idle_timeout.value).toEqual('45s'); + }); + it('doesnt add the suffix if doesnt end with number', () => { + const newVars = fixApmDurationVars(mockApmVars); + expect(newVars.shutdown_timeout.value).toEqual(mockApmVars.shutdown_timeout.value); + }); + }); + describe('when the APM is not a duration var', () => { + it('keeps the same value', () => { + const newVars = fixApmDurationVars(mockApmVars); + expect(newVars.url.value).toEqual(mockApmVars.url.value); + expect(newVars.max_header_bytes.value).toEqual(mockApmVars.max_header_bytes.value); + }); + }); +}); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.ts new file mode 100644 index 0000000000000..bea92f35f52cc --- /dev/null +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.ts @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { PackagePolicyConfigRecord } from '../../../../../integrations/types'; +import { DURATION_APM_SETTINGS_VARS } from '../../../../constants'; + +// Fix duration vars, if it's a migrated setting, and it's a plain old number with no suffix, we should assume seconds +export function fixApmDurationVars(vars: PackagePolicyConfigRecord) { + const { IDLE_TIMEOUT, READ_TIMEOUT, SHUTDOWN_TIMEOUT, TAIL_SAMPLING_INTERVAL, WRITE_TIMEOUT } = + DURATION_APM_SETTINGS_VARS; + // convert vars to array, map each key/value pair into another pair + // and then fromEntries gives back the object + return Object.fromEntries( + Object.entries(vars).map((entry: any) => { + if ( + entry[0] === IDLE_TIMEOUT || + entry[0] === READ_TIMEOUT || + entry[0] === SHUTDOWN_TIMEOUT || + entry[0] === TAIL_SAMPLING_INTERVAL || + entry[0] === WRITE_TIMEOUT + ) { + // we add the unit seconds sufix as default + if (/[0-9]+$/.test(entry[1].value)) { + entry[1] = { ...entry[1], value: entry[1].value + 's' }; + return [entry[0], entry[1]]; + } + } + return entry; + }) + ); +} diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/index.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/index.ts index e8206e9dbbf97..be01b9834a7dc 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/index.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/utils/index.ts @@ -6,3 +6,4 @@ */ export * from './has_upgrade_available'; +export { fixApmDurationVars } from './fix_apm_duration_vars'; diff --git a/x-pack/plugins/fleet/public/constants/index.ts b/x-pack/plugins/fleet/public/constants/index.ts index dddd7552f0151..6fa1a4161a066 100644 --- a/x-pack/plugins/fleet/public/constants/index.ts +++ b/x-pack/plugins/fleet/public/constants/index.ts @@ -29,3 +29,11 @@ export * from './page_paths'; export const INDEX_NAME = '.kibana'; export const CUSTOM_LOGS_INTEGRATION_NAME = 'log'; + +export const DURATION_APM_SETTINGS_VARS = { + IDLE_TIMEOUT: 'idle_timeout', + READ_TIMEOUT: 'read_timeout', + SHUTDOWN_TIMEOUT: 'shutdown_timeout', + TAIL_SAMPLING_INTERVAL: 'tail_sampling_interval', + WRITE_TIMEOUT: 'write_timeout', +};