@@ -69,10 +69,24 @@ var (
69
69
"test_gauge" ,
70
70
"Testing gauge functionality" ,
71
71
)
72
+
73
+ testDisabledSum = monitoring .NewSum (
74
+ "events_disabled_total" ,
75
+ "Number of events observed, by name and kind" ,
76
+ monitoring .WithLabels (name , kind ),
77
+ )
78
+
79
+ testConditionalSum = monitoring .NewSum (
80
+ "events_conditional_total" ,
81
+ "Number of events observed, by name and kind" ,
82
+ monitoring .WithLabels (name , kind ),
83
+ )
72
84
)
73
85
74
86
func init () {
75
87
monitoring .MustRegister (testSum , hookSum , int64Sum , testDistribution , testGauge )
88
+ testDisabledSum = monitoring .RegisterIf (testDisabledSum , func () bool { return false })
89
+ testConditionalSum = monitoring .RegisterIf (testConditionalSum , func () bool { return true })
76
90
}
77
91
78
92
func TestSum (t * testing.T ) {
@@ -141,6 +155,23 @@ func TestSum(t *testing.T) {
141
155
}
142
156
}
143
157
158
+ func TestRegisterIfSum (t * testing.T ) {
159
+ testDisabledSum .With (name .Value ("foo" ), kind .Value ("bar" )).Increment ()
160
+ var err error
161
+ _ , err = view .RetrieveData (testDisabledSum .Name ())
162
+ if err == nil || ! strings .EqualFold (err .Error (), "cannot retrieve data; view \" events_disabled_total\" is not registered" ) {
163
+ t .Errorf ("failure validating disabled metrics. exptected error but got %v" , err )
164
+ }
165
+ testConditionalSum .With (name .Value ("foo" ), kind .Value ("bar" )).Increment ()
166
+ rows , err := view .RetrieveData (testConditionalSum .Name ())
167
+ if err != nil {
168
+ t .Errorf ("exptected to register metric %s but got %v" , testConditionalSum .Name (), err )
169
+ }
170
+ if len (rows ) == 0 {
171
+ t .Errorf ("exptected to metric %s has values but got %v" , testConditionalSum .Name (), len (rows ))
172
+ }
173
+ }
174
+
144
175
func TestGauge (t * testing.T ) {
145
176
exp := & testExporter {rows : make (map [string ][]* view.Row )}
146
177
view .RegisterExporter (exp )
0 commit comments