@@ -229,26 +229,18 @@ func (cmm *csiMetricsManager) RecordMetrics(
229
229
operationName string ,
230
230
operationErr error ,
231
231
operationDuration time.Duration ) {
232
- cmm .recordMetricsWithLabels (operationName , operationErr , operationDuration )
232
+ cmm .recordMetricsWithLabels (operationName , operationErr , operationDuration , nil )
233
233
}
234
234
235
235
// recordMetricsWithLabels is the internal implementation of RecordMetrics.
236
236
func (cmm * csiMetricsManager ) recordMetricsWithLabels (
237
237
operationName string ,
238
238
operationErr error ,
239
239
operationDuration time.Duration ,
240
- labelValues ... string ) {
240
+ labelValues map [ string ] string ) {
241
241
values := []string {cmm .driverName , operationName , getErrorCode (operationErr )}
242
- toAdd := len (labelValues )
243
- if toAdd > len (cmm .additionalLabelNames ) {
244
- // To many labels?! Truncate. Shouldn't happen because of
245
- // error checking in WithLabelValues.
246
- toAdd = len (cmm .additionalLabelNames )
247
- }
248
- values = append (values , labelValues [0 :toAdd ]... )
249
- for i := toAdd ; i < len (cmm .additionalLabelNames ); i ++ {
250
- // Backfill missing values with empty string.
251
- values = append (values , "" )
242
+ for _ , name := range cmm .additionalLabelNames {
243
+ values = append (values , labelValues [name ])
252
244
}
253
245
for _ , label := range cmm .additionalLabels {
254
246
values = append (values , label .value )
@@ -260,34 +252,42 @@ type csiMetricsManagerWithValues struct {
260
252
* csiMetricsManager
261
253
262
254
// additionalValues holds the values passed via WithLabelValues.
263
- additionalValues [ ]string
255
+ additionalValues map [ string ]string
264
256
}
265
257
266
258
// WithLabelValues in the base metrics manager creates a fresh wrapper with no labels and let's
267
259
// that deal with adding the label values.
268
260
func (cmm * csiMetricsManager ) WithLabelValues (labels map [string ]string ) (CSIMetricsManager , error ) {
269
- cmmv := & csiMetricsManagerWithValues {csiMetricsManager : cmm }
261
+ cmmv := & csiMetricsManagerWithValues {
262
+ csiMetricsManager : cmm ,
263
+ additionalValues : map [string ]string {},
264
+ }
270
265
return cmmv .WithLabelValues (labels )
271
266
}
272
267
273
268
// WithLabelValues in the wrapper creates a wrapper which has all existing labels and
274
- // adds the new ones, with error checking.
269
+ // adds the new ones, with error checking. Can be called multiple times. Each call then
270
+ // can add some new value(s). It is an error to overwrite an already set value.
271
+ // If RecordMetrics is called before setting all additional values, the missing ones will
272
+ // be empty.
275
273
func (cmmv * csiMetricsManagerWithValues ) WithLabelValues (labels map [string ]string ) (CSIMetricsManager , error ) {
276
- extended := & csiMetricsManagerWithValues {cmmv .csiMetricsManager , append ([]string {}, cmmv .additionalValues ... )}
277
-
278
- for name := range labels {
279
- if ! cmmv .haveAdditionalLabel (name ) {
274
+ extended := & csiMetricsManagerWithValues {
275
+ csiMetricsManager : cmmv .csiMetricsManager ,
276
+ additionalValues : map [string ]string {},
277
+ }
278
+ // We need to copy the old values to avoid modifying the map in cmmv.
279
+ for name , value := range cmmv .additionalValues {
280
+ extended .additionalValues [name ] = value
281
+ }
282
+ // Now add all new values.
283
+ for name , value := range labels {
284
+ if ! extended .haveAdditionalLabel (name ) {
280
285
return nil , fmt .Errorf ("label %q was not defined via WithLabelNames" , name )
281
286
}
282
- }
283
- // Add in same order as in the label definition.
284
- for _ , name := range cmmv .additionalLabelNames {
285
- if value , ok := labels [name ]; ok {
286
- if len (cmmv .additionalValues ) >= len (extended .additionalLabelNames ) {
287
- return nil , fmt .Errorf ("label %q = %q cannot be added, all labels already have values %v" , name , value , cmmv .additionalValues )
288
- }
289
- extended .additionalValues = append (extended .additionalValues , value )
287
+ if v , ok := extended .additionalValues [name ]; ok {
288
+ return nil , fmt .Errorf ("label %q already has value %q" , name , v )
290
289
}
290
+ extended .additionalValues [name ] = value
291
291
}
292
292
return extended , nil
293
293
}
@@ -306,7 +306,7 @@ func (cmmv *csiMetricsManagerWithValues) RecordMetrics(
306
306
operationName string ,
307
307
operationErr error ,
308
308
operationDuration time.Duration ) {
309
- cmmv .recordMetricsWithLabels (operationName , operationErr , operationDuration , cmmv .additionalValues ... )
309
+ cmmv .recordMetricsWithLabels (operationName , operationErr , operationDuration , cmmv .additionalValues )
310
310
}
311
311
312
312
// SetDriverName is called to update the CSI driver name. This should be done
0 commit comments