@@ -31,6 +31,7 @@ import (
31
31
"github.com/cespare/xxhash/v2"
32
32
dto "github.com/prometheus/client_model/go"
33
33
"github.com/prometheus/common/expfmt"
34
+ "github.com/prometheus/common/model"
34
35
"google.golang.org/protobuf/proto"
35
36
)
36
37
@@ -62,14 +63,27 @@ func init() {
62
63
MustRegister (NewGoCollector ())
63
64
}
64
65
66
+ type RegistryOption func (* Registry )
67
+
68
+ func WithNameValidationScheme (scheme model.ValidationScheme ) RegistryOption {
69
+ return func (r * Registry ) {
70
+ r .nameValidationScheme = scheme
71
+ }
72
+ }
73
+
65
74
// NewRegistry creates a new vanilla Registry without any Collectors
66
75
// pre-registered.
67
- func NewRegistry () * Registry {
68
- return & Registry {
69
- collectorsByID : map [uint64 ]Collector {},
70
- descIDs : map [uint64 ]struct {}{},
71
- dimHashesByName : map [string ]uint64 {},
76
+ func NewRegistry (opts ... RegistryOption ) * Registry {
77
+ reg := & Registry {
78
+ collectorsByID : map [uint64 ]Collector {},
79
+ descIDs : map [uint64 ]struct {}{},
80
+ dimHashesByName : map [string ]uint64 {},
81
+ nameValidationScheme : model .UTF8Validation ,
82
+ }
83
+ for _ , opt := range opts {
84
+ opt (reg )
72
85
}
86
+ return reg
73
87
}
74
88
75
89
// NewPedanticRegistry returns a registry that checks during collection if each
@@ -264,6 +278,7 @@ type Registry struct {
264
278
dimHashesByName map [string ]uint64
265
279
uncheckedCollectors []Collector
266
280
pedanticChecksEnabled bool
281
+ nameValidationScheme model.ValidationScheme
267
282
}
268
283
269
284
// Register implements Registerer.
@@ -503,6 +518,7 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
503
518
metric , metricFamiliesByName ,
504
519
metricHashes ,
505
520
registeredDescIDs ,
521
+ r .nameValidationScheme ,
506
522
))
507
523
case metric , ok := <- umc :
508
524
if ! ok {
@@ -513,6 +529,7 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
513
529
metric , metricFamiliesByName ,
514
530
metricHashes ,
515
531
nil ,
532
+ r .nameValidationScheme ,
516
533
))
517
534
default :
518
535
if goroutineBudget <= 0 || len (checkedCollectors )+ len (uncheckedCollectors ) == 0 {
@@ -530,6 +547,7 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
530
547
metric , metricFamiliesByName ,
531
548
metricHashes ,
532
549
registeredDescIDs ,
550
+ r .nameValidationScheme ,
533
551
))
534
552
case metric , ok := <- umc :
535
553
if ! ok {
@@ -540,6 +558,7 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
540
558
metric , metricFamiliesByName ,
541
559
metricHashes ,
542
560
nil ,
561
+ r .nameValidationScheme ,
543
562
))
544
563
}
545
564
break
@@ -622,6 +641,7 @@ func processMetric(
622
641
metricFamiliesByName map [string ]* dto.MetricFamily ,
623
642
metricHashes map [uint64 ]struct {},
624
643
registeredDescIDs map [uint64 ]struct {},
644
+ nameValidationScheme model.ValidationScheme ,
625
645
) error {
626
646
desc := metric .Desc ()
627
647
// Wrapped metrics collected by an unchecked Collector can have an
@@ -705,7 +725,7 @@ func processMetric(
705
725
}
706
726
metricFamiliesByName [desc .fqName ] = metricFamily
707
727
}
708
- if err := checkMetricConsistency (metricFamily , dtoMetric , metricHashes ); err != nil {
728
+ if err := checkMetricConsistency (metricFamily , dtoMetric , metricHashes , nameValidationScheme ); err != nil {
709
729
return err
710
730
}
711
731
if registeredDescIDs != nil {
@@ -791,7 +811,8 @@ func (gs Gatherers) Gather() ([]*dto.MetricFamily, error) {
791
811
metricFamiliesByName [mf .GetName ()] = existingMF
792
812
}
793
813
for _ , m := range mf .Metric {
794
- if err := checkMetricConsistency (existingMF , m , metricHashes ); err != nil {
814
+ // TODO(juliusmh): hardcoded UTF8 validation
815
+ if err := checkMetricConsistency (existingMF , m , metricHashes , model .UTF8Validation ); err != nil {
795
816
errs = append (errs , err )
796
817
continue
797
818
}
@@ -870,6 +891,7 @@ func checkMetricConsistency(
870
891
metricFamily * dto.MetricFamily ,
871
892
dtoMetric * dto.Metric ,
872
893
metricHashes map [uint64 ]struct {},
894
+ nameValidationScheme model.ValidationScheme ,
873
895
) error {
874
896
name := metricFamily .GetName ()
875
897
@@ -894,7 +916,7 @@ func checkMetricConsistency(
894
916
name , dtoMetric , labelName ,
895
917
)
896
918
}
897
- if ! checkLabelName (labelName ) {
919
+ if ! checkLabelName (labelName , nameValidationScheme ) {
898
920
return fmt .Errorf (
899
921
"collected metric %q { %s} has a label with an invalid name: %s" ,
900
922
name , dtoMetric , labelName ,
0 commit comments