@@ -253,14 +253,14 @@ func disconnectCloud(reason error) {
253253 defer cloudDisconnectLock .Unlock ()
254254
255255 if cloudDisconnectChan == nil {
256- cloudLogger .Tracef ("cloud disconnect channel is not set, no need to disconnect" )
256+ cloudLogger .Trace (). Msg ("cloud disconnect channel is not set, no need to disconnect" )
257257 return
258258 }
259259
260260 // just in case the channel is closed, we don't want to panic
261261 defer func () {
262262 if r := recover (); r != nil {
263- cloudLogger .Infof ( " cloud disconnect channel is closed, no need to disconnect: %v" , r )
263+ cloudLogger .Warn (). Interface ( "reason" , r ). Msg ( " cloud disconnect channel is closed, no need to disconnect" )
264264 }
265265 }()
266266 cloudDisconnectChan <- reason
@@ -289,11 +289,16 @@ func runWebsocketClient() error {
289289 header .Set ("Authorization" , "Bearer " + config .CloudToken )
290290 dialCtx , cancelDial := context .WithTimeout (context .Background (), CloudWebSocketConnectTimeout )
291291
292+ scopedLogger := websocketLogger .With ().
293+ Str ("source" , wsURL .Host ).
294+ Str ("sourceType" , "cloud" ).
295+ Logger ()
296+
292297 defer cancelDial ()
293298 c , _ , err := websocket .Dial (dialCtx , wsURL .String (), & websocket.DialOptions {
294299 HTTPHeader : header ,
295300 OnPingReceived : func (ctx context.Context , payload []byte ) bool {
296- websocketLogger . Infof ( "ping frame received: %v, source: %s, sourceType: cloud " , payload , wsURL . Host )
301+ scopedLogger . Info (). Bytes ( "payload " , payload ). Int ( "length" , len ( payload )). Msg ( "ping frame received" )
297302
298303 metricConnectionTotalPingReceivedCount .WithLabelValues ("cloud" , wsURL .Host ).Inc ()
299304 metricConnectionLastPingReceivedTimestamp .WithLabelValues ("cloud" , wsURL .Host ).SetToCurrentTime ()
@@ -304,19 +309,19 @@ func runWebsocketClient() error {
304309 // if the context is canceled, we don't want to return an error
305310 if err != nil {
306311 if errors .Is (err , context .Canceled ) {
307- cloudLogger .Infof ("websocket connection canceled" )
312+ cloudLogger .Info (). Msg ("websocket connection canceled" )
308313 return nil
309314 }
310315 return err
311316 }
312317 defer c .CloseNow () //nolint:errcheck
313- cloudLogger .Infof ( "websocket connected to %s " , wsURL )
318+ cloudLogger .Info (). Str ( "url " , wsURL . String ()). Msg ( "websocket connected" )
314319
315320 // set the metrics when we successfully connect to the cloud.
316321 wsResetMetrics (true , "cloud" , wsURL .Host )
317322
318323 // we don't have a source for the cloud connection
319- return handleWebRTCSignalWsMessages (c , true , wsURL .Host )
324+ return handleWebRTCSignalWsMessages (c , true , wsURL .Host , & scopedLogger )
320325}
321326
322327func authenticateSession (ctx context.Context , c * websocket.Conn , req WebRTCSessionRequest ) error {
@@ -327,7 +332,7 @@ func authenticateSession(ctx context.Context, c *websocket.Conn, req WebRTCSessi
327332 _ = wsjson .Write (context .Background (), c , gin.H {
328333 "error" : fmt .Sprintf ("failed to initialize OIDC provider: %v" , err ),
329334 })
330- cloudLogger .Errorf ( "failed to initialize OIDC provider: %v" , err )
335+ cloudLogger .Warn (). Err ( err ). Msg ( "failed to initialize OIDC provider" )
331336 return err
332337 }
333338
@@ -396,8 +401,8 @@ func handleSessionRequest(ctx context.Context, c *websocket.Conn, req WebRTCSess
396401 }()
397402 }
398403
399- cloudLogger .Info ("new session accepted" )
400- cloudLogger .Tracef ( " new session accepted: %v" , session )
404+ cloudLogger .Info (). Interface ( "session" , session ). Msg ( "new session accepted" )
405+ cloudLogger .Trace (). Interface ( "session" , session ). Msg ( " new session accepted" )
401406 currentSession = session
402407 _ = wsjson .Write (context .Background (), c , gin.H {"type" : "answer" , "data" : sd })
403408 return nil
@@ -413,21 +418,21 @@ func RunWebsocketClient() {
413418
414419 // If the network is not up, well, we can't connect to the cloud.
415420 if ! networkState .Up {
416- cloudLogger .Warn ("waiting for network to be up, will retry in 3 seconds" )
421+ cloudLogger .Warn (). Msg ( "waiting for network to be up, will retry in 3 seconds" )
417422 time .Sleep (3 * time .Second )
418423 continue
419424 }
420425
421426 // If the system time is not synchronized, the API request will fail anyway because the TLS handshake will fail.
422427 if isTimeSyncNeeded () && ! timeSyncSuccess {
423- cloudLogger .Warn ("system time is not synced, will retry in 3 seconds" )
428+ cloudLogger .Warn (). Msg ( "system time is not synced, will retry in 3 seconds" )
424429 time .Sleep (3 * time .Second )
425430 continue
426431 }
427432
428433 err := runWebsocketClient ()
429434 if err != nil {
430- cloudLogger .Errorf ( "websocket client error: %v" , err )
435+ cloudLogger .Warn (). Err ( err ). Msg ( "websocket client error" )
431436 metricCloudConnectionStatus .Set (0 )
432437 metricCloudConnectionFailureCount .Inc ()
433438 time .Sleep (5 * time .Second )
@@ -479,7 +484,7 @@ func rpcDeregisterDevice() error {
479484 return fmt .Errorf ("failed to save configuration after deregistering: %w" , err )
480485 }
481486
482- cloudLogger .Infof ("device deregistered, disconnecting from cloud" )
487+ cloudLogger .Info (). Msg ("device deregistered, disconnecting from cloud" )
483488 disconnectCloud (fmt .Errorf ("device deregistered" ))
484489
485490 return nil
0 commit comments