@@ -113,21 +113,75 @@ func TestGaugeV_SetWithLabels(t *testing.T) {
113113 assert .Len (t , m .Label , 2 )
114114}
115115
116- func TestNewBuildInfoCollector (t * testing.T ) {
117- metric := NewGaugeFuncMetric (prometheus.GaugeOpts {
118- Namespace : Namespace ,
119- Name : "build_info" ,
120- ConstLabels : prometheus.Labels {
121- "version" : "0.0.1" ,
122- "goversion" : "1.24" ,
123- "arch" : "arm64" ,
116+ func TestNewGaugeFuncMetric (t * testing.T ) {
117+ tests := []struct {
118+ name string
119+ metricName string
120+ subSystem string
121+ constLabels prometheus.Labels
122+ expectedFqName string
123+ expectedDescString string
124+ expectedGaugeFuncReturn float64
125+ }{
126+ {
127+ name : "NewGaugeFuncMetric returns build_info" ,
128+ metricName : "build_info" ,
129+ subSystem : "" ,
130+ constLabels : prometheus.Labels {
131+ "version" : "0.0.1" ,
132+ "goversion" : "1.24" ,
133+ "arch" : "arm64" ,
134+ },
135+ expectedFqName : "external_dns_build_info" ,
136+ expectedDescString : "version=\" 0.0.1\" " ,
137+ expectedGaugeFuncReturn : 1 ,
138+ },
139+ {
140+ name : "NewGaugeFuncMetric subsystem alters name" ,
141+ metricName : "metricName" ,
142+ subSystem : "subSystem" ,
143+ constLabels : prometheus.Labels {},
144+ expectedFqName : "external_dns_subSystem_metricName" ,
145+ expectedDescString : "" ,
146+ expectedGaugeFuncReturn : 1 ,
124147 },
125- })
148+ {
149+ name : "NewGaugeFuncMetric GaugeFunc returns 1" ,
150+ metricName : "metricName" ,
151+ subSystem : "" ,
152+ constLabels : prometheus.Labels {},
153+ expectedFqName : "external_dns_metricName" ,
154+ expectedDescString : "" ,
155+ expectedGaugeFuncReturn : 1 ,
156+ },
157+ }
158+
159+ for _ , tt := range tests {
160+ t .Run (tt .name , func (t * testing.T ) {
161+ metric := NewGaugeFuncMetric (prometheus.GaugeOpts {
162+ Namespace : Namespace ,
163+ Name : tt .metricName ,
164+ Subsystem : tt .subSystem ,
165+ ConstLabels : tt .constLabels ,
166+ })
167+
168+ desc := metric .GaugeFunc .Desc ()
169+
170+ assert .Equal (t , tt .expectedFqName , reflect .ValueOf (desc ).Elem ().FieldByName ("fqName" ).String ())
171+ assert .Contains (t , desc .String (), tt .expectedDescString )
126172
127- desc := metric .GaugeFunc .Desc ()
173+ testRegistry := prometheus .NewRegistry ()
174+ err := testRegistry .Register (metric .GaugeFunc )
175+ require .NoError (t , err )
128176
129- assert .Equal (t , "external_dns_build_info" , reflect .ValueOf (desc ).Elem ().FieldByName ("fqName" ).String ())
130- assert .Contains (t , desc .String (), "version=\" 0.0.1\" " )
177+ metricFamily , err := testRegistry .Gather ()
178+ require .NoError (t , err )
179+ require .Len (t , metricFamily , 1 )
180+
181+ require .NotNil (t , metricFamily [0 ].Metric [0 ].Gauge )
182+ assert .InDelta (t , tt .expectedGaugeFuncReturn , metricFamily [0 ].Metric [0 ].GetGauge ().GetValue (), 0.0001 )
183+ })
184+ }
131185}
132186
133187func TestSummaryV_SetWithLabels (t * testing.T ) {
0 commit comments