@@ -400,3 +400,82 @@ func TestWithGRPCInfo(t *testing.T) {
400400 require .Equal (t , "container-1" , fields .ContainerID )
401401 require .Equal (t , "db-1" , fields .Database )
402402}
403+
404+ func TestReportGRPCCallBeginJSON (t * testing.T ) {
405+ ctx := grpcinfo .WithGRPCInfo (context .Background ())
406+ ctx = peer .NewContext (
407+ ctx , & peer.Peer {
408+ Addr : & mockAddr {address : "192.168.1.1" },
409+ },
410+ )
411+ ctx = metadata .NewIncomingContext (ctx , metadata .Pairs ("x_forwarded_for" , "10.0.0.1" ))
412+
413+ req := & pb.GetBackupRequest {Id : "id-req" }
414+ method := pb .BackupScheduleService_GetBackupSchedule_FullMethodName
415+ subj := "subj"
416+ token := "tok.***"
417+
418+ out := CaptureEventAsString (
419+ t , func () {
420+ ReportGRPCCallBegin (ctx , req , method , subj , token )
421+ },
422+ )
423+
424+ var ej EventJson
425+ require .NoError (t , json .Unmarshal ([]byte (out ), & ej ))
426+
427+ var evt GRPCCallEvent
428+ require .NoError (t , json .Unmarshal ([]byte (ej .Event .TextData ), & evt ))
429+
430+ assert .Equal (t , method , evt .MethodName )
431+ assert .Equal (t , StatusInProcess , evt .Status )
432+ assert .Equal (t , formatSubject (subj ), evt .Subject )
433+ assert .Equal (t , token , evt .SanitizedToken )
434+ assert .Equal (t , "{none}" , evt .FolderID )
435+ assert .Equal (t , "{none}" , evt .Database )
436+ requestID , _ := grpcinfo .GetRequestID (ctx )
437+ assert .Equal (t , requestID , evt .IdempotencyKey )
438+ assert .Contains (t , string (evt .GRPCRequest ), "id-req" )
439+ assert .Contains (t , evt .RemoteAddress , "10.0.0.1" )
440+ assert .Contains (t , evt .RemoteAddress , "192.168.1.1" )
441+ }
442+
443+ func TestReportGRPCCallEndJSON (t * testing.T ) {
444+ ctx := grpcinfo .WithGRPCInfo (context .Background ())
445+ // add peer info and forwarded header
446+ ctx = peer .NewContext (
447+ ctx , & peer.Peer {
448+ Addr : & mockAddr {address : "192.168.2.2" },
449+ },
450+ )
451+ ctx = metadata .NewIncomingContext (ctx , metadata .Pairs ("x_forwarded_for" , "172.16.0.5" ))
452+
453+ method := pb .BackupScheduleService_GetBackupSchedule_FullMethodName
454+ subj := "end-subj"
455+ token := "end-tok"
456+ database := "db-1"
457+ err := status .Error (codes .NotFound , "not found" )
458+
459+ out := CaptureEventAsString (
460+ t , func () {
461+ ReportGRPCCallEnd (ctx , method , subj , token , "" , database , err )
462+ },
463+ )
464+
465+ var ej EventJson
466+ require .NoError (t , json .Unmarshal ([]byte (out ), & ej ))
467+
468+ var evt GRPCCallEvent
469+ require .NoError (t , json .Unmarshal ([]byte (ej .Event .TextData ), & evt ))
470+
471+ assert .Equal (t , method , evt .MethodName )
472+ assert .Equal (t , formatSubject (subj ), evt .Subject )
473+ assert .Equal (t , token , evt .SanitizedToken )
474+ assert .Equal (t , "{none}" , evt .FolderID )
475+ assert .Equal (t , database , evt .Database )
476+ assert .Equal (t , StatusError , evt .Status )
477+ assert .Equal (t , err .Error (), evt .Reason )
478+ assert .Nil (t , evt .GRPCRequest )
479+ assert .Contains (t , evt .RemoteAddress , "172.16.0.5" )
480+ assert .Contains (t , evt .RemoteAddress , "192.168.2.2" )
481+ }
0 commit comments