Skip to content
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

[APM] Agent span_frames_min_duration configuration input cannot handle 0 or -1 #62777

Merged
merged 2 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -10,7 +10,8 @@ interface AmountAndUnit {
}

export function amountAndUnitToObject(value: string): AmountAndUnit {
const [, amount = '', unit = ''] = value.match(/(\d+)?(\w+)?/) || [];
// matches any postive and negative number and its unit.
const [, amount = '', unit = ''] = value.match(/(^-?\d+)?(\w+)?/) || [];
return { amount, unit };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,56 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { durationRt } from './duration_rt';
import { durationRt, getDurationRt } from './duration_rt';
import { isRight } from 'fp-ts/lib/Either';

describe('durationRt', () => {
describe('it should not accept', () => {
[undefined, null, '', 0, 'foo', true, false, '100', 's', 'm', '0h'].map(
[
undefined,
null,
'',
0,
'foo',
true,
false,
'100',
's',
'm',
'0ms',
'-1ms'
].map(input => {
it(`${JSON.stringify(input)}`, () => {
expect(isRight(durationRt.decode(input))).toBe(false);
});
});
});

describe('it should accept', () => {
['1000ms', '2s', '3m', '1s'].map(input => {
it(`${JSON.stringify(input)}`, () => {
expect(isRight(durationRt.decode(input))).toBe(true);
});
});
});
});

describe('getDurationRt', () => {
const customDurationRt = getDurationRt({ min: -1 });
describe('it should not accept', () => {
[undefined, null, '', 0, 'foo', true, false, '100', 's', 'm', '-2ms'].map(
input => {
it(`${JSON.stringify(input)}`, () => {
expect(isRight(durationRt.decode(input))).toBe(false);
expect(isRight(customDurationRt.decode(input))).toBe(false);
});
}
);
});

describe('It should accept', () => {
['1000ms', '2s', '3m'].map(input => {
describe('it should accept', () => {
['1000ms', '2s', '3m', '1s', '-1s', '0ms'].map(input => {
it(`${JSON.stringify(input)}`, () => {
expect(isRight(durationRt.decode(input))).toBe(true);
expect(isRight(customDurationRt.decode(input))).toBe(true);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ import { amountAndUnitToObject } from '../amount_and_unit';

export const DURATION_UNITS = ['ms', 's', 'm'];

export const durationRt = new t.Type<string, string, unknown>(
'durationRt',
t.string.is,
(input, context) => {
return either.chain(t.string.validate(input, context), inputAsString => {
const { amount, unit } = amountAndUnitToObject(inputAsString);
const amountAsInt = parseInt(amount, 10);
const isValidUnit = DURATION_UNITS.includes(unit);
const isValid = amountAsInt > 0 && isValidUnit;
export function getDurationRt({ min }: { min: number }) {
return new t.Type<string, string, unknown>(
'durationRt',
t.string.is,
(input, context) => {
return either.chain(t.string.validate(input, context), inputAsString => {
const { amount, unit } = amountAndUnitToObject(inputAsString);
const amountAsInt = parseInt(amount, 10);
const isValidUnit = DURATION_UNITS.includes(unit);
const isValid = amountAsInt >= min && isValidUnit;

return isValid
? t.success(inputAsString)
: t.failure(
input,
context,
`Must have numeric amount and a valid unit (${DURATION_UNITS})`
);
});
},
t.identity
);
return isValid
? t.success(inputAsString)
: t.failure(
input,
context,
`Must have numeric amount and a valid unit (${DURATION_UNITS})`
);
});
},
t.identity
);
}

export const durationRt = getDurationRt({ min: 1 });

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 @@ -8,6 +8,7 @@ import { i18n } from '@kbn/i18n';
import { getIntegerRt } from '../runtime_types/integer_rt';
import { captureBodyRt } from '../runtime_types/capture_body_rt';
import { RawSettingDefinition } from './types';
import { getDurationRt } from '../runtime_types/duration_rt';

/*
* Settings added here will show up in the UI and will be validated on the client and server
Expand Down Expand Up @@ -143,6 +144,7 @@ export const generalSettings: RawSettingDefinition[] = [
{
key: 'span_frames_min_duration',
type: 'duration',
validation: getDurationRt({ min: -1 }),
defaultValue: '5ms',
label: i18n.translate('xpack.apm.agentConfig.spanFramesMinDuration.label', {
defaultMessage: 'Span frames minimum duration'
Expand All @@ -154,7 +156,8 @@ export const generalSettings: RawSettingDefinition[] = [
'In its default settings, the APM agent will collect a stack trace with every recorded span.\nWhile this is very helpful to find the exact place in your code that causes the span, collecting this stack trace does have some overhead. \nWhen setting this option to a negative value, like `-1ms`, stack traces will be collected for all spans. Setting it to a positive value, e.g. `5ms`, will limit stack trace collection to spans with durations equal to or longer than the given value, e.g. 5 milliseconds.\n\nTo disable stack trace collection for spans completely, set the value to `0ms`.'
}
),
excludeAgents: ['js-base', 'rum-js', 'nodejs']
excludeAgents: ['js-base', 'rum-js', 'nodejs'],
min: -1
},

// STACK_TRACE_LIMIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ interface BytesSetting extends BaseSetting {
interface DurationSetting extends BaseSetting {
type: 'duration';
units?: string[];
min?: number;
}

export type RawSettingDefinition =
Expand Down