Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions prometheus/vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,31 +416,30 @@ func (m *metricMap) deleteByLabels(labels Labels, curry []curriedLabelValue) int
var numDeleted int

for h, metrics := range m.metrics {
i := findMetricWithPartialLabels(m.desc, metrics, labels, curry)
if i >= len(metrics) {
// Didn't find matching labels in this metric slice.
continue
remaining := metrics[:0]
for _, metric := range metrics {
if matchPartialLabels(m.desc, metric.values, labels, curry) {
numDeleted++
continue
}
remaining = append(remaining, metric)
}
// Clear the tail of the backing array so deleted metrics don't
// stay reachable (and hence unable to be garbage collected)
// through the array's spare capacity.
for i := len(remaining); i < len(metrics); i++ {
metrics[i] = metricWithLabelValues{}
}
if len(remaining) == 0 {
delete(m.metrics, h)
} else {
m.metrics[h] = remaining
}
delete(m.metrics, h)
numDeleted++
}

return numDeleted
}

// findMetricWithPartialLabel returns the index of the matching metric or
// len(metrics) if not found.
func findMetricWithPartialLabels(
desc *Desc, metrics []metricWithLabelValues, labels Labels, curry []curriedLabelValue,
) int {
for i, metric := range metrics {
if matchPartialLabels(desc, metric.values, labels, curry) {
return i
}
}
return len(metrics)
}

// indexOf searches the given slice of strings for the target string and returns
// the index or len(items) as well as a boolean whether the search succeeded.
func indexOf(target string, items []string) (int, bool) {
Expand Down
13 changes: 13 additions & 0 deletions prometheus/vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ func TestDeletePartialMatch(t *testing.T) {
testDeletePartialMatch(t, vec)
}

func TestDeletePartialMatchWithCollisions(t *testing.T) {
vec := NewGaugeVec(
GaugeOpts{
Name: "test",
Help: "helpless",
},
[]string{"l1", "l2", "l3"},
)
vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
testDeletePartialMatch(t, vec)
}

func TestDeletePartialMatchWithConstraints(t *testing.T) {
vec := V2.NewGaugeVec(GaugeVecOpts{
GaugeOpts{
Expand Down
Loading