Skip to content

Commit 8a6679d

Browse files
committed
metrics: Add an EditLabel method
1 parent 45216d6 commit 8a6679d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

metrics/metrics.go

+17
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ func (m *M) SetLabel(name string, value interface{}) {
7575
}
7676
}
7777

78+
// EditLabel calls edit with the current value of the specified label.
79+
// The value returned by edit replaces the contents of the label.
80+
// If edit returns nil, the label is removed from the set.
81+
// If the label did not exist, the argument to edit is nil.
82+
func (m *M) EditLabel(name string, edit func(interface{}) interface{}) {
83+
if m != nil {
84+
m.mu.Lock()
85+
defer m.mu.Unlock()
86+
newValue := edit(m.label[name])
87+
if newValue == nil {
88+
delete(m.label, name)
89+
} else {
90+
m.label[name] = newValue
91+
}
92+
}
93+
}
94+
7895
// Snapshot copies an atomic snapshot of the collected metrics into the non-nil
7996
// fields of the provided snapshot value. Only the fields of snap that are not
8097
// nil are snapshotted.

0 commit comments

Comments
 (0)