Skip to content

Commit

Permalink
PMM-12382 Match custom labels based on prometheus model
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkubinec committed Aug 2, 2023
1 parent 78728b9 commit 2460d11
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions public/app/percona/shared/helpers/validators.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ 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 contains dash', () => {
const testString = 'key-:value';

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

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';
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) && !!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

0 comments on commit 2460d11

Please sign in to comment.