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

Method Collect

agent/agent.go:2005–2096  ·  view source on GitHub ↗

Collect collects additional stats from the agent

(ctx context.Context, networkStats map[netlogtype.Connection]netlogtype.Counts)

Source from the content-addressed store, hash-verified

2003
2004// Collect collects additional stats from the agent
2005func (a *agent) Collect(ctx context.Context, networkStats map[netlogtype.Connection]netlogtype.Counts) *proto.Stats {
2006 a.logger.Debug(context.Background(), "computing stats report")
2007 stats := &proto.Stats{
2008 ConnectionCount: int64(len(networkStats)),
2009 ConnectionsByProto: map[string]int64{},
2010 }
2011 for conn, counts := range networkStats {
2012 stats.ConnectionsByProto[conn.Proto.String()]++
2013 // #nosec G115 - Safe conversions for network statistics which we expect to be within int64 range
2014 stats.RxBytes += int64(counts.RxBytes)
2015 // #nosec G115 - Safe conversions for network statistics which we expect to be within int64 range
2016 stats.RxPackets += int64(counts.RxPackets)
2017 // #nosec G115 - Safe conversions for network statistics which we expect to be within int64 range
2018 stats.TxBytes += int64(counts.TxBytes)
2019 // #nosec G115 - Safe conversions for network statistics which we expect to be within int64 range
2020 stats.TxPackets += int64(counts.TxPackets)
2021 }
2022
2023 // The count of active sessions.
2024 sshStats := a.sshServer.ConnStats()
2025 stats.SessionCountSsh = sshStats.Sessions
2026 stats.SessionCountVscode = sshStats.VSCode
2027 stats.SessionCountJetbrains = sshStats.JetBrains
2028
2029 stats.SessionCountReconnectingPty = a.reconnectingPTYServer.ConnCount()
2030
2031 // Compute the median connection latency!
2032 a.logger.Debug(ctx, "starting peer latency measurement for stats")
2033 var wg sync.WaitGroup
2034 var mu sync.Mutex
2035 status := a.network.Status()
2036 durations := []float64{}
2037 p2pConns := 0
2038 derpConns := 0
2039 pingCtx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
2040 defer cancelFunc()
2041 for nodeID, peer := range status.Peer {
2042 if !peer.Active {
2043 continue
2044 }
2045 addresses, found := a.network.NodeAddresses(nodeID)
2046 if !found {
2047 continue
2048 }
2049 if len(addresses) == 0 {
2050 continue
2051 }
2052 wg.Add(1)
2053 go func() {
2054 defer wg.Done()
2055 duration, p2p, _, err := a.network.Ping(pingCtx, addresses[0].Addr())
2056 if err != nil {
2057 return
2058 }
2059 mu.Lock()
2060 defer mu.Unlock()
2061 durations = append(durations, float64(duration.Microseconds()))
2062 if p2p {

Callers

nothing calls this directly

Calls 15

collectMetricsMethod · 0.95
ConnStatsMethod · 0.80
ConnCountMethod · 0.80
NodeAddressesMethod · 0.80
WithLabelValuesMethod · 0.80
AddMethod · 0.65
PingMethod · 0.65
WaitMethod · 0.65
SetMethod · 0.65
StringMethod · 0.45
StatusMethod · 0.45
DoneMethod · 0.45

Tested by

no test coverage detected