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 2 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
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
Loading