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

Add logs to wsnet listener #388

Merged
merged 4 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/rjeczalik/notify v0.9.2
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015
Expand Down
24 changes: 16 additions & 8 deletions internal/cmd/agent.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cmd

import (
"context"
"log"
"net/url"
"os"
"os/signal"
"syscall"

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

Expand Down Expand Up @@ -46,7 +48,10 @@ coder agent start
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
var (
ctx = cmd.Context()
log = slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug)
)
if coderURL == "" {
var ok bool
coderURL, ok = os.LookupEnv("CODER_URL")
Expand All @@ -73,20 +78,23 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
}
}

listener, err := wsnet.Listen(context.Background(), wsnet.ListenEndpoint(u, token), token)
log.Info(ctx, "starting wsnet listener", slog.F("coder_access_url", u.String()))
listener, err := wsnet.Listen(ctx, log, wsnet.ListenEndpoint(u, token), token)
if err != nil {
return xerrors.Errorf("listen: %w", err)
}
defer func() {
err := listener.Close()
if err != nil {
log.Error(ctx, "close listener", slog.Error(err))
}
}()

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

if err = listener.Close(); err != nil {
log.Panic(err)
}

return nil
},
}
Expand Down
Loading