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

PMM-12382 Match custom labels based on prometheus model #681

Merged
merged 5 commits into from
Aug 23, 2023
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
45 changes: 44 additions & 1 deletion public/app/percona/shared/helpers/validators.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('validatePort test', () => {
});

describe('validateKeyValue test', () => {
const errorMessage = 'Values have to be in key:value format, and separated with new line or space';

it('return empty string when key:value is separated by whitespace', () => {
const testString = 'key:value key2:value2';

Expand All @@ -35,8 +37,49 @@ describe('validateKeyValue test', () => {
expect(validators.validateKeyValue(testString)).toEqual(undefined);
});

it('returns empty string if value containts dash', () => {
const testString = 'key:value-';

expect(validators.validateKeyValue(testString)).toEqual(undefined);
});

it('returns empty string if key starts with a dash', () => {
const testString = '_key:value';

expect(validators.validateKeyValue(testString)).toEqual(undefined);
});

it('returns correct error message when key contains dash', () => {
const testString = 'key-:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key starts with two underscores', () => {
const testString = '__key:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key starts with two underscores', () => {
const testString = '__key:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key starts with number', () => {
const testString = '0key:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key contains non ASCII character', () => {
const testString = 'keý:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('return correct error message when value is invalid', () => {
const errorMessage = 'Values have to be in key:value format, and separated with new line or space';
const testString = 'key:value-key2:value2';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
Expand Down
12 changes: 11 additions & 1 deletion public/app/percona/shared/helpers/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ export const validators = {
!value
.split(/[\n\s]/)
.filter(Boolean)
.every((element) => /^[a-z0-9]+:[a-z0-9]+$/.test(element))
.every((element) => {
const [key, value, ...rest] = element.split(':');

// check key against prometheus data model
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key) && !key.startsWith('__') && !!value && !rest.length) {
return true;
}

return false;
})
) {
return 'Values have to be in key:value format, and separated with new line or space';
}
Expand Down
Loading