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

Fix promlint order algorithm not work issue #6445

Merged
merged 1 commit into from
Dec 11, 2019

Conversation

RainbowMango
Copy link
Contributor

The less function provided to sort.SliceStable not work well. Since we should start to compare Text only if the two Metric are equal.

	// Ensure deterministic output.
	sort.SliceStable(problems, func(i, j int) bool {
		if problems[i].Metric < problems[j].Metric {
			return true
		} 

		return problems[i].Text < problems[j].Text
	})

Signed-off-by: RainbowMango [email protected]

@RainbowMango
Copy link
Contributor Author

/cc @brancz
I happened to see this issue when I try to lint metrics from Kubernetes.

@brancz
Copy link
Member

brancz commented Dec 11, 2019

lgtm

Copy link
Member

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

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

It can be even shorter/cleaner IMO:

	sort.SliceStable(problems, func(i, j int) bool {
		if problems[i].Metric == problems[j].Metric {
			return problems[i].Text < problems[j].Text
		}

		return problems[i].Metric < problems[j].Metric
	})

@brancz brancz merged commit e53f751 into prometheus:master Dec 11, 2019
@RainbowMango
Copy link
Contributor Author

By the way, I don't think we need to define a new type of problems here:

// problems is a slice of Problems with a helper method to easily append
// additional Problems to the slice.
type problems []Problem
// Add appends a new Problem to the slice for the specified metric, with
// the specified issue text.
func (p *problems) Add(mf dto.MetricFamily, text string) {
*p = append(*p, Problem{
Metric: mf.GetName(),
Text: text,
})
}

If people using a Goland IDE, and Goland always complains Receiver 'problems' may be 'nil' in call in place of involving Add() function. This is anti-pattern? How do you think?

problems.Add(mf, "no help text")

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.

3 participants