@@ -179,6 +179,8 @@ type Agent struct {
179179 mode Mode
180180 // listener is the current connection to the server.
181181 listener atomic.Pointer [net.Listener ]
182+ // logger is the agent's logger instance.
183+ logger * log.Entry
182184}
183185
184186// NewAgent creates a new agent instance, requiring the ShellHub server's address to connect to, the namespace's tenant
@@ -269,6 +271,16 @@ func (a *Agent) Initialize() error {
269271
270272 a .closed .Store (false )
271273
274+ a .logger = log .WithFields (log.Fields {
275+ "version" : AgentVersion ,
276+ "tenant_id" : a .authData .Namespace ,
277+ "server_address" : a .config .ServerAddress ,
278+ "ssh_endpoint" : a .serverInfo .Endpoints .SSH ,
279+ "api_endpoint" : a .serverInfo .Endpoints .API ,
280+ "connection_version" : a .config .ConnectionVersion ,
281+ "sshid" : fmt .Sprintf ("%s.%s@%s" , a .authData .Namespace , a .authData .Name , strings .Split (a .serverInfo .Endpoints .SSH , ":" )[0 ]),
282+ })
283+
272284 return nil
273285}
274286
@@ -419,21 +431,11 @@ func (a *Agent) listenV1(ctx context.Context) error {
419431
420432 go a .ping (ctx , AgentPingDefaultInterval ) //nolint:errcheck
421433
422- logger := log .WithFields (log.Fields {
423- "version" : AgentVersion ,
424- "tenant_id" : a .authData .Namespace ,
425- "server_address" : a .config .ServerAddress ,
426- "ssh_endpoint" : a .serverInfo .Endpoints .SSH ,
427- "api_endpoint" : a .serverInfo .Endpoints .API ,
428- "connection_version" : a .config .ConnectionVersion ,
429- "sshid" : fmt .Sprintf ("%s.%s@%s" , a .authData .Namespace , a .authData .Name , strings .Split (a .serverInfo .Endpoints .SSH , ":" )[0 ]),
430- })
431-
432434 ctx , cancel := context .WithCancel (ctx )
433435 go func () {
434436 for {
435437 if a .isClosed () {
436- logger .Info ("Stopped listening for connections" )
438+ a . logger .Info ("Stopped listening for connections" )
437439
438440 cancel ()
439441
@@ -442,28 +444,28 @@ func (a *Agent) listenV1(ctx context.Context) error {
442444
443445 ShellHubConnectV1Path := "/ssh/connection"
444446
445- logger .Debug ("Using tunnel version 1" )
447+ a . logger .Debug ("Using tunnel version 1" )
446448
447449 listener , err := a .cli .NewReverseListenerV1 (
448450 ctx ,
449451 a .authData .Token ,
450452 ShellHubConnectV1Path ,
451453 )
452454 if err != nil {
453- logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
455+ a . logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
454456
455457 time .Sleep (time .Second * 10 )
456458
457459 continue
458460 }
459461 a .listener .Store (& listener )
460462
461- logger .Info ("Server connection established" )
463+ a . logger .Info ("Server connection established" )
462464
463465 a .listening <- true
464466
465467 if err := tun .Listen (ctx , listener ); err != nil {
466- logger .WithError (err ).Error ("Tunnel listener exited with error" )
468+ a . logger .WithError (err ).Error ("Tunnel listener exited with error" )
467469 }
468470
469471 a .listening <- false
@@ -484,21 +486,11 @@ func (a *Agent) listenV2(ctx context.Context) error {
484486
485487 go a .ping (ctx , AgentPingDefaultInterval ) //nolint:errcheck
486488
487- logger := log .WithFields (log.Fields {
488- "version" : AgentVersion ,
489- "tenant_id" : a .authData .Namespace ,
490- "server_address" : a .config .ServerAddress ,
491- "ssh_endpoint" : a .serverInfo .Endpoints .SSH ,
492- "api_endpoint" : a .serverInfo .Endpoints .API ,
493- "connection_version" : a .config .ConnectionVersion ,
494- "sshid" : fmt .Sprintf ("%s.%s@%s" , a .authData .Namespace , a .authData .Name , strings .Split (a .serverInfo .Endpoints .SSH , ":" )[0 ]),
495- })
496-
497489 ctx , cancel := context .WithCancel (ctx )
498490 go func () {
499491 for {
500492 if a .isClosed () {
501- logger .Info ("Stopped listening for connections" )
493+ a . logger .Info ("Stopped listening for connections" )
502494
503495 cancel ()
504496
@@ -507,7 +499,7 @@ func (a *Agent) listenV2(ctx context.Context) error {
507499
508500 ShellHubConnectV2Path := "/agent/connection"
509501
510- logger .Debug ("Using tunnel version 2" )
502+ a . logger .Debug ("Using tunnel version 2" )
511503
512504 listener , err := a .cli .NewReverseListenerV2 (
513505 ctx ,
@@ -516,20 +508,20 @@ func (a *Agent) listenV2(ctx context.Context) error {
516508 client .NewReverseV2ConfigFromMap (a .authData .Config ),
517509 )
518510 if err != nil {
519- logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
511+ a . logger .Error ("Failed to connect to server through reverse tunnel. Retry in 10 seconds" )
520512
521513 time .Sleep (time .Second * 10 )
522514
523515 continue
524516 }
525517 a .listener .Store (& listener )
526518
527- logger .Info ("Server connection established" )
519+ a . logger .Info ("Server connection established" )
528520
529521 a .listening <- true
530522
531523 if err := tun .Listen (ctx , listener ); err != nil {
532- logger .WithError (err ).Error ("Tunnel listener exited with error" )
524+ a . logger .WithError (err ).Error ("Tunnel listener exited with error" )
533525 }
534526
535527 a .listening <- false
0 commit comments