Skip to content

Commit 31022af

Browse files
committed
Report short-lived connections are logged
1 parent 691fc72 commit 31022af

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

agent/agent.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ func (a *agent) init(ctx context.Context) {
351351
go a.run(ctx)
352352
if a.statsReporter != nil {
353353
cl, err := a.statsReporter(ctx, a.logger, func() *Stats {
354-
return a.stats.Copy()
354+
ss := a.stats.Copy()
355+
a.stats.Reset()
356+
return ss
355357
})
356358
if err != nil {
357359
a.logger.Error(ctx, "report stats", slog.Error(err))

agent/stats.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ func (s *Stats) Copy() *Stats {
6363
return &ss
6464
}
6565

66+
func (s *Stats) Reset() {
67+
s.Lock()
68+
defer s.Unlock()
69+
70+
for _, ps := range s.ProtocolStats {
71+
atomic.StoreInt64(&ps.NumConns, 0)
72+
atomic.StoreInt64(&ps.RxBytes, 0)
73+
atomic.StoreInt64(&ps.TxBytes, 0)
74+
}
75+
}
76+
6677
// goConn launches a new connection-processing goroutine, account for
6778
// s.Conns in a thread-safe manner.
6879
func (s *Stats) goConn(conn net.Conn, protocol string, fn func(conn net.Conn)) {
@@ -79,17 +90,16 @@ func (s *Stats) goConn(conn net.Conn, protocol string, fn func(conn net.Conn)) {
7990
Conn: conn,
8091
}
8192

82-
go func() {
83-
atomic.AddInt64(&ps.NumConns, 1)
84-
defer func() {
85-
atomic.AddInt64(&ps.NumConns, -1)
86-
}()
87-
88-
fn(cs)
89-
}()
93+
atomic.AddInt64(&ps.NumConns, 1)
94+
go fn(cs)
9095
}
9196

9297
// StatsReporter periodically accept and records agent stats.
98+
// The agent should send incremental stats instead of the cumulative
99+
// value so that SQL queries can efficiently detect activity rates and
100+
// short-lived connections.
101+
//
102+
// E.g., we want to easily query for periods where transfers exceeded 100MB.
93103
type StatsReporter func(
94104
ctx context.Context,
95105
log slog.Logger,

coderd/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func FillEmptyDAUDays(rows []database.GetDAUsFromAgentStatsRow) []database.GetDA
5252
}
5353

5454
func (api *API) daus(rw http.ResponseWriter, r *http.Request) {
55-
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceMetrics) {
55+
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceUser) {
5656
httpapi.Forbidden(rw)
5757
return
5858
}

0 commit comments

Comments
 (0)