@@ -746,37 +746,120 @@ func BenchmarkHandler(b *testing.B) {
746
746
}
747
747
748
748
func TestAlreadyRegistered (t * testing.T ) {
749
- reg := prometheus .NewRegistry ()
750
749
original := prometheus .NewCounterVec (
750
+ prometheus.CounterOpts {
751
+ Name : "test" ,
752
+ Help : "help" ,
753
+ ConstLabels : prometheus.Labels {"const" : "label" },
754
+ },
755
+ []string {"foo" , "bar" },
756
+ )
757
+ equalButNotSame := prometheus .NewCounterVec (
758
+ prometheus.CounterOpts {
759
+ Name : "test" ,
760
+ Help : "help" ,
761
+ ConstLabels : prometheus.Labels {"const" : "label" },
762
+ },
763
+ []string {"foo" , "bar" },
764
+ )
765
+ originalWithoutConstLabel := prometheus .NewCounterVec (
751
766
prometheus.CounterOpts {
752
767
Name : "test" ,
753
768
Help : "help" ,
754
769
},
755
770
[]string {"foo" , "bar" },
756
771
)
757
- equalButNotSame := prometheus .NewCounterVec (
772
+ equalButNotSameWithoutConstLabel := prometheus .NewCounterVec (
758
773
prometheus.CounterOpts {
759
774
Name : "test" ,
760
775
Help : "help" ,
761
776
},
762
777
[]string {"foo" , "bar" },
763
778
)
764
- var err error
765
- if err = reg .Register (original ); err != nil {
766
- t .Fatal (err )
767
- }
768
- if err = reg .Register (equalButNotSame ); err == nil {
769
- t .Fatal ("expected error when registering equal collector" )
779
+
780
+ scenarios := []struct {
781
+ name string
782
+ originalCollector prometheus.Collector
783
+ registerWith func (prometheus.Registerer ) prometheus.Registerer
784
+ newCollector prometheus.Collector
785
+ reRegisterWith func (prometheus.Registerer ) prometheus.Registerer
786
+ }{
787
+ {
788
+ "RegisterNormallyReregisterNormally" ,
789
+ original ,
790
+ func (r prometheus.Registerer ) prometheus.Registerer { return r },
791
+ equalButNotSame ,
792
+ func (r prometheus.Registerer ) prometheus.Registerer { return r },
793
+ },
794
+ {
795
+ "RegisterNormallyReregisterWrapped" ,
796
+ original ,
797
+ func (r prometheus.Registerer ) prometheus.Registerer { return r },
798
+ equalButNotSameWithoutConstLabel ,
799
+ func (r prometheus.Registerer ) prometheus.Registerer {
800
+ return prometheus .WrapRegistererWith (prometheus.Labels {"const" : "label" }, r )
801
+ },
802
+ },
803
+ {
804
+ "RegisterWrappedReregisterWrapped" ,
805
+ originalWithoutConstLabel ,
806
+ func (r prometheus.Registerer ) prometheus.Registerer {
807
+ return prometheus .WrapRegistererWith (prometheus.Labels {"const" : "label" }, r )
808
+ },
809
+ equalButNotSameWithoutConstLabel ,
810
+ func (r prometheus.Registerer ) prometheus.Registerer {
811
+ return prometheus .WrapRegistererWith (prometheus.Labels {"const" : "label" }, r )
812
+ },
813
+ },
814
+ {
815
+ "RegisterWrappedReregisterNormally" ,
816
+ originalWithoutConstLabel ,
817
+ func (r prometheus.Registerer ) prometheus.Registerer {
818
+ return prometheus .WrapRegistererWith (prometheus.Labels {"const" : "label" }, r )
819
+ },
820
+ equalButNotSame ,
821
+ func (r prometheus.Registerer ) prometheus.Registerer { return r },
822
+ },
823
+ {
824
+ "RegisterDoublyWrappedReregisterDoublyWrapped" ,
825
+ originalWithoutConstLabel ,
826
+ func (r prometheus.Registerer ) prometheus.Registerer {
827
+ return prometheus .WrapRegistererWithPrefix (
828
+ "wrap_" ,
829
+ prometheus .WrapRegistererWith (prometheus.Labels {"const" : "label" }, r ),
830
+ )
831
+ },
832
+ equalButNotSameWithoutConstLabel ,
833
+ func (r prometheus.Registerer ) prometheus.Registerer {
834
+ return prometheus .WrapRegistererWithPrefix (
835
+ "wrap_" ,
836
+ prometheus .WrapRegistererWith (prometheus.Labels {"const" : "label" }, r ),
837
+ )
838
+ },
839
+ },
770
840
}
771
- if are , ok := err .(prometheus.AlreadyRegisteredError ); ok {
772
- if are .ExistingCollector != original {
773
- t .Error ("expected original collector but got something else" )
774
- }
775
- if are .ExistingCollector == equalButNotSame {
776
- t .Error ("expected original callector but got new one" )
777
- }
778
- } else {
779
- t .Error ("unexpected error:" , err )
841
+
842
+ for _ , s := range scenarios {
843
+ t .Run (s .name , func (t * testing.T ) {
844
+ var err error
845
+ reg := prometheus .NewRegistry ()
846
+ if err = s .registerWith (reg ).Register (s .originalCollector ); err != nil {
847
+ t .Fatal (err )
848
+ }
849
+ if err = s .reRegisterWith (reg ).Register (s .newCollector ); err == nil {
850
+ t .Fatal ("expected error when registering new collector" )
851
+ }
852
+ if are , ok := err .(prometheus.AlreadyRegisteredError ); ok {
853
+ if are .ExistingCollector != s .originalCollector {
854
+ t .Error ("expected original collector but got something else" )
855
+ }
856
+ if are .ExistingCollector == s .newCollector {
857
+ t .Error ("expected original collector but got new one" )
858
+ }
859
+ } else {
860
+ t .Error ("unexpected error:" , err )
861
+ }
862
+ })
780
863
}
781
864
}
782
865
0 commit comments