@@ -43,6 +43,13 @@ func SetupGlobalPromauto(factoryArg ...promauto.Factory) {
43
43
}
44
44
}
45
45
46
+ // CustomLabelsProvider is an interface that allows to convert anything to a prometheus.Labels
47
+ // It allows to provide your own FAST implementation of Struct->prometheus.Labels conversion
48
+ // without using reflection.
49
+ type CustomLabelsProvider interface {
50
+ ToPrometheusLabels () prometheus.Labels
51
+ }
52
+
46
53
// promsafeTag is the tag name used for promsafe labels inside structs.
47
54
// The tag is optional, as if not present, field is used with snake_cased FieldName.
48
55
// It's useful to use a tag when you want to override the default naming or exclude a field from the metric.
@@ -273,6 +280,10 @@ func extractLabelsWithValues(labelProvider labelsProviderMarker) prometheus.Labe
273
280
return nil
274
281
}
275
282
283
+ if clp , ok := labelProvider .(CustomLabelsProvider ); ok {
284
+ return clp .ToPrometheusLabels ()
285
+ }
286
+
276
287
// TODO: let's handle defaults as well, why not?
277
288
278
289
// Here, then, it can be only a struct, that is a parent of StructLabelProvider
@@ -291,13 +302,20 @@ func extractLabelValues(labelProvider labelsProviderMarker) []string {
291
302
}
292
303
293
304
// extractLabelNames extracts labels names from a given labelsProviderMarker (parent instance of aStructLabelProvider)
305
+ // Deprecated: refactor is required. Order of result slice is not guaranteed.
294
306
func extractLabelNames (labelProvider labelsProviderMarker ) []string {
295
307
if any (labelProvider ) == nil {
296
308
return nil
297
309
}
298
310
311
+ var labels prometheus.Labels
312
+ if clp , ok := labelProvider .(CustomLabelsProvider ); ok {
313
+ labels = clp .ToPrometheusLabels ()
314
+ } else {
315
+ labels = extractLabelFromStruct (labelProvider )
316
+ }
317
+
299
318
// Here, then, it can be only a struct, that is a parent of StructLabelProvider
300
- labels := extractLabelFromStruct (labelProvider )
301
319
labelNames := make ([]string , 0 , len (labels ))
302
320
for k := range labels {
303
321
labelNames = append (labelNames , k )
0 commit comments