MCPcopy Index your code
hub / github.com/coder/coder / collectNetworkStats

Function collectNetworkStats

cli/ssh.go:1629–1666  ·  view source on GitHub ↗
(ctx context.Context, agentConn workspacesdk.AgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts)

Source from the content-addressed store, hash-verified

1627}
1628
1629func collectNetworkStats(ctx context.Context, agentConn workspacesdk.AgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts) (*sshNetworkStats, error) {
1630 latency, p2p, pingResult, err := agentConn.Ping(ctx)
1631 if err != nil {
1632 return nil, err
1633 }
1634 node := agentConn.TailnetConn().Node()
1635 derpMap := agentConn.TailnetConn().DERPMap()
1636
1637 totalRx := uint64(0)
1638 totalTx := uint64(0)
1639 for _, stat := range counts {
1640 totalRx += stat.RxBytes
1641 totalTx += stat.TxBytes
1642 }
1643 // Tracking the time since last request is required because
1644 // ExtractTrafficStats() resets its counters after each call.
1645 dur := end.Sub(start)
1646 uploadSecs := float64(totalTx) / dur.Seconds()
1647 downloadSecs := float64(totalRx) / dur.Seconds()
1648
1649 preferredDerpName := tailnet.ExtractPreferredDERPName(pingResult, node, derpMap)
1650 derpLatency := tailnet.ExtractDERPLatency(node, derpMap)
1651 if _, ok := derpLatency[preferredDerpName]; !ok {
1652 derpLatency[preferredDerpName] = 0
1653 }
1654 derpLatencyMs := maps.Map(derpLatency, func(dur time.Duration) float64 {
1655 return float64(dur) / float64(time.Millisecond)
1656 })
1657
1658 return &sshNetworkStats{
1659 P2P: p2p,
1660 Latency: float64(latency.Microseconds()) / 1000,
1661 PreferredDERP: preferredDerpName,
1662 DERPLatency: derpLatencyMs,
1663 UploadBytesSec: int64(uploadSecs),
1664 DownloadBytesSec: int64(downloadSecs),
1665 }, nil
1666}
1667
1668type coderConnectDialerContextKey struct{}
1669

Callers 1

setStatsCallbackFunction · 0.85

Calls 7

ExtractPreferredDERPNameFunction · 0.92
ExtractDERPLatencyFunction · 0.92
MapFunction · 0.92
PingMethod · 0.65
NodeMethod · 0.65
TailnetConnMethod · 0.65
DERPMapMethod · 0.65

Tested by

no test coverage detected