@@ -160,11 +160,14 @@ func TestSiteLinkErrorMetrics(t *testing.T) {
160160 reg := prometheus .NewPedanticRegistry ()
161161 metrics := New (reg )
162162 interRouter := ptrTo ("inter-router" )
163+ statusUp := ptrTo ("up" )
163164 statusDown := ptrTo ("down" )
164165
165166 metrics .Add (vanflow.RouterRecord {
166167 BaseRecord : vanflow .NewBase ("r01" ), Parent : ptrTo ("s01" ), Mode : interRouter ,
167168 })
169+
170+ // Initial Add sets the baseline. Counter stays at zero.
168171 metrics .Add (vanflow.LinkRecord {
169172 BaseRecord : vanflow .NewBase ("l01" ),
170173 Role : interRouter ,
@@ -174,6 +177,8 @@ func TestSiteLinkErrorMetrics(t *testing.T) {
174177 })
175178 assert .Equal (t , prom_testutil .CollectAndCount (reg , "skupper_site_link_errors_total" ), 1 )
176179 assert .Equal (t , prom_testutil .ToFloat64 (metrics .siteLinkErrors .WithLabelValues ("s01" , "inter-router" )), 0.0 )
180+
181+ // Update increments DownCount 12 -> 15: counter += 3.
177182 metrics .Update (
178183 vanflow.LinkRecord {
179184 BaseRecord : vanflow .NewBase ("l01" ),
@@ -192,23 +197,51 @@ func TestSiteLinkErrorMetrics(t *testing.T) {
192197 )
193198 assert .Equal (t , prom_testutil .CollectAndCount (reg , "skupper_site_link_errors_total" ), 1 )
194199 assert .Equal (t , prom_testutil .ToFloat64 (metrics .siteLinkErrors .WithLabelValues ("s01" , "inter-router" )), 3.0 )
200+
201+ // Source reconnect with higher DownCount: the collector removes all
202+ // records from the source and re-adds them. The re-added record
203+ // carries the already-incremented DownCount. The counter must detect
204+ // the delta across the Remove+Add boundary.
195205 metrics .Remove (vanflow.LinkRecord {
196206 BaseRecord : vanflow .NewBase ("l01" ),
197207 Role : interRouter ,
198208 Status : statusDown ,
199209 Parent : ptrTo ("r01" ),
200210 DownCount : ptrTo (uint64 (15 )),
201211 })
202- // cannot decrease
203- assert .Equal (t , prom_testutil .CollectAndCount (reg , "skupper_site_link_errors_total" ), 1 )
212+ // Counter must not decrease after removal.
204213 assert .Equal (t , prom_testutil .ToFloat64 (metrics .siteLinkErrors .WithLabelValues ("s01" , "inter-router" )), 3.0 )
214+ metrics .Add (vanflow.LinkRecord {
215+ BaseRecord : vanflow .NewBase ("l01" ),
216+ Role : interRouter ,
217+ Status : statusUp ,
218+ Peer : ptrTo ("ap02" ),
219+ Parent : ptrTo ("r01" ),
220+ DownCount : ptrTo (uint64 (17 )),
221+ })
222+ // Delta of 2 (17 - 15): counter = 5.
223+ assert .Equal (t , prom_testutil .ToFloat64 (metrics .siteLinkErrors .WithLabelValues ("s01" , "inter-router" )), 5.0 )
224+
225+ // Router restart resets DownCount to zero. Remove+Add with a lower
226+ // DownCount resets the baseline without incrementing the counter.
227+ metrics .Remove (vanflow.LinkRecord {
228+ BaseRecord : vanflow .NewBase ("l01" ),
229+ Role : interRouter ,
230+ Status : statusUp ,
231+ Peer : ptrTo ("ap02" ),
232+ Parent : ptrTo ("r01" ),
233+ DownCount : ptrTo (uint64 (17 )),
234+ })
205235 metrics .Add (vanflow.LinkRecord {
206236 BaseRecord : vanflow .NewBase ("l01" ),
207237 Role : interRouter ,
208238 Status : statusDown ,
209239 Parent : ptrTo ("r01" ),
210240 DownCount : ptrTo (uint64 (0 )),
211241 })
242+ assert .Equal (t , prom_testutil .ToFloat64 (metrics .siteLinkErrors .WithLabelValues ("s01" , "inter-router" )), 5.0 )
243+
244+ // After reset, subsequent Updates still increment from the new baseline.
212245 metrics .Update (
213246 vanflow.LinkRecord {
214247 BaseRecord : vanflow .NewBase ("l01" ),
@@ -225,9 +258,7 @@ func TestSiteLinkErrorMetrics(t *testing.T) {
225258 DownCount : ptrTo (uint64 (1 )),
226259 },
227260 )
228- // after delete, can still increment
229- assert .Equal (t , prom_testutil .CollectAndCount (reg , "skupper_site_link_errors_total" ), 1 )
230- assert .Equal (t , prom_testutil .ToFloat64 (metrics .siteLinkErrors .WithLabelValues ("s01" , "inter-router" )), 4.0 )
261+ assert .Equal (t , prom_testutil .ToFloat64 (metrics .siteLinkErrors .WithLabelValues ("s01" , "inter-router" )), 6.0 )
231262}
232263
233264func ptrTo [T any ](s T ) * T {
0 commit comments