- Notifications
You must be signed in to change notification settings - Fork923
feat: add --network-info-dir and --network-info-interval flags to coder ssh#16078
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
224 changes: 209 additions & 15 deletionscli/ssh.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,6 +3,7 @@ package cli | ||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
@@ -13,6 +14,7 @@ import ( | ||
"os/exec" | ||
"path/filepath" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
@@ -21,11 +23,14 @@ import ( | ||
"github.com/gofrs/flock" | ||
"github.com/google/uuid" | ||
"github.com/mattn/go-isatty" | ||
"github.com/spf13/afero" | ||
gossh "golang.org/x/crypto/ssh" | ||
gosshagent "golang.org/x/crypto/ssh/agent" | ||
"golang.org/x/term" | ||
"golang.org/x/xerrors" | ||
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" | ||
"tailscale.com/tailcfg" | ||
"tailscale.com/types/netlogtype" | ||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/sloghuman" | ||
@@ -55,19 +60,21 @@ var ( | ||
func (r *RootCmd) ssh() *serpent.Command { | ||
var ( | ||
stdio bool | ||
forwardAgent bool | ||
forwardGPG bool | ||
identityAgent string | ||
wsPollInterval time.Duration | ||
waitEnum string | ||
noWait bool | ||
logDirPath string | ||
remoteForwards []string | ||
env []string | ||
usageApp string | ||
disableAutostart bool | ||
appearanceConfig codersdk.AppearanceConfig | ||
networkInfoDir string | ||
networkInfoInterval time.Duration | ||
) | ||
client := new(codersdk.Client) | ||
cmd := &serpent.Command{ | ||
@@ -284,13 +291,21 @@ func (r *RootCmd) ssh() *serpent.Command { | ||
return err | ||
} | ||
var errCh <-chan error | ||
if networkInfoDir != "" { | ||
errCh, err = setStatsCallback(ctx, conn, logger, networkInfoDir, networkInfoInterval) | ||
bcpeinhardt marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
watchAndClose(ctx, func() error { | ||
stack.close(xerrors.New("watchAndClose")) | ||
return nil | ||
}, logger, client, workspace, errCh) | ||
}() | ||
copier.copy(&wg) | ||
return nil | ||
@@ -312,6 +327,14 @@ func (r *RootCmd) ssh() *serpent.Command { | ||
return err | ||
} | ||
var errCh <-chan error | ||
if networkInfoDir != "" { | ||
errCh, err = setStatsCallback(ctx, conn, logger, networkInfoDir, networkInfoInterval) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
@@ -324,6 +347,7 @@ func (r *RootCmd) ssh() *serpent.Command { | ||
logger, | ||
client, | ||
workspace, | ||
errCh, | ||
) | ||
}() | ||
@@ -540,6 +564,17 @@ func (r *RootCmd) ssh() *serpent.Command { | ||
Value: serpent.StringOf(&usageApp), | ||
Hidden: true, | ||
}, | ||
{ | ||
Flag: "network-info-dir", | ||
Description: "Specifies a directory to write network information periodically.", | ||
Value: serpent.StringOf(&networkInfoDir), | ||
}, | ||
{ | ||
Flag: "network-info-interval", | ||
Description: "Specifies the interval to update network information.", | ||
Default: "5s", | ||
Value: serpent.DurationOf(&networkInfoInterval), | ||
}, | ||
sshDisableAutostartOption(serpent.BoolOf(&disableAutostart)), | ||
} | ||
return cmd | ||
@@ -555,7 +590,7 @@ func (r *RootCmd) ssh() *serpent.Command { | ||
// will usually not propagate. | ||
// | ||
// See: https://github.com/coder/coder/issues/6180 | ||
func watchAndClose(ctx context.Context, closer func() error, logger slog.Logger, client *codersdk.Client, workspace codersdk.Workspace, errCh <-chan error) { | ||
// Ensure session is ended on both context cancellation | ||
// and workspace stop. | ||
defer func() { | ||
@@ -606,6 +641,9 @@ startWatchLoop: | ||
logger.Info(ctx, "workspace stopped") | ||
return | ||
} | ||
case err := <-errCh: | ||
logger.Error(ctx, "failed to collect network stats", slog.Error(err)) | ||
return | ||
} | ||
} | ||
} | ||
@@ -1144,3 +1182,159 @@ func getUsageAppName(usageApp string) codersdk.UsageAppName { | ||
return codersdk.UsageAppNameSSH | ||
} | ||
func setStatsCallback( | ||
ctx context.Context, | ||
agentConn *workspacesdk.AgentConn, | ||
logger slog.Logger, | ||
networkInfoDir string, | ||
networkInfoInterval time.Duration, | ||
) (<-chan error, error) { | ||
fs, ok := ctx.Value("fs").(afero.Fs) | ||
if !ok { | ||
fs = afero.NewOsFs() | ||
} | ||
if err := fs.MkdirAll(networkInfoDir, 0o700); err != nil { | ||
return nil, xerrors.Errorf("mkdir: %w", err) | ||
} | ||
// The VS Code extension obtains the PID of the SSH process to | ||
// read files to display logs and network info. | ||
// | ||
// We get the parent PID because it's assumed `ssh` is calling this | ||
// command via the ProxyCommand SSH option. | ||
pid := os.Getppid() | ||
// The VS Code extension obtains the PID of the SSH process to | ||
// read the file below which contains network information to display. | ||
// | ||
// We get the parent PID because it's assumed `ssh` is calling this | ||
// command via the ProxyCommand SSH option. | ||
networkInfoFilePath := filepath.Join(networkInfoDir, fmt.Sprintf("%d.json", pid)) | ||
var ( | ||
firstErrTime time.Time | ||
errCh = make(chan error, 1) | ||
) | ||
cb := func(start, end time.Time, virtual, _ map[netlogtype.Connection]netlogtype.Counts) { | ||
sendErr := func(tolerate bool, err error) { | ||
logger.Error(ctx, "collect network stats", slog.Error(err)) | ||
// Tolerate up to 1 minute of errors. | ||
if tolerate { | ||
if firstErrTime.IsZero() { | ||
logger.Info(ctx, "tolerating network stats errors for up to 1 minute") | ||
firstErrTime = time.Now() | ||
} | ||
if time.Since(firstErrTime) < time.Minute { | ||
return | ||
} | ||
} | ||
select { | ||
case errCh <- err: | ||
default: | ||
} | ||
} | ||
stats, err := collectNetworkStats(ctx, agentConn, start, end, virtual) | ||
if err != nil { | ||
sendErr(true, err) | ||
return | ||
} | ||
rawStats, err := json.Marshal(stats) | ||
if err != nil { | ||
sendErr(false, err) | ||
return | ||
} | ||
err = afero.WriteFile(fs, networkInfoFilePath, rawStats, 0o600) | ||
if err != nil { | ||
sendErr(false, err) | ||
return | ||
} | ||
firstErrTime = time.Time{} | ||
} | ||
now := time.Now() | ||
cb(now, now.Add(time.Nanosecond), map[netlogtype.Connection]netlogtype.Counts{}, map[netlogtype.Connection]netlogtype.Counts{}) | ||
agentConn.SetConnStatsCallback(networkInfoInterval, 2048, cb) | ||
return errCh, nil | ||
} | ||
type sshNetworkStats struct { | ||
P2P bool `json:"p2p"` | ||
Latency float64 `json:"latency"` | ||
PreferredDERP string `json:"preferred_derp"` | ||
DERPLatency map[string]float64 `json:"derp_latency"` | ||
UploadBytesSec int64 `json:"upload_bytes_sec"` | ||
DownloadBytesSec int64 `json:"download_bytes_sec"` | ||
} | ||
func collectNetworkStats(ctx context.Context, agentConn *workspacesdk.AgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts) (*sshNetworkStats, error) { | ||
latency, p2p, pingResult, err := agentConn.Ping(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
node := agentConn.Node() | ||
derpMap := agentConn.DERPMap() | ||
derpLatency := map[string]float64{} | ||
// Convert DERP region IDs to friendly names for display in the UI. | ||
for rawRegion, latency := range node.DERPLatency { | ||
regionParts := strings.SplitN(rawRegion, "-", 2) | ||
regionID, err := strconv.Atoi(regionParts[0]) | ||
if err != nil { | ||
continue | ||
} | ||
region, found := derpMap.Regions[regionID] | ||
if !found { | ||
// It's possible that a workspace agent is using an old DERPMap | ||
// and reports regions that do not exist. If that's the case, | ||
// report the region as unknown! | ||
region = &tailcfg.DERPRegion{ | ||
RegionID: regionID, | ||
RegionName: fmt.Sprintf("Unnamed %d", regionID), | ||
} | ||
} | ||
// Convert the microseconds to milliseconds. | ||
derpLatency[region.RegionName] = latency * 1000 | ||
} | ||
totalRx := uint64(0) | ||
totalTx := uint64(0) | ||
for _, stat := range counts { | ||
totalRx += stat.RxBytes | ||
totalTx += stat.TxBytes | ||
} | ||
// Tracking the time since last request is required because | ||
// ExtractTrafficStats() resets its counters after each call. | ||
dur := end.Sub(start) | ||
uploadSecs := float64(totalTx) / dur.Seconds() | ||
downloadSecs := float64(totalRx) / dur.Seconds() | ||
// Sometimes the preferred DERP doesn't match the one we're actually | ||
// connected with. Perhaps because the agent prefers a different DERP and | ||
// we're using that server instead. | ||
preferredDerpID := node.PreferredDERP | ||
if pingResult.DERPRegionID != 0 { | ||
preferredDerpID = pingResult.DERPRegionID | ||
} | ||
preferredDerp, ok := derpMap.Regions[preferredDerpID] | ||
preferredDerpName := fmt.Sprintf("Unnamed %d", preferredDerpID) | ||
if ok { | ||
preferredDerpName = preferredDerp.RegionName | ||
} | ||
if _, ok := derpLatency[preferredDerpName]; !ok { | ||
derpLatency[preferredDerpName] = 0 | ||
} | ||
return &sshNetworkStats{ | ||
P2P: p2p, | ||
Latency: float64(latency.Microseconds()) / 1000, | ||
PreferredDERP: preferredDerpName, | ||
DERPLatency: derpLatency, | ||
UploadBytesSec: int64(uploadSecs), | ||
DownloadBytesSec: int64(downloadSecs), | ||
}, nil | ||
} |
73 changes: 73 additions & 0 deletionscli/ssh_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.