Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05e4499

Browse files
authored
chore: convert agent stats to use a table (#6374)
* chore: convert workspace agent stats from json to table * chore: convert agent stats to use a table Backwards compatibility becomes hard when all agent stats are in a JSON blob. We also want to query this table for new agents that are failing health checks so we can display it in the UI. * Fix migration using default values
1 parent 7cf1e20 commit 05e4499

22 files changed

+307
-235
lines changed

agent/agent.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,12 @@ func (a *agent) init(ctx context.Context) {
767767

768768
func convertAgentStats(counts map[netlogtype.Connection]netlogtype.Counts) *agentsdk.Stats {
769769
stats := &agentsdk.Stats{
770-
ConnsByProto: map[string]int64{},
771-
NumConns: int64(len(counts)),
770+
ConnectionsByProto: map[string]int64{},
771+
ConnectionCount: int64(len(counts)),
772772
}
773773

774774
for conn, count := range counts {
775-
stats.ConnsByProto[conn.Proto.String()]++
775+
stats.ConnectionsByProto[conn.Proto.String()]++
776776
stats.RxPackets += int64(count.RxPackets)
777777
stats.RxBytes += int64(count.RxBytes)
778778
stats.TxPackets += int64(count.TxPackets)

agent/agent_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestAgent_Stats_SSH(t *testing.T) {
7373
require.Eventuallyf(t, func() bool {
7474
var ok bool
7575
s, ok = <-stats
76-
return ok && s.NumConns > 0 && s.RxBytes > 0 && s.TxBytes > 0
76+
return ok && s.ConnectionCount > 0 && s.RxBytes > 0 && s.TxBytes > 0
7777
}, testutil.WaitLong, testutil.IntervalFast,
7878
"never saw stats: %+v", s,
7979
)
@@ -102,7 +102,7 @@ func TestAgent_Stats_ReconnectingPTY(t *testing.T) {
102102
require.Eventuallyf(t, func() bool {
103103
var ok bool
104104
s, ok = <-stats
105-
return ok && s.NumConns > 0 && s.RxBytes > 0 && s.TxBytes > 0
105+
return ok && s.ConnectionCount > 0 && s.RxBytes > 0 && s.TxBytes > 0
106106
}, testutil.WaitLong, testutil.IntervalFast,
107107
"never saw stats: %+v", s,
108108
)

coderd/apidoc/docs.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbauthz/querier.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,18 +1413,18 @@ func (q *querier) UpdateWorkspaceAgentConnectionByID(ctx context.Context, arg da
14131413
return update(q.log, q.auth, fetch, q.db.UpdateWorkspaceAgentConnectionByID)(ctx, arg)
14141414
}
14151415

1416-
func (q *querier) InsertAgentStat(ctx context.Context, arg database.InsertAgentStatParams) (database.AgentStat, error) {
1416+
func (q *querier) InsertWorkspaceAgentStat(ctx context.Context, arg database.InsertWorkspaceAgentStatParams) (database.WorkspaceAgentStat, error) {
14171417
// TODO: This is a workspace agent operation. Should users be able to query this?
14181418
// Not really sure what this is for.
14191419
workspace, err := q.db.GetWorkspaceByID(ctx, arg.WorkspaceID)
14201420
if err != nil {
1421-
return database.AgentStat{}, err
1421+
return database.WorkspaceAgentStat{}, err
14221422
}
14231423
err = q.authorizeContext(ctx, rbac.ActionUpdate, workspace)
14241424
if err != nil {
1425-
return database.AgentStat{}, err
1425+
return database.WorkspaceAgentStat{}, err
14261426
}
1427-
return q.db.InsertAgentStat(ctx, arg)
1427+
return q.db.InsertWorkspaceAgentStat(ctx, arg)
14281428
}
14291429

14301430
func (q *querier) UpdateWorkspaceAppHealthByID(ctx context.Context, arg database.UpdateWorkspaceAppHealthByIDParams) error {

coderd/database/dbauthz/querier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,9 @@ func (s *MethodTestSuite) TestWorkspace() {
11701170
ID: agt.ID,
11711171
}).Asserts(ws, rbac.ActionUpdate).Returns()
11721172
}))
1173-
s.Run("InsertAgentStat", s.Subtest(func(db database.Store, check *expects) {
1173+
s.Run("InsertWorkspaceAgentStat", s.Subtest(func(db database.Store, check *expects) {
11741174
ws := dbgen.Workspace(s.T(), db, database.Workspace{})
1175-
check.Args(database.InsertAgentStatParams{
1175+
check.Args(database.InsertWorkspaceAgentStatParams{
11761176
WorkspaceID: ws.ID,
11771177
}).Asserts(ws, rbac.ActionUpdate)
11781178
}))

coderd/database/dbauthz/system.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ func (q *querier) GetWorkspaceResourceMetadataCreatedAfter(ctx context.Context,
197197
return q.db.GetWorkspaceResourceMetadataCreatedAfter(ctx, createdAt)
198198
}
199199

200-
func (q *querier) DeleteOldAgentStats(ctx context.Context) error {
201-
return q.db.DeleteOldAgentStats(ctx)
200+
func (q *querier) DeleteOldWorkspaceAgentStats(ctx context.Context) error {
201+
return q.db.DeleteOldWorkspaceAgentStats(ctx)
202202
}
203203

204204
func (q *querier) GetParameterSchemasCreatedAfter(ctx context.Context, createdAt time.Time) ([]database.ParameterSchema, error) {

coderd/database/dbauthz/system_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
128128
_ = dbgen.WorkspaceResourceMetadatums(s.T(), db, database.WorkspaceResourceMetadatum{})
129129
check.Args(time.Now()).Asserts()
130130
}))
131-
s.Run("DeleteOldAgentStats", s.Subtest(func(db database.Store, check *expects) {
131+
s.Run("DeleteOldWorkspaceAgentStats", s.Subtest(func(db database.Store, check *expects) {
132132
check.Args().Asserts()
133133
}))
134134
s.Run("GetParameterSchemasCreatedAfter", s.Subtest(func(db database.Store, check *expects) {

coderd/database/dbfake/databasefake.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func New() database.Store {
4040
mutex: &sync.RWMutex{},
4141
data: &data{
4242
apiKeys: make([]database.APIKey, 0),
43-
agentStats: make([]database.AgentStat, 0),
4443
organizationMembers: make([]database.OrganizationMember, 0),
4544
organizations: make([]database.Organization, 0),
4645
users: make([]database.User, 0),
@@ -60,6 +59,7 @@ func New() database.Store {
6059
provisionerJobs: make([]database.ProvisionerJob, 0),
6160
templateVersions: make([]database.TemplateVersion, 0),
6261
templates: make([]database.Template, 0),
62+
workspaceAgentStats: make([]database.WorkspaceAgentStat, 0),
6363
workspaceBuilds: make([]database.WorkspaceBuild, 0),
6464
workspaceApps: make([]database.WorkspaceApp, 0),
6565
workspaces: make([]database.Workspace, 0),
@@ -98,7 +98,7 @@ type data struct {
9898
userLinks []database.UserLink
9999

100100
// New tables
101-
agentStats []database.AgentStat
101+
workspaceAgentStats []database.WorkspaceAgentStat
102102
auditLogs []database.AuditLog
103103
files []database.File
104104
gitAuthLinks []database.GitAuthLink
@@ -258,29 +258,34 @@ func (q *fakeQuerier) AcquireProvisionerJob(_ context.Context, arg database.Acqu
258258
return database.ProvisionerJob{}, sql.ErrNoRows
259259
}
260260

261-
func (*fakeQuerier) DeleteOldAgentStats(_ context.Context) error {
261+
func (*fakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
262262
// no-op
263263
return nil
264264
}
265265

266-
func (q *fakeQuerier) InsertAgentStat(_ context.Context, p database.InsertAgentStatParams) (database.AgentStat, error) {
266+
func (q *fakeQuerier) InsertWorkspaceAgentStat(_ context.Context, p database.InsertWorkspaceAgentStatParams) (database.WorkspaceAgentStat, error) {
267267
if err := validateDatabaseType(p); err != nil {
268-
return database.AgentStat{}, err
268+
return database.WorkspaceAgentStat{}, err
269269
}
270270

271271
q.mutex.Lock()
272272
defer q.mutex.Unlock()
273273

274-
stat := database.AgentStat{
275-
ID: p.ID,
276-
CreatedAt: p.CreatedAt,
277-
WorkspaceID: p.WorkspaceID,
278-
AgentID: p.AgentID,
279-
UserID: p.UserID,
280-
Payload: p.Payload,
281-
TemplateID: p.TemplateID,
282-
}
283-
q.agentStats = append(q.agentStats, stat)
274+
stat := database.WorkspaceAgentStat{
275+
ID: p.ID,
276+
CreatedAt: p.CreatedAt,
277+
WorkspaceID: p.WorkspaceID,
278+
AgentID: p.AgentID,
279+
UserID: p.UserID,
280+
ConnectionsByProto: p.ConnectionsByProto,
281+
ConnectionCount: p.ConnectionCount,
282+
RxPackets: p.RxPackets,
283+
RxBytes: p.RxBytes,
284+
TxPackets: p.TxPackets,
285+
TxBytes: p.TxBytes,
286+
TemplateID: p.TemplateID,
287+
}
288+
q.workspaceAgentStats = append(q.workspaceAgentStats, stat)
284289
return stat, nil
285290
}
286291

@@ -290,7 +295,7 @@ func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) (
290295

291296
seens := make(map[time.Time]map[uuid.UUID]struct{})
292297

293-
for _, as := range q.agentStats {
298+
for _, as := range q.workspaceAgentStats {
294299
if as.TemplateID != templateID {
295300
continue
296301
}
@@ -330,7 +335,7 @@ func (q *fakeQuerier) GetDeploymentDAUs(_ context.Context) ([]database.GetDeploy
330335

331336
seens := make(map[time.Time]map[uuid.UUID]struct{})
332337

333-
for _, as := range q.agentStats {
338+
for _, as := range q.workspaceAgentStats {
334339
date := as.CreatedAt.Truncate(time.Hour * 24)
335340

336341
dateEntry := seens[date]

coderd/database/dump.sql

Lines changed: 18 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ALTER TABLE workspace_agent_stats RENAME TO agent_stats;
2+
3+
ALTER TABLE agent_stats ADD COLUMN payload jsonb NOT NULL DEFAULT '{}'::jsonb;
4+
ALTER TABLE agent_stats DROP COLUMN connections_by_proto,
5+
DROP COLUMN connection_count,
6+
DROP COLUMN rx_packets,
7+
DROP COLUMN rx_bytes,
8+
DROP COLUMN tx_packets,
9+
DROP COLUMN tx_bytes;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ALTER TABLE agent_stats RENAME TO workspace_agent_stats;
2+
3+
ALTER TABLE workspace_agent_stats ADD COLUMN connections_by_proto jsonb NOT NULL DEFAULT '{}'::jsonb;
4+
ALTER TABLE workspace_agent_stats ADD COLUMN connection_count integer DEFAULT 0 NOT NULL;
5+
ALTER TABLE workspace_agent_stats ADD COLUMN rx_packets integer DEFAULT 0 NOT NULL;
6+
ALTER TABLE workspace_agent_stats ADD COLUMN rx_bytes integer DEFAULT 0 NOT NULL;
7+
ALTER TABLE workspace_agent_stats ADD COLUMN tx_packets integer DEFAULT 0 NOT NULL;
8+
ALTER TABLE workspace_agent_stats ADD COLUMN tx_bytes integer DEFAULT 0 NOT NULL;
9+
10+
UPDATE workspace_agent_stats SET
11+
connections_by_proto = coalesce((payload ->> 'conns_by_proto')::jsonb, '{}'::jsonb),
12+
connection_count = coalesce((payload ->> 'num_conns')::integer, 0),
13+
rx_packets = coalesce((payload ->> 'rx_packets')::integer, 0),
14+
rx_bytes = coalesce((payload ->> 'rx_bytes')::integer, 0),
15+
tx_packets = coalesce((payload ->> 'tx_packets')::integer, 0),
16+
tx_bytes = coalesce((payload ->> 'tx_bytes')::integer, 0);
17+
18+
ALTER TABLE workspace_agent_stats DROP COLUMN payload;

coderd/database/models.go

Lines changed: 15 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)