@@ -28,21 +28,21 @@ import (
2828 "sync/atomic"
2929 "time"
3030
31- msdk "github.com/livekit/media-sdk"
32- "github.com/livekit/protocol/rpc"
33-
3431 "github.com/frostbyte73/core"
3532 "github.com/icholy/digest"
3633 "github.com/pkg/errors"
3734
35+ msdk "github.com/livekit/media-sdk"
3836 "github.com/livekit/media-sdk/dtmf"
3937 "github.com/livekit/media-sdk/rtp"
4038 "github.com/livekit/media-sdk/sdp"
4139 "github.com/livekit/media-sdk/tones"
4240 "github.com/livekit/protocol/livekit"
4341 "github.com/livekit/protocol/logger"
42+ "github.com/livekit/protocol/rpc"
4443 lksip "github.com/livekit/protocol/sip"
4544 "github.com/livekit/protocol/tracer"
45+ "github.com/livekit/protocol/utils/traceid"
4646 "github.com/livekit/psrpc"
4747 lksdk "github.com/livekit/server-sdk-go/v2"
4848 "github.com/livekit/sipgo/sip"
@@ -150,7 +150,7 @@ func (s *Server) getInvite(sipCallID string) *inProgressInvite {
150150 return is
151151}
152152
153- func (s * Server ) handleInviteAuth (log logger.Logger , req * sip.Request , tx sip.ServerTransaction , from , username , password string ) (ok bool ) {
153+ func (s * Server ) handleInviteAuth (tid traceid. ID , log logger.Logger , req * sip.Request , tx sip.ServerTransaction , from , username , password string ) (ok bool ) {
154154 log = log .WithValues (
155155 "username" , username ,
156156 "passwordHash" , hashPassword (password ),
@@ -306,10 +306,12 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
306306 return psrpc .NewError (psrpc .MalformedRequest , errors .Wrap (err , "cannot parse source IP" ))
307307 }
308308 callID := lksip .NewCallID ()
309+ tid := traceid .FromGUID (callID )
309310 tr := callTransportFromReq (req )
310311 legTr := legTransportFromReq (req )
311312 log := s .log .WithValues (
312313 "callID" , callID ,
314+ "traceID" , tid .String (),
313315 "fromIP" , src .Addr (),
314316 "toIP" , req .Destination (),
315317 "transport" , tr ,
@@ -446,7 +448,7 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
446448 cc .Processing ()
447449 }
448450 s .getCallInfo (cc .SIPCallID ()).countInvite (log , req )
449- if ! s .handleInviteAuth (log , req , tx , from .User , r .Username , r .Password ) {
451+ if ! s .handleInviteAuth (tid , log , req , tx , from .User , r .Username , r .Password ) {
450452 cmon .InviteErrorShort ("unauthorized" )
451453 // handleInviteAuth will generate the SIP Response as needed
452454 return psrpc .NewErrorf (psrpc .PermissionDenied , "invalid credentials were provided" )
@@ -457,9 +459,9 @@ func (s *Server) processInvite(req *sip.Request, tx sip.ServerTransaction) (retE
457459 // ok
458460 }
459461
460- call = s .newInboundCall (log , cmon , cc , callInfo , state , nil )
462+ call = s .newInboundCall (tid , log , cmon , cc , callInfo , state , start , nil )
461463 call .joinDur = joinDur
462- return call .handleInvite (call .ctx , req , r .TrunkID , s .conf )
464+ return call .handleInvite (call .ctx , tid , req , r .TrunkID , s .conf )
463465}
464466
465467func (s * Server ) onOptions (log * slog.Logger , req * sip.Request , tx sip.ServerTransaction ) {
@@ -576,10 +578,12 @@ func (s *Server) onNotify(log *slog.Logger, req *sip.Request, tx sip.ServerTrans
576578
577579type inboundCall struct {
578580 s * Server
581+ tid traceid.ID
579582 log logger.Logger
580583 cc * sipInbound
581584 mon * stats.CallMonitor
582585 state * CallState
586+ callStart time.Time
583587 extraAttrs map [string ]string
584588 attrsToHdr map [string ]string
585589 ctx context.Context
@@ -600,18 +604,22 @@ type inboundCall struct {
600604}
601605
602606func (s * Server ) newInboundCall (
607+ tid traceid.ID ,
603608 log logger.Logger ,
604609 mon * stats.CallMonitor ,
605610 cc * sipInbound ,
606611 call * rpc.SIPCall ,
607612 state * CallState ,
613+ callStart time.Time ,
608614 extra map [string ]string ,
609615) * inboundCall {
610616 // Map known headers immediately on join. The rest of the mapping will be available later.
611617 extra = HeadersToAttrs (extra , nil , 0 , cc , nil )
612618 c := & inboundCall {
613619 s : s ,
614620 log : log ,
621+ tid : tid ,
622+ callStart : callStart ,
615623 mon : mon ,
616624 cc : cc ,
617625 call : call ,
@@ -633,7 +641,7 @@ func (s *Server) newInboundCall(
633641 return c
634642}
635643
636- func (c * inboundCall ) handleInvite (ctx context.Context , req * sip.Request , trunkID string , conf * config.Config ) error {
644+ func (c * inboundCall ) handleInvite (ctx context.Context , tid traceid. ID , req * sip.Request , trunkID string , conf * config.Config ) error {
637645 c .mon .InviteAccept ()
638646 c .mon .CallStart ()
639647 defer c .mon .CallEnd ()
@@ -697,7 +705,7 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
697705 }
698706
699707 runMedia := func (enc livekit.SIPMediaEncryption ) ([]byte , error ) {
700- answerData , err := c .runMediaConn (req .Body (), enc , conf , disp .EnabledFeatures )
708+ answerData , err := c .runMediaConn (tid , req .Body (), enc , conf , disp .EnabledFeatures )
701709 if err != nil {
702710 isError := true
703711 status , reason := callDropped , "media-failed"
@@ -874,7 +882,7 @@ func (c *inboundCall) handleInvite(ctx context.Context, req *sip.Request, trunkI
874882 }
875883}
876884
877- func (c * inboundCall ) runMediaConn (offerData []byte , enc livekit.SIPMediaEncryption , conf * config.Config , features []livekit.SIPFeature ) (answerData []byte , _ error ) {
885+ func (c * inboundCall ) runMediaConn (tid traceid. ID , offerData []byte , enc livekit.SIPMediaEncryption , conf * config.Config , features []livekit.SIPFeature ) (answerData []byte , _ error ) {
878886 c .mon .SDPSize (len (offerData ), true )
879887 c .log .Debugw ("SDP offer" , "sdp" , string (offerData ))
880888 e , err := sdpEncryption (enc )
@@ -883,7 +891,7 @@ func (c *inboundCall) runMediaConn(offerData []byte, enc livekit.SIPMediaEncrypt
883891 return nil , err
884892 }
885893
886- mp , err := NewMediaPort (c .log , c .mon , & MediaOptions {
894+ mp , err := NewMediaPort (tid , c .log , c .mon , & MediaOptions {
887895 IP : c .s .sconf .MediaIP ,
888896 Ports : conf .RTPPort ,
889897 MediaTimeoutInitial : c .s .conf .MediaTimeoutInitial ,
@@ -1057,7 +1065,7 @@ func (c *inboundCall) pinPrompt(ctx context.Context, trunkID string) (disp CallD
10571065}
10581066
10591067func (c * inboundCall ) printStats (log logger.Logger ) {
1060- log .Infow ("call statistics" , "stats" , c .stats .Load ())
1068+ log .Infow ("call statistics" , "stats" , c .stats .Load (), "durMin" , int ( time . Since ( c . callStart ). Minutes ()) )
10611069}
10621070
10631071// close should only be called from handleInvite.
@@ -1095,11 +1103,13 @@ func (c *inboundCall) close(error bool, status CallStatus, reason string) {
10951103
10961104 // Call the handler asynchronously to avoid blocking
10971105 if c .s .handler != nil {
1098- go c .s .handler .OnSessionEnd (context .Background (), & CallIdentifier {
1099- ProjectID : c .projectID ,
1100- CallID : c .call .LkCallId ,
1101- SipCallID : c .call .SipCallId ,
1102- }, c .state .callInfo , reason )
1106+ go func (tid traceid.ID ) {
1107+ c .s .handler .OnSessionEnd (context .Background (), & CallIdentifier {
1108+ ProjectID : c .projectID ,
1109+ CallID : c .call .LkCallId ,
1110+ SipCallID : c .call .SipCallId ,
1111+ }, c .state .callInfo , reason )
1112+ }(c .tid )
11031113 }
11041114
11051115 c .cancel ()
0 commit comments