@@ -296,7 +296,7 @@ func (api *API) updaterLoop() {
296296if err := api .cleanupSubAgents (api .ctx );err != nil {
297297api .logger .Error (api .ctx ,"cleanup subagents failed" ,slog .Error (err ))
298298}else {
299- api .logger .Debug (api .ctx ,"subagent cleanup complete" )
299+ api .logger .Debug (api .ctx ,"cleanup subagents complete" )
300300}
301301
302302// Perform an initial update to populate the container list, this
@@ -440,6 +440,20 @@ func (api *API) updateContainers(ctx context.Context) error {
440440// on the latest list of containers. This method assumes that api.mu is
441441// held.
442442func (api * API )processUpdatedContainersLocked (ctx context.Context ,updated codersdk.WorkspaceAgentListContainersResponse ) {
443+ dcFields := func (dc codersdk.WorkspaceAgentDevcontainer ) []slog.Field {
444+ f := []slog.Field {
445+ slog .F ("devcontainer_id" ,dc .ID ),
446+ slog .F ("devcontainer_name" ,dc .Name ),
447+ slog .F ("workspace_folder" ,dc .WorkspaceFolder ),
448+ slog .F ("config_path" ,dc .ConfigPath ),
449+ }
450+ if dc .Container != nil {
451+ f = append (f ,slog .F ("container_id" ,dc .Container .ID ))
452+ f = append (f ,slog .F ("container_name" ,dc .Container .FriendlyName ))
453+ }
454+ return f
455+ }
456+
443457// Reset the container links in known devcontainers to detect if
444458// they still exist.
445459for _ ,dc := range api .knownDevcontainers {
@@ -460,6 +474,13 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
460474continue
461475}
462476
477+ logger := api .logger .With (
478+ slog .F ("container_id" ,updated .Containers [i ].ID ),
479+ slog .F ("container_name" ,updated .Containers [i ].FriendlyName ),
480+ slog .F ("workspace_folder" ,workspaceFolder ),
481+ slog .F ("config_file" ,configFile ),
482+ )
483+
463484if dc ,ok := api .knownDevcontainers [workspaceFolder ];ok {
464485// If no config path is set, this devcontainer was defined
465486// in Terraform without the optional config file. Assume the
@@ -468,7 +489,7 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
468489if dc .ConfigPath == "" && configFile != "" {
469490dc .ConfigPath = configFile
470491if err := api .watcher .Add (configFile );err != nil {
471- api . logger .Error (ctx ,"watch devcontainer config file failed" ,slog .Error (err ), slog . F ( "file" , configFile ))
492+ logger .With ( dcFields ( dc ) ... ). Error (ctx ,"watch devcontainer config file failed" ,slog .Error (err ))
472493}
473494}
474495
@@ -477,13 +498,7 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
477498continue
478499}
479500
480- if configFile != "" {
481- if err := api .watcher .Add (configFile );err != nil {
482- api .logger .Error (ctx ,"watch devcontainer config file failed" ,slog .Error (err ),slog .F ("file" ,configFile ))
483- }
484- }
485-
486- api .knownDevcontainers [workspaceFolder ]= codersdk.WorkspaceAgentDevcontainer {
501+ dc := codersdk.WorkspaceAgentDevcontainer {
487502ID :uuid .New (),
488503Name :"" ,// Updated later based on container state.
489504WorkspaceFolder :workspaceFolder ,
@@ -492,11 +507,21 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
492507Dirty :false ,// Updated later based on config file changes.
493508Container :container ,
494509}
510+
511+ if configFile != "" {
512+ if err := api .watcher .Add (configFile );err != nil {
513+ logger .With (dcFields (dc )... ).Error (ctx ,"watch devcontainer config file failed" ,slog .Error (err ))
514+ }
515+ }
516+
517+ api .knownDevcontainers [workspaceFolder ]= dc
495518}
496519
497520// Iterate through all known devcontainers and update their status
498521// based on the current state of the containers.
499522for _ ,dc := range api .knownDevcontainers {
523+ logger := api .logger .With (dcFields (dc )... )
524+
500525if dc .Container != nil {
501526if ! api .devcontainerNames [dc .Name ] {
502527// If the devcontainer name wasn't set via terraform, we
@@ -532,7 +557,7 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
532557if _ ,injected := api .injectedSubAgentProcs [dc .Container .ID ];! injected && dc .Status == codersdk .WorkspaceAgentDevcontainerStatusRunning {
533558err := api .injectSubAgentIntoContainerLocked (ctx ,dc )
534559if err != nil {
535- api . logger .Error (ctx ,"inject subagent into container failed" ,slog .Error (err ))
560+ logger .Error (ctx ,"inject subagent into container failed" ,slog .Error (err ))
536561}
537562}
538563