Skip to content

Commit 8c63c28

Browse files
committed
Merge pull request #93 from prometheus/fix-cow-set
Fix pointer receiver in COWMetric.Set().
2 parents e5098ac + a68f44f commit 8c63c28

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

model/metric.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type COWMetric struct {
101101

102102
// Set sets a label name in the wrapped Metric to a given value and copies the
103103
// Metric initially, if it is not already a copy.
104-
func (m COWMetric) Set(ln LabelName, lv LabelValue) {
104+
func (m *COWMetric) Set(ln LabelName, lv LabelValue) {
105105
m.doCOW()
106106
m.Metric[ln] = lv
107107
}

model/metric_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,52 @@ func BenchmarkMetric(b *testing.B) {
7070
testMetric(b)
7171
}
7272
}
73+
74+
func TestCOWMetric(t *testing.T) {
75+
testMetric := Metric{
76+
"to_delete": "test1",
77+
"to_change": "test2",
78+
}
79+
80+
scenarios := []struct {
81+
fn func(*COWMetric)
82+
out Metric
83+
}{
84+
{
85+
fn: func(cm *COWMetric) {
86+
cm.Delete("to_delete")
87+
},
88+
out: Metric{
89+
"to_change": "test2",
90+
},
91+
},
92+
{
93+
fn: func(cm *COWMetric) {
94+
cm.Set("to_change", "changed")
95+
},
96+
out: Metric{
97+
"to_delete": "test1",
98+
"to_change": "changed",
99+
},
100+
},
101+
}
102+
103+
for i, s := range scenarios {
104+
orig := testMetric.Clone()
105+
cm := &COWMetric{
106+
Metric: orig,
107+
}
108+
109+
s.fn(cm)
110+
111+
// Test that the original metric was not modified.
112+
if !orig.Equal(testMetric) {
113+
t.Fatalf("%d. original metric changed; expected %v, got %v", i, testMetric, orig)
114+
}
115+
116+
// Test that the new metric has the right changes.
117+
if !cm.Metric.Equal(s.out) {
118+
t.Fatalf("%d. copied metric doesn't contain expected changes; expected %v, got %v", i, s.out, cm.Metric)
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)