@@ -164,6 +164,8 @@ type Agent struct {
164164 mode Mode
165165 // listener is the current connection to the server.
166166 listener atomic.Pointer [net.Listener ]
167+ // logger is the agent's logger instance.
168+ logger * log.Entry
167169}
168170
169171// NewAgent creates a new agent instance, requiring the ShellHub server's address to connect to, the namespace's tenant
@@ -254,6 +256,16 @@ func (a *Agent) Initialize() error {
254256
255257 a .closed .Store (false )
256258
259+ a .logger = log .WithFields (log.Fields {
260+ "version" : AgentVersion ,
261+ "tenant_id" : a .authData .Namespace ,
262+ "server_address" : a .config .ServerAddress ,
263+ "ssh_endpoint" : a .serverInfo .Endpoints .SSH ,
264+ "api_endpoint" : a .serverInfo .Endpoints .API ,
265+ "connection_version" : a .config .ConnectionVersion ,
266+ "sshid" : fmt .Sprintf ("%s.%s@%s" , a .authData .Namespace , a .authData .Name , strings .Split (a .serverInfo .Endpoints .SSH , ":" )[0 ]),
267+ })
268+
257269 return nil
258270}
259271
@@ -404,21 +416,11 @@ func (a *Agent) listenV1(ctx context.Context) error {
404416
405417 go a .ping (ctx , AgentPingDefaultInterval ) //nolint:errcheck
406418
407- logger := log .WithFields (log.Fields {
408- "version" : AgentVersion ,
409- "tenant_id" : a .authData .Namespace ,
410- "server_address" : a .config .ServerAddress ,
411- "ssh_endpoint" : a .serverInfo .Endpoints .SSH ,
412- "api_endpoint" : a .serverInfo .Endpoints .API ,
413- "connection_version" : a .config .ConnectionVersion ,
414- "sshid" : fmt .Sprintf ("%s.%s@%s" , a .authData .Namespace , a .authData .Name , strings .Split (a .serverInfo .Endpoints .SSH , ":" )[0 ]),
415- })
416-
417419 ctx , cancel := context .WithCancel (ctx )
418420 go func () {
419421 for {
420422 if a .isClosed () {
421- logger .Info ("Stopped listening for connections" )
423+ a . logger .Info ("Stopped listening for connections" )
422424
423425 cancel ()
424426
@@ -427,28 +429,28 @@ func (a *Agent) listenV1(ctx context.Context) error {
427429
428430 ShellHubConnectV1Path := "/ssh/connection"
429431
430- logger .Debug ("Using tunnel version 1" )
432+ a . logger .Debug ("Using tunnel version 1" )
431433
432434 listener , err := a .cli .NewReverseListenerV1 (
433435 ctx ,
434436 a .authData .Token ,
435437 ShellHubConnectV1Path ,
436438 )
437439 if err != nil {
438- logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
440+ a . logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
439441
440442 time .Sleep (time .Second * 10 )
441443
442444 continue
443445 }
444446 a .listener .Store (& listener )
445447
446- logger .Info ("Server connection established" )
448+ a . logger .Info ("Server connection established" )
447449
448450 a .listening <- true
449451
450452 if err := tun .Listen (ctx , listener ); err != nil {
451- logger .WithError (err ).Error ("Tunnel listener exited with error" )
453+ a . logger .WithError (err ).Error ("Tunnel listener exited with error" )
452454 }
453455
454456 a .listening <- false
@@ -469,21 +471,11 @@ func (a *Agent) listenV2(ctx context.Context) error {
469471
470472 go a .ping (ctx , AgentPingDefaultInterval ) //nolint:errcheck
471473
472- logger := log .WithFields (log.Fields {
473- "version" : AgentVersion ,
474- "tenant_id" : a .authData .Namespace ,
475- "server_address" : a .config .ServerAddress ,
476- "ssh_endpoint" : a .serverInfo .Endpoints .SSH ,
477- "api_endpoint" : a .serverInfo .Endpoints .API ,
478- "connection_version" : a .config .ConnectionVersion ,
479- "sshid" : fmt .Sprintf ("%s.%s@%s" , a .authData .Namespace , a .authData .Name , strings .Split (a .serverInfo .Endpoints .SSH , ":" )[0 ]),
480- })
481-
482474 ctx , cancel := context .WithCancel (ctx )
483475 go func () {
484476 for {
485477 if a .isClosed () {
486- logger .Info ("Stopped listening for connections" )
478+ a . logger .Info ("Stopped listening for connections" )
487479
488480 cancel ()
489481
@@ -492,7 +484,7 @@ func (a *Agent) listenV2(ctx context.Context) error {
492484
493485 ShellHubConnectV2Path := "/agent/connection"
494486
495- logger .Debug ("Using tunnel version 2" )
487+ a . logger .Debug ("Using tunnel version 2" )
496488
497489 listener , err := a .cli .NewReverseListenerV2 (
498490 ctx ,
@@ -501,20 +493,20 @@ func (a *Agent) listenV2(ctx context.Context) error {
501493 client .NewReverseV2ConfigFromMap (a .authData .Config ),
502494 )
503495 if err != nil {
504- logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
496+ a . logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
505497
506498 time .Sleep (time .Second * 10 )
507499
508500 continue
509501 }
510502 a .listener .Store (& listener )
511503
512- logger .Info ("Server connection established" )
504+ a . logger .Info ("Server connection established" )
513505
514506 a .listening <- true
515507
516508 if err := tun .Listen (ctx , listener ); err != nil {
517- logger .WithError (err ).Error ("Tunnel listener exited with error" )
509+ a . logger .WithError (err ).Error ("Tunnel listener exited with error" )
518510 }
519511
520512 a .listening <- false
0 commit comments