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

Check validity of label values #335

Merged
merged 9 commits into from
Aug 25, 2017

Conversation

m3co-code
Copy link
Contributor

This PR fixes #274.

@beorn7 I was not sure whether I should cover all similar implementation with individual unit tests. Maybe have a look first and tell me if you think we need more tests or on different places.

In regards to the Gathering part, at the moment an invalid label value will make the whole Gather run fail. Might it be better to be not so restrictive and only remove the invalid metrics or should it be this strict in your opinion?

@beorn7
Copy link
Member

beorn7 commented Aug 22, 2017

Looks good at a first glance. Will do a more detailed review ASAP.

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot. My comments are mostly in the realm of nit-picking.

Note that the gathering doesn't stop completely if an invalid label value is encountered. You did the right thing, perhaps without noticing.


for _, val := range vals {
if !utf8.ValidString(val) {
return errors.New(fmt.Sprintf("label value %#v is not valid utf8", val))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golint says: "should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think %q is a bit more natural in the format string (has the same effect as %#v for strings, IIRC, but I had to think about it, while %q makes immediate sense to me).

Nit: It's called UTF-8.


for name, val := range labels {
if !utf8.ValidString(val) {
return errors.New(fmt.Sprintf("label %s: %#v is not valid utf8", name, val))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golint says: "should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put in "value" somewhere here, e.g. "label %s: value %q is not valid UTF-8"

return nil
}

func validateLabels(labels Labels, expectedNumberOfValues int) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function is also here, let's not call the file label_values.go but just labels.go. And now that you have created this file, how about moving the errInconsistentCardinality here, too? Perhaps even move checkLabelNames, reservedLabelPrefix, and the Labels type here...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, perhaps this function should be called validateValuesInLabels or something to make clear it's only looking at the values, not at the names. (Which also makes me think there should be perhaps a doc comment next to the call to this function that explains how the label names are checked implicitly, e.g. line 231 in vec.go.)

@@ -23,6 +23,8 @@ import (

"github.com/golang/protobuf/proto"

"unicode/utf8"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard library imports should go into the first block.

@m3co-code
Copy link
Contributor Author

Thanks for the review! Will come back to this PR soonish and refine it :)

@m3co-code
Copy link
Contributor Author

Ok, feedback is implemented.

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there. You just got the renaming confused.

return nil
}

func validateValuesInLabels(vals []string, expectedNumberOfValues int) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You confused the two functions here. This was called validateLabelValues, which was a fine name. validateValuesInLabels should be the new name of validateLabels above.


var errInconsistentCardinality = errors.New("inconsistent label cardinality")

func validateLabels(labels Labels, expectedNumberOfValues int) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be called validateValuesInLabels. See next comment below.

@m3co-code
Copy link
Contributor Author

Ah gotcha, thanks for the notice!

@beorn7
Copy link
Member

beorn7 commented Aug 25, 2017

Cool. Thank you. Merging now…

@beorn7 beorn7 merged commit 3eb912b into prometheus:master Aug 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check validity of label values.
2 participants