mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
Fixes #11451 A refactor of the Agent API passes metrics as protobufs, which include pointers to label name/value pairs. The aggregator tested for sameness by doing a shallow compare of label values, which for different stats reports would compare unequal because the pointers would be different. This fix does a deep compare. While testing I also noted that we neglect to compare template names. This is unlikely to have caused any issue in practice, since the combination of username/workspace is unique, but in the context of comparing metric labels we should do the comparison. If a user creates a workspace, deletes it, then recreates from a different template, we could in principle have reported incorrect stats for the old template.
27 lines
541 B
Go
27 lines
541 B
Go
package proto
|
|
|
|
func LabelsEqual(a, b []*Stats_Metric_Label) bool {
|
|
am := make(map[string]string, len(a))
|
|
for _, lbl := range a {
|
|
v := lbl.GetValue()
|
|
if v == "" {
|
|
// Prometheus considers empty labels as equivalent to being absent
|
|
continue
|
|
}
|
|
am[lbl.GetName()] = lbl.GetValue()
|
|
}
|
|
lenB := 0
|
|
for _, lbl := range b {
|
|
v := lbl.GetValue()
|
|
if v == "" {
|
|
// Prometheus considers empty labels as equivalent to being absent
|
|
continue
|
|
}
|
|
lenB++
|
|
if am[lbl.GetName()] != v {
|
|
return false
|
|
}
|
|
}
|
|
return len(am) == lenB
|
|
}
|