Skip to content

Commit 9bd442c

Browse files
committed
Avoid duplicate inserts to preserve DB space
1 parent 31022af commit 9bd442c

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

agent/agent.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,7 @@ 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-
ss := a.stats.Copy()
355-
a.stats.Reset()
356-
return ss
354+
return a.stats.Copy()
357355
})
358356
if err != nil {
359357
a.logger.Error(ctx, "report stats", slog.Error(err))

agent/stats.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@ 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-
7766
// goConn launches a new connection-processing goroutine, account for
7867
// s.Conns in a thread-safe manner.
7968
func (s *Stats) goConn(conn net.Conn, protocol string, fn func(conn net.Conn)) {
@@ -91,15 +80,12 @@ func (s *Stats) goConn(conn net.Conn, protocol string, fn func(conn net.Conn)) {
9180
}
9281

9382
atomic.AddInt64(&ps.NumConns, 1)
94-
go fn(cs)
83+
go func() {
84+
fn(cs)
85+
}()
9586
}
9687

9788
// 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.
10389
type StatsReporter func(
10490
ctx context.Context,
10591
log slog.Logger,

coderd/metrics.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"net/http"
66
"os"
7+
"reflect"
78
"strconv"
89
"time"
910

@@ -19,8 +20,6 @@ import (
1920
"github.com/coder/coder/codersdk"
2021
)
2122

22-
const AgentStatIntervalEnv = "CODER_AGENT_STAT_INTERVAL"
23-
2423
func FillEmptyDAUDays(rows []database.GetDAUsFromAgentStatsRow) []database.GetDAUsFromAgentStatsRow {
2524
var newRows []database.GetDAUsFromAgentStatsRow
2625

@@ -77,6 +76,8 @@ func (api *API) daus(rw http.ResponseWriter, r *http.Request) {
7776
httpapi.Write(rw, http.StatusOK, resp)
7877
}
7978

79+
const AgentStatIntervalEnv = "CODER_AGENT_STAT_INTERVAL"
80+
8081
func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Request) {
8182
api.websocketWaitMutex.Lock()
8283
api.websocketWaitGroup.Add(1)
@@ -123,6 +124,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
123124
}
124125
defer conn.Close(websocket.StatusAbnormalClosure, "")
125126

127+
// The complexity of the DAU query is inversely proportional to this interval.
126128
var interval = time.Minute
127129

128130
// Allow overriding the stat interval for debugging and testing purposes.
@@ -141,6 +143,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
141143

142144
ctx := r.Context()
143145
timer := time.NewTicker(interval)
146+
var lastReport codersdk.AgentStatsReportResponse
144147
for {
145148
err := wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
146149
if err != nil {
@@ -170,15 +173,20 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
170173
return
171174
}
172175

176+
// Avoid inserting duplicate rows to preserve DB space.
177+
var insert = !reflect.DeepEqual(lastReport, rep)
178+
173179
api.Logger.Debug(ctx, "read stats report",
174180
slog.F("agent", workspaceAgent.ID),
175181
slog.F("resource", resource.ID),
176182
slog.F("workspace", workspace.ID),
183+
slog.F("insert", insert),
177184
slog.F("payload", rep),
178185
)
179186

180-
// Avoid inserting empty rows to preserve DB space.
181-
if len(rep.ProtocolStats) > 0 {
187+
if insert {
188+
lastReport = rep
189+
182190
_, err = api.Database.InsertAgentStat(ctx, database.InsertAgentStatParams{
183191
ID: uuid.NewString(),
184192
CreatedAt: time.Now(),

0 commit comments

Comments
 (0)