Skip to content

feat(agent): add http debug routes for magicsock #7287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 29 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Options struct {
ReconnectingPTYTimeout time.Duration
EnvironmentVariables map[string]string
Logger slog.Logger
AgentPorts map[int]string
IgnorePorts map[int]string
SSHMaxTimeout time.Duration
TailnetListenPort uint16
}
Expand All @@ -76,7 +76,12 @@ type Client interface {
PatchStartupLogs(ctx context.Context, req agentsdk.PatchStartupLogs) error
}

func New(options Options) io.Closer {
type Agent interface {
HTTPDebug() http.Handler
io.Closer
}

func New(options Options) Agent {
if options.ReconnectingPTYTimeout == 0 {
options.ReconnectingPTYTimeout = 5 * time.Minute
}
Expand Down Expand Up @@ -112,7 +117,7 @@ func New(options Options) io.Closer {
tempDir: options.TempDir,
lifecycleUpdate: make(chan struct{}, 1),
lifecycleReported: make(chan codersdk.WorkspaceAgentLifecycle, 1),
ignorePorts: options.AgentPorts,
ignorePorts: options.IgnorePorts,
connStatsChan: make(chan *agentsdk.Stats, 1),
sshMaxTimeout: options.SSHMaxTimeout,
}
Expand Down Expand Up @@ -1267,6 +1272,27 @@ func (a *agent) isClosed() bool {
}
}

func (a *agent) HTTPDebug() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.closeMutex.Lock()
network := a.network
a.closeMutex.Unlock()

if network == nil {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("network is not ready yet"))
return
}

if r.URL.Path == "/debug/magicsock" {
network.MagicsockServeHTTPDebug(w, r)
} else {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("404 not found"))
}
})
}

func (a *agent) Close() error {
a.closeMutex.Lock()
defer a.closeMutex.Unlock()
Expand Down
28 changes: 22 additions & 6 deletions cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
sshMaxTimeout time.Duration
tailnetListenPort int64
prometheusAddress string
debugAddress string
)
cmd := &clibase.Cmd{
Use: "agent",
Expand All @@ -48,7 +49,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
ctx, cancel := context.WithCancel(inv.Context())
defer cancel()

agentPorts := map[int]string{}
ignorePorts := map[int]string{}

isLinux := runtime.GOOS == "linux"

Expand Down Expand Up @@ -125,14 +126,14 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
defer pprofSrvClose()
// Do a best effort here. If this fails, it's not a big deal.
if port, err := urlPort(pprofAddress); err == nil {
agentPorts[port] = "pprof"
ignorePorts[port] = "pprof"
}

prometheusSrvClose := ServeHandler(ctx, logger, prometheusMetricsHandler(), prometheusAddress, "prometheus")
defer prometheusSrvClose()
// Do a best effort here. If this fails, it's not a big deal.
if port, err := urlPort(prometheusAddress); err == nil {
agentPorts[port] = "prometheus"
ignorePorts[port] = "prometheus"
}

// exchangeToken returns a session token.
Expand Down Expand Up @@ -196,7 +197,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
return xerrors.Errorf("add executable to $PATH: %w", err)
}

closer := agent.New(agent.Options{
agnt := agent.New(agent.Options{
Client: client,
Logger: logger,
LogDir: logDir,
Expand All @@ -215,11 +216,19 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
EnvironmentVariables: map[string]string{
"GIT_ASKPASS": executablePath,
},
AgentPorts: agentPorts,
IgnorePorts: ignorePorts,
SSHMaxTimeout: sshMaxTimeout,
})

debugSrvClose := ServeHandler(ctx, logger, agnt.HTTPDebug(), debugAddress, "debug")
defer debugSrvClose()
// Do a best effort here. If this fails, it's not a big deal.
if port, err := urlPort(debugAddress); err == nil {
ignorePorts[port] = "debug"
}

<-ctx.Done()
return closer.Close()
return agnt.Close()
},
}

Expand Down Expand Up @@ -273,6 +282,13 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
Value: clibase.StringOf(&prometheusAddress),
Description: "The bind address to serve Prometheus metrics.",
},
{
Flag: "debug-address",
Default: "127.0.0.1:2113",
Env: "CODER_AGENT_DEBUG_ADDRESS",
Value: clibase.StringOf(&debugAddress),
Description: "The bind address to serve a debug HTTP server.",
},
}

return cmd
Expand Down
4 changes: 4 additions & 0 deletions tailnet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ func (c *Conn) SetConnStatsCallback(maxPeriod time.Duration, maxConns int, dump
c.tunDevice.SetStatistics(connStats)
}

func (c *Conn) MagicsockServeHTTPDebug(w http.ResponseWriter, r *http.Request) {
c.magicConn.ServeHTTPDebug(w, r)
}

type listenKey struct {
network string
host string
Expand Down