Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 4de99d5

Browse files
authored
Add logs to wsnet listener (#388)
1 parent 1294dd9 commit 4de99d5

File tree

5 files changed

+199
-188
lines changed

5 files changed

+199
-188
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
2424
github.com/rjeczalik/notify v0.9.2
2525
github.com/spf13/cobra v1.2.1
26+
github.com/stretchr/testify v1.7.0
2627
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
2728
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
2829
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015

internal/cmd/agent.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cmd
22

33
import (
4-
"context"
5-
"log"
64
"net/url"
75
"os"
86
"os/signal"
97
"syscall"
108

9+
// We use slog here since agent runs in the background and we can benefit
10+
// from structured logging.
11+
"cdr.dev/slog"
12+
"cdr.dev/slog/sloggers/sloghuman"
1113
"github.com/spf13/cobra"
1214
"golang.org/x/xerrors"
1315

@@ -46,7 +48,10 @@ coder agent start
4648
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
4749
`,
4850
RunE: func(cmd *cobra.Command, args []string) error {
49-
ctx := cmd.Context()
51+
var (
52+
ctx = cmd.Context()
53+
log = slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug)
54+
)
5055
if coderURL == "" {
5156
var ok bool
5257
coderURL, ok = os.LookupEnv("CODER_URL")
@@ -73,20 +78,23 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
7378
}
7479
}
7580

76-
listener, err := wsnet.Listen(context.Background(), wsnet.ListenEndpoint(u, token), token)
81+
log.Info(ctx, "starting wsnet listener", slog.F("coder_access_url", u.String()))
82+
listener, err := wsnet.Listen(ctx, log, wsnet.ListenEndpoint(u, token), token)
7783
if err != nil {
7884
return xerrors.Errorf("listen: %w", err)
7985
}
86+
defer func() {
87+
err := listener.Close()
88+
if err != nil {
89+
log.Error(ctx, "close listener", slog.Error(err))
90+
}
91+
}()
8092

8193
// Block until user sends SIGINT or SIGTERM
8294
sigs := make(chan os.Signal, 1)
8395
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
8496
<-sigs
8597

86-
if err = listener.Close(); err != nil {
87-
log.Panic(err)
88-
}
89-
9098
return nil
9199
},
92100
}

0 commit comments

Comments
 (0)