@@ -335,7 +335,7 @@ func TestVStreamMulti(t *testing.T) {
335335 }
336336}
337337
338- func TestVStreamsCreatedAndLagMetrics (t * testing.T ) {
338+ func TestVStreamsMetrics (t * testing.T ) {
339339 ctx , cancel := context .WithCancel (context .Background ())
340340 defer cancel ()
341341 cell := "aa"
@@ -346,9 +346,12 @@ func TestVStreamsCreatedAndLagMetrics(t *testing.T) {
346346 vsm := newTestVStreamManager (ctx , hc , st , cell )
347347 vsm .vstreamsCreated .ResetAll ()
348348 vsm .vstreamsLag .ResetAll ()
349+ vsm .vstreamsCount .ResetAll ()
350+ vsm .vstreamsEventsStreamed .ResetAll ()
351+ vsm .vstreamsEndedWithErrors .ResetAll ()
349352 sbc0 := hc .AddTestTablet (cell , "1.1.1.1" , 1001 , ks , "-20" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
350353 addTabletToSandboxTopo (t , ctx , st , ks , "-20" , sbc0 .Tablet ())
351- sbc1 := hc .AddTestTablet (cell , "1.1.1.1 " , 1002 , ks , "20-40" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
354+ sbc1 := hc .AddTestTablet (cell , "1.1.1.2 " , 1002 , ks , "20-40" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
352355 addTabletToSandboxTopo (t , ctx , st , ks , "20-40" , sbc1 .Tablet ())
353356
354357 send0 := []* binlogdatapb.VEvent {
@@ -377,15 +380,96 @@ func TestVStreamsCreatedAndLagMetrics(t *testing.T) {
377380 ch := startVStream (ctx , t , vsm , vgtid , nil )
378381 <- ch
379382 <- ch
383+ expectedLabels1 := "TestVStream.-20.PRIMARY"
384+ expectedLabels2 := "TestVStream.20-40.PRIMARY"
380385 wantVStreamsCreated := make (map [string ]int64 )
381- wantVStreamsCreated ["TestVStream.-20.PRIMARY" ] = 1
382- wantVStreamsCreated ["TestVStream.20-40.PRIMARY" ] = 1
386+ wantVStreamsCreated [expectedLabels1 ] = 1
387+ wantVStreamsCreated [expectedLabels2 ] = 1
383388 assert .Equal (t , wantVStreamsCreated , vsm .vstreamsCreated .Counts (), "vstreamsCreated matches" )
384389
385390 wantVStreamsLag := make (map [string ]int64 )
386- wantVStreamsLag ["TestVStream.-20.PRIMARY" ] = 5
387- wantVStreamsLag ["TestVStream.20-40.PRIMARY" ] = 7
391+ wantVStreamsLag [expectedLabels1 ] = 5
392+ wantVStreamsLag [expectedLabels2 ] = 7
388393 assert .Equal (t , wantVStreamsLag , vsm .vstreamsLag .Counts (), "vstreamsLag matches" )
394+
395+ wantVStreamsCount := make (map [string ]int64 )
396+ wantVStreamsCount [expectedLabels1 ] = 1
397+ wantVStreamsCount [expectedLabels2 ] = 1
398+ assert .Equal (t , wantVStreamsCount , vsm .vstreamsCount .Counts (), "vstreamsCount matches" )
399+
400+ wantVStreamsEventsStreamed := make (map [string ]int64 )
401+ wantVStreamsEventsStreamed [expectedLabels1 ] = 2
402+ wantVStreamsEventsStreamed [expectedLabels2 ] = 2
403+ assert .Equal (t , wantVStreamsEventsStreamed , vsm .vstreamsEventsStreamed .Counts (), "vstreamsEventsStreamed matches" )
404+
405+ wantVStreamsEndedWithErrors := make (map [string ]int64 )
406+ wantVStreamsEndedWithErrors [expectedLabels1 ] = 0
407+ wantVStreamsEndedWithErrors [expectedLabels2 ] = 0
408+ assert .Equal (t , wantVStreamsEndedWithErrors , vsm .vstreamsEndedWithErrors .Counts (), "vstreamsEndedWithErrors matches" )
409+ }
410+
411+ func TestVStreamsMetricsErrors (t * testing.T ) {
412+ ctx , cancel := context .WithCancel (context .Background ())
413+ defer cancel ()
414+ cell := "aa"
415+ ks := "TestVStream"
416+ _ = createSandbox (ks )
417+ hc := discovery .NewFakeHealthCheck (nil )
418+ st := getSandboxTopo (ctx , cell , ks , []string {"-20" , "20-40" })
419+ vsm := newTestVStreamManager (ctx , hc , st , cell )
420+ vsm .vstreamsCreated .ResetAll ()
421+ vsm .vstreamsLag .ResetAll ()
422+ vsm .vstreamsCount .ResetAll ()
423+ vsm .vstreamsEventsStreamed .ResetAll ()
424+ vsm .vstreamsEndedWithErrors .ResetAll ()
425+ sbc0 := hc .AddTestTablet (cell , "1.1.1.1" , 1001 , ks , "-20" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
426+ addTabletToSandboxTopo (t , ctx , st , ks , "-20" , sbc0 .Tablet ())
427+ sbc1 := hc .AddTestTablet (cell , "1.1.1.2" , 1002 , ks , "20-40" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
428+ addTabletToSandboxTopo (t , ctx , st , ks , "20-40" , sbc1 .Tablet ())
429+
430+ const wantErr = "Invalid arg message"
431+ sbc0 .AddVStreamEvents (nil , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , wantErr ))
432+
433+ send1 := []* binlogdatapb.VEvent {
434+ {Type : binlogdatapb .VEventType_GTID , Gtid : "gtid02" },
435+ {Type : binlogdatapb .VEventType_COMMIT , Timestamp : 10 , CurrentTime : 17 * 1e9 },
436+ }
437+ sbc1 .AddVStreamEvents (send1 , nil )
438+
439+ vgtid := & binlogdatapb.VGtid {
440+ ShardGtids : []* binlogdatapb.ShardGtid {{
441+ Keyspace : ks ,
442+ Shard : "-20" ,
443+ Gtid : "pos" ,
444+ }, {
445+ Keyspace : ks ,
446+ Shard : "20-40" ,
447+ Gtid : "pos" ,
448+ }},
449+ }
450+ ch := make (chan * binlogdatapb.VStreamResponse )
451+ done := make (chan struct {})
452+ go func () {
453+ err := vsm .VStream (ctx , topodatapb .TabletType_PRIMARY , vgtid , nil , & vtgatepb.VStreamFlags {}, func (events []* binlogdatapb.VEvent ) error {
454+ ch <- & binlogdatapb.VStreamResponse {Events : events }
455+ return nil
456+ })
457+
458+ if err == nil || ! strings .Contains (err .Error (), wantErr ) {
459+ require .ErrorContains (t , err , wantErr )
460+ }
461+ close (done )
462+ }()
463+ <- ch
464+ <- done
465+
466+ expectedLabels1 := "TestVStream.-20.PRIMARY"
467+ expectedLabels2 := "TestVStream.20-40.PRIMARY"
468+
469+ wantVStreamsEndedWithErrors := make (map [string ]int64 )
470+ wantVStreamsEndedWithErrors [expectedLabels1 ] = 1
471+ wantVStreamsEndedWithErrors [expectedLabels2 ] = 0
472+ assert .Equal (t , wantVStreamsEndedWithErrors , vsm .vstreamsEndedWithErrors .Counts (), "vstreamsEndedWithErrors matches" )
389473}
390474
391475func TestVStreamRetriableErrors (t * testing.T ) {
0 commit comments