Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you add unit tests for this logic?

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
) {
Comment on lines +19 to +25
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@MiriamAparicio if you make DURATION_APM_SETTINGS_VARS an array you can simplify this to:

if (DURATION_APM_SETTINGS_VARS.includes(entry[0])) {

// 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;
})
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export * from './has_upgrade_available';
export { fixApmDurationVars } from './fix_apm_duration_vars';
8 changes: 8 additions & 0 deletions x-pack/plugins/fleet/public/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};