Skip to content

Commit

Permalink
[APM] Add four decimal places float validation for transaction_sample…
Browse files Browse the repository at this point in the history
…_rate (#188555)

Closes #183840 

## Summary

This PR adds four decimal places float validation for
`transaction_sample_rate` and keeps the three decimal places float
validation for all other fields


![image](https://github.com/user-attachments/assets/359136b8-6a3a-4a51-ab51-085cd03a0371)


## Testing 

- Open APM -> Settings -> Agent Configuration and go to the next time
where `transaction_sample_rate` and check the validation -> Up to 4
decimal places should be allowed:



https://github.com/user-attachments/assets/0c596277-11b2-40d3-991e-d04f97a4ac5f
  • Loading branch information
jennypavlova authored Jul 18, 2024
1 parent 6dade93 commit db5d8db
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 { floatFourDecimalPlacesRt } from './float_four_decimal_places_rt';
import { isRight } from 'fp-ts/lib/Either';

describe('floatFourDecimalPlacesRt', () => {
it('does not accept empty values', () => {
expect(isRight(floatFourDecimalPlacesRt.decode(undefined))).toBe(false);
expect(isRight(floatFourDecimalPlacesRt.decode(null))).toBe(false);
expect(isRight(floatFourDecimalPlacesRt.decode(''))).toBe(false);
});

it('should only accept stringified numbers', () => {
expect(isRight(floatFourDecimalPlacesRt.decode('0.5'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode(0.5))).toBe(false);
});

it('checks if the number falls within 0, 1', () => {
expect(isRight(floatFourDecimalPlacesRt.decode('0'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode('0.5'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode('-0.1'))).toBe(false);
expect(isRight(floatFourDecimalPlacesRt.decode('1.1'))).toBe(false);
expect(isRight(floatFourDecimalPlacesRt.decode(NaN))).toBe(false);
});

it('checks whether the number of decimals is 4', () => {
expect(isRight(floatFourDecimalPlacesRt.decode('1'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode('0.9'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode('0.99'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode('0.999'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode('0.9999'))).toBe(true);
expect(isRight(floatFourDecimalPlacesRt.decode('0.99999'))).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 * as t from 'io-ts';
import { either } from 'fp-ts/lib/Either';

export const floatFourDecimalPlacesRt = new t.Type<string, string, unknown>(
'floatFourDecimalPlacesRt',
t.string.is,
(input, context) => {
return either.chain(t.string.validate(input, context), (inputAsString) => {
const inputAsFloat = parseFloat(inputAsString);
const maxFourDecimals = parseFloat(inputAsFloat.toFixed(4)) === inputAsFloat;

const isValid = inputAsFloat >= 0 && inputAsFloat <= 1 && maxFourDecimals;

return isValid
? t.success(inputAsString)
: t.failure(input, context, 'Must be a number between 0.0000 and 1');
});
},
t.identity
);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { floatThreeDecimalPlacesRt } from './float_three_decimal_places_rt';
import { isRight } from 'fp-ts/lib/Either';

describe('floatThreeDecimalPlacesRt', () => {
it('does not accept empty values', () => {
expect(isRight(floatThreeDecimalPlacesRt.decode(undefined))).toBe(false);
expect(isRight(floatThreeDecimalPlacesRt.decode(null))).toBe(false);
expect(isRight(floatThreeDecimalPlacesRt.decode(''))).toBe(false);
});

it('should only accept stringified numbers', () => {
expect(isRight(floatThreeDecimalPlacesRt.decode('0.5'))).toBe(true);
expect(isRight(floatThreeDecimalPlacesRt.decode(0.5))).toBe(false);
});

it('checks if the number falls within 0, 1', () => {
expect(isRight(floatThreeDecimalPlacesRt.decode('0'))).toBe(true);
expect(isRight(floatThreeDecimalPlacesRt.decode('0.5'))).toBe(true);
expect(isRight(floatThreeDecimalPlacesRt.decode('-0.1'))).toBe(false);
expect(isRight(floatThreeDecimalPlacesRt.decode('1.1'))).toBe(false);
expect(isRight(floatThreeDecimalPlacesRt.decode(NaN))).toBe(false);
});

it('checks whether the number of decimals is 3', () => {
expect(isRight(floatThreeDecimalPlacesRt.decode('1'))).toBe(true);
expect(isRight(floatThreeDecimalPlacesRt.decode('0.9'))).toBe(true);
expect(isRight(floatThreeDecimalPlacesRt.decode('0.99'))).toBe(true);
expect(isRight(floatThreeDecimalPlacesRt.decode('0.999'))).toBe(true);
expect(isRight(floatThreeDecimalPlacesRt.decode('0.9999'))).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import * as t from 'io-ts';
import { either } from 'fp-ts/lib/Either';

export const floatRt = new t.Type<string, string, unknown>(
'floatRt',
export const floatThreeDecimalPlacesRt = new t.Type<string, string, unknown>(
'floatThreeDecimalPlacesRt',
t.string.is,
(input, context) => {
return either.chain(t.string.validate(input, context), (inputAsString) => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { AgentName } from '../../../typings/es_schemas/ui/fields/agent';
import { booleanRt } from '../runtime_types/boolean_rt';
import { getIntegerRt } from '../runtime_types/integer_rt';
import { isRumOrMobileAgentName } from '../../agent_name';
import { floatRt } from '../runtime_types/float_rt';
import { floatThreeDecimalPlacesRt } from '../runtime_types/float_three_decimal_places_rt';
import { floatFourDecimalPlacesRt } from '../runtime_types/float_four_decimal_places_rt';
import { RawSettingDefinition, SettingDefinition } from './types';
import { generalSettings } from './general_settings';
import { javaSettings } from './java_settings';
Expand Down Expand Up @@ -45,8 +46,14 @@ function getSettingDefaults(setting: RawSettingDefinition): SettingDefinition {
}

case 'float': {
if (setting.key === 'transaction_sample_rate') {
return {
validation: floatFourDecimalPlacesRt,
...setting,
};
}
return {
validation: floatRt,
validation: floatThreeDecimalPlacesRt,
...setting,
};
}
Expand Down

0 comments on commit db5d8db

Please sign in to comment.