@@ -125,6 +125,93 @@ func testDeleteLabelValues(t *testing.T, vec *GaugeVec) {
125
125
}
126
126
}
127
127
128
+ func TestDeletePartialMatch (t * testing.T ) {
129
+ baseVec := NewGaugeVec (
130
+ GaugeOpts {
131
+ Name : "test" ,
132
+ Help : "helpless" ,
133
+ },
134
+ []string {"l1" , "l2" , "l3" },
135
+ )
136
+
137
+ assertNoMetric := func (t * testing.T ) {
138
+ if n := len (baseVec .metricMap .metrics ); n != 0 {
139
+ t .Error ("expected no metrics, got" , n )
140
+ }
141
+ }
142
+
143
+ // No metric value is set.
144
+ if got , want := baseVec .DeletePartialMatch (Labels {"l1" : "v1" , "l2" : "v2" }), 0 ; got != want {
145
+ t .Errorf ("got %v, want %v" , got , want )
146
+ }
147
+
148
+ baseVec .With (Labels {"l1" : "baseValue1" , "l2" : "baseValue2" , "l3" : "baseValue3" }).Inc ()
149
+ baseVec .With (Labels {"l1" : "multiDeleteV1" , "l2" : "diff1BaseValue2" , "l3" : "v3" }).(Gauge ).Set (42 )
150
+ baseVec .With (Labels {"l1" : "multiDeleteV1" , "l2" : "diff2BaseValue2" , "l3" : "v3" }).(Gauge ).Set (84 )
151
+ baseVec .With (Labels {"l1" : "multiDeleteV1" , "l2" : "diff3BaseValue2" , "l3" : "v3" }).(Gauge ).Set (168 )
152
+
153
+ curriedVec := baseVec .MustCurryWith (Labels {"l2" : "curriedValue2" })
154
+ curriedVec .WithLabelValues ("curriedValue1" , "curriedValue3" ).Inc ()
155
+ curriedVec .WithLabelValues ("curriedValue1" , "differentCurriedValue3" ).Inc ()
156
+ curriedVec .WithLabelValues ("differentCurriedValue1" , "differentCurriedValue3" ).Inc ()
157
+
158
+ // Try to delete nonexistent label with existent value from curried vector.
159
+ if got , want := curriedVec .DeletePartialMatch (Labels {"lx" : "curriedValue1" }), 0 ; got != want {
160
+ t .Errorf ("got %v, want %v" , got , want )
161
+ }
162
+
163
+ // Try to delete valid label with nonexistent value from curried vector.
164
+ if got , want := curriedVec .DeletePartialMatch (Labels {"l1" : "badValue1" }), 0 ; got != want {
165
+ t .Errorf ("got %v, want %v" , got , want )
166
+ }
167
+
168
+ // Try to delete from a curried vector based on labels which were curried.
169
+ // This operation succeeds when run against the base vector below.
170
+ if got , want := curriedVec .DeletePartialMatch (Labels {"l2" : "curriedValue2" }), 0 ; got != want {
171
+ t .Errorf ("got %v, want %v" , got , want )
172
+ }
173
+
174
+ // Try to delete from a curried vector based on labels which were curried,
175
+ // but the value actually exists in the base vector.
176
+ if got , want := curriedVec .DeletePartialMatch (Labels {"l2" : "baseValue2" }), 0 ; got != want {
177
+ t .Errorf ("got %v, want %v" , got , want )
178
+ }
179
+
180
+ // Delete multiple matching metrics from a curried vector based on partial values.
181
+ if got , want := curriedVec .DeletePartialMatch (Labels {"l1" : "curriedValue1" }), 2 ; got != want {
182
+ t .Errorf ("got %v, want %v" , got , want )
183
+ }
184
+
185
+ // Try to delete nonexistent label with existent value from base vector.
186
+ if got , want := baseVec .DeletePartialMatch (Labels {"lx" : "curriedValue1" }), 0 ; got != want {
187
+ t .Errorf ("got %v, want %v" , got , want )
188
+ }
189
+
190
+ // Try to delete partially invalid labels from base vector.
191
+ if got , want := baseVec .DeletePartialMatch (Labels {"l1" : "baseValue1" , "l2" : "badValue2" }), 0 ; got != want {
192
+ t .Errorf ("got %v, want %v" , got , want )
193
+ }
194
+
195
+ // Delete from the base vector based on values which were curried.
196
+ // This operation fails when run against a curried vector above.
197
+ if got , want := baseVec .DeletePartialMatch (Labels {"l2" : "curriedValue2" }), 1 ; got != want {
198
+ t .Errorf ("got %v, want %v" , got , want )
199
+ }
200
+
201
+ // Delete multiple metrics from the base vector based on a single valid label.
202
+ if got , want := baseVec .DeletePartialMatch (Labels {"l1" : "multiDeleteV1" }), 3 ; got != want {
203
+ t .Errorf ("got %v, want %v" , got , want )
204
+ }
205
+
206
+ // Delete from the base vector based on values which were not curried.
207
+ if got , want := baseVec .DeletePartialMatch (Labels {"l3" : "baseValue3" }), 1 ; got != want {
208
+ t .Errorf ("got %v, want %v" , got , want )
209
+ }
210
+
211
+ // All metrics should have been deleted now.
212
+ assertNoMetric (t )
213
+ }
214
+
128
215
func TestMetricVec (t * testing.T ) {
129
216
vec := NewGaugeVec (
130
217
GaugeOpts {
0 commit comments