-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Fix validation for duration vars on APM configuration settings #124336
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
Merged
MiriamAparicio
merged 2 commits into
elastic:main
from
MiriamAparicio:fix-validation-warning-apm-integration-editor
Feb 3, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
.../fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
| }); | ||
| }); |
35 changes: 35 additions & 0 deletions
35
...tions/fleet/sections/agent_policy/edit_package_policy_page/utils/fix_apm_duration_vars.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MiriamAparicio if you make 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; | ||
| }) | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ | |
| */ | ||
|
|
||
| export * from './has_upgrade_available'; | ||
| export { fixApmDurationVars } from './fix_apm_duration_vars'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?