-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Support any UTF-8 string as label name #3321
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
Changes from all commits
95d8aa9
56dc89d
29e0703
4c01b12
7ae44f1
ecaf75e
d19c81a
2bc49b5
ccc3af9
51b5f25
48c7d4e
024a9f0
a3eddd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright 2017 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package labels | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "unicode" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/prometheus/common/model" | ||
| ) | ||
|
|
||
| // IsValidName validates that model.LabelName is not a whitespace string and contains only valid UTF-8 symbols | ||
| func IsValidName(ln model.LabelName) bool { | ||
| lns := string(ln) | ||
| allSpaces := true | ||
| for _, i := range lns { | ||
| if !unicode.IsSpace(i) { | ||
| allSpaces = false | ||
| break | ||
| } | ||
| } | ||
| return !allSpaces && utf8.ValidString(lns) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is used in many other places to validate incoming labels: API calls, config parsing\validation. |
||
| } | ||
|
|
||
| // IsValidSet validates that model.LabelSet keys and values are are valid | ||
| func IsValidSet(ls model.LabelSet) error { | ||
| for ln, lv := range ls { | ||
| if !IsValidName(ln) { | ||
| return fmt.Errorf("invalid name %q", ln) | ||
| } | ||
| if !lv.IsValid() { | ||
| return fmt.Errorf("invalid value %q", lv) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright 2017 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package labels | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| "unicode" | ||
|
|
||
| "github.com/prometheus/common/model" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIsValidName(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| labelName model.LabelName | ||
| valid bool | ||
| }{ | ||
| { | ||
| name: "invalid: empty string", | ||
| labelName: "", | ||
| valid: false, | ||
| }, | ||
| { | ||
| name: "invalid: all spaces", | ||
| labelName: " ", | ||
| valid: false, | ||
| }, | ||
| { | ||
| name: "invalid: only whitespaces", | ||
| labelName: func() model.LabelName { | ||
| whiteSpaceBuilder := strings.Builder{} | ||
| for _, r16 := range unicode.White_Space.R16 { | ||
| for sym := r16.Lo; sym <= r16.Hi; sym += r16.Stride { | ||
| whiteSpaceBuilder.WriteRune(rune(sym)) | ||
| } | ||
| } | ||
| return model.LabelName(whiteSpaceBuilder.String()) | ||
| }(), | ||
| valid: false, | ||
| }, | ||
| { | ||
| name: "valid: Prometheus label", | ||
| labelName: "TEST_label_name_12345", | ||
| valid: true, | ||
| }, | ||
| { | ||
| name: "valid: any UTF-8 character", | ||
| labelName: "\nlabel:test data.爱!🙂\t", | ||
| valid: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| require.Equal(t, testCase.valid, IsValidName(testCase.labelName)) | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,9 @@ func NewMatcher(t MatchType, n, v string) (*Matcher, error) { | |
| } | ||
|
|
||
| func (m *Matcher) String() string { | ||
| if !model.LabelName(m.Name).IsValid() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand what this does, would it be possible to explain it in a comment?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! The idea is to check whether the name is Prometheus-compatible and return it without quotes (same as it does now), and wrap it in double quotes otherwise. |
||
| return fmt.Sprintf(`"%s"%s"%s"`, openMetricsEscape(m.Name), m.Type, openMetricsEscape(m.Value)) | ||
| } | ||
| return fmt.Sprintf(`%s%s"%s"`, m.Name, m.Type, openMetricsEscape(m.Value)) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was moved to a newline, just wanted to check if that was intentional?