-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 potential race with LabelPairs #511
Changes from all commits
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 |
---|---|---|
|
@@ -125,7 +125,7 @@ func populateMetric( | |
labelPairs []*dto.LabelPair, | ||
m *dto.Metric, | ||
) error { | ||
m.Label = labelPairs | ||
m.Label = copyLabelPairs(labelPairs) | ||
switch t { | ||
case CounterValue: | ||
m.Counter = &dto.Counter{Value: proto.Float64(v)} | ||
|
@@ -160,3 +160,15 @@ func makeLabelPairs(desc *Desc, labelValues []string) []*dto.LabelPair { | |
sort.Sort(labelPairSorter(labelPairs)) | ||
return labelPairs | ||
} | ||
|
||
func copyLabelPairs(source []*dto.LabelPair) []*dto.LabelPair { | ||
labelPairs := make([]*dto.LabelPair, 0, len(source)) | ||
for _, pair := range source { | ||
labelPairs = append(labelPairs, &dto.LabelPair{ | ||
Name: pair.Name, | ||
Value: pair.Value, | ||
}) | ||
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 you need to append here since you created the slice with the correct length already? Why not just assign the index of the 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 was mostly a copy-paste job from the method above, as I was aiming for consistency. I don't think there is any significant difference in the two approaches, but I don't feel strongly in either direction. |
||
} | ||
// shouldn't need sorting, as it should already be sorted | ||
return labelPairs | ||
} |
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.
Do we know the sweet spot that this happens? Or, even better, the underlying reason why more makes this break?
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.
@bobbytables i have not yet identified the exact scenario under which things get wonky. My guess is the sorting of the labelpairs can be racy. This PR is really just an attempt to highlight the issue and hopefully lead to further discovery, etc.
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.
I think the reason is here: https://golang.org/src/sort/sort.go?s=5414:5439#L184
I.e. the sort algorithm changes with more than 12 elements.