Skip to content

Commit f62203c

Browse files
committed
connect usage flag to metrics cache
1 parent 66b94e7 commit f62203c

File tree

12 files changed

+85
-44
lines changed

12 files changed

+85
-44
lines changed

coderd/agentapi/stats.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
7474
workspaceAgent,
7575
getWorkspaceAgentByIDRow.TemplateName,
7676
req.Stats,
77+
false,
7778
)
7879
if err != nil {
7980
return nil, xerrors.Errorf("report agent stats: %w", err)

coderd/coderd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,16 @@ func New(options *Options) *API {
421421
panic(xerrors.Errorf("read site bin failed: %w", err))
422422
}
423423

424+
experiments.Enabled(codersdk.ExperimentWorkspaceUsage)
425+
424426
metricsCache := metricscache.New(
425427
options.Database,
426428
options.Logger.Named("metrics_cache"),
427429
metricscache.Intervals{
428430
TemplateBuildTimes: options.MetricsCacheRefreshInterval,
429431
DeploymentStats: options.AgentStatsRefreshInterval,
430432
},
433+
experiments.Enabled(codersdk.ExperimentWorkspaceUsage),
431434
)
432435

433436
oauthConfigs := &httpmw.OAuth2Configs{

coderd/database/queries.sql.go

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

coderd/database/queries/workspaceagentstats.sql

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ INSERT INTO
1717
session_count_jetbrains,
1818
session_count_reconnecting_pty,
1919
session_count_ssh,
20-
connection_median_latency_ms
20+
connection_median_latency_ms,
21+
usage
2122
)
2223
SELECT
2324
unnest(@id :: uuid[]) AS id,
@@ -36,7 +37,8 @@ SELECT
3637
unnest(@session_count_jetbrains :: bigint[]) AS session_count_jetbrains,
3738
unnest(@session_count_reconnecting_pty :: bigint[]) AS session_count_reconnecting_pty,
3839
unnest(@session_count_ssh :: bigint[]) AS session_count_ssh,
39-
unnest(@connection_median_latency_ms :: double precision[]) AS connection_median_latency_ms;
40+
unnest(@connection_median_latency_ms :: double precision[]) AS connection_median_latency_ms,
41+
unnest(@usage :: boolean[]) AS usage;
4042

4143
-- name: GetTemplateDAUs :many
4244
SELECT
@@ -135,14 +137,15 @@ minute_buckets AS (
135137
agent_id,
136138
date_trunc('minute', created_at) AS minute_bucket,
137139
SUM(session_count_vscode) AS session_count_vscode,
140+
SUM(session_count_ssh) AS session_count_ssh,
138141
SUM(session_count_jetbrains) AS session_count_jetbrains,
139-
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
140-
SUM(session_count_ssh) AS session_count_ssh
142+
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
141143
FROM
142144
workspace_agent_stats
143145
WHERE
144146
created_at >= $1
145147
AND created_at < date_trunc('minute', now()) -- Exclude current partial minute
148+
AND usage = true
146149
GROUP BY
147150
agent_id,
148151
minute_bucket
@@ -164,9 +167,9 @@ latest_buckets AS (
164167
latest_agent_stats AS (
165168
SELECT
166169
SUM(session_count_vscode) AS session_count_vscode,
170+
SUM(session_count_ssh) AS session_count_ssh,
167171
SUM(session_count_jetbrains) AS session_count_jetbrains,
168-
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
169-
SUM(session_count_ssh) AS session_count_ssh
172+
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
170173
FROM
171174
latest_buckets
172175
)
@@ -223,14 +226,15 @@ minute_buckets AS (
223226
agent_id,
224227
date_trunc('minute', created_at) AS minute_bucket,
225228
SUM(session_count_vscode) AS session_count_vscode,
229+
SUM(session_count_ssh) AS session_count_ssh,
226230
SUM(session_count_jetbrains) AS session_count_jetbrains,
227-
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
228-
SUM(session_count_ssh) AS session_count_ssh
231+
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
229232
FROM
230233
workspace_agent_stats
231234
WHERE
232235
created_at >= $1
233236
AND created_at < date_trunc('minute', now()) -- Exclude current partial minute
237+
AND usage = true
234238
GROUP BY
235239
agent_id,
236240
minute_bucket,
@@ -256,9 +260,9 @@ latest_buckets AS (
256260
SELECT
257261
agent_id,
258262
SUM(session_count_vscode) AS session_count_vscode,
263+
SUM(session_count_ssh) AS session_count_ssh,
259264
SUM(session_count_jetbrains) AS session_count_jetbrains,
260-
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty,
261-
SUM(session_count_ssh) AS session_count_ssh
265+
SUM(session_count_reconnecting_pty) AS session_count_reconnecting_pty
262266
FROM
263267
latest_buckets
264268
)
@@ -336,6 +340,7 @@ WITH agent_stats AS (
336340
coalesce(SUM(session_count_reconnecting_pty), 0)::bigint AS session_count_reconnecting_pty,
337341
coalesce(SUM(connection_count), 0)::bigint AS connection_count
338342
FROM workspace_agent_stats
343+
WHERE usage = true
339344
GROUP BY user_id, agent_id, workspace_id
340345
), latest_agent_latencies AS (
341346
SELECT

coderd/insights_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
706706
SessionCountJetbrains: stat.sessionCountJetBrains,
707707
SessionCountReconnectingPty: stat.sessionCountReconnectingPTY,
708708
SessionCountSsh: stat.sessionCountSSH,
709-
})
709+
}, false)
710710
require.NoError(t, err, "want no error inserting agent stats")
711711
createdAt = createdAt.Add(30 * time.Second)
712712
}
@@ -1605,7 +1605,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
16051605
SessionCountJetbrains: stat.sessionCountJetBrains,
16061606
SessionCountReconnectingPty: stat.sessionCountReconnectingPTY,
16071607
SessionCountSsh: stat.sessionCountSSH,
1608-
})
1608+
}, false)
16091609
require.NoError(t, err, "want no error inserting agent stats")
16101610
createdAt = createdAt.Add(30 * time.Second)
16111611
}

coderd/metricscache/metricscache.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ type Cache struct {
3434

3535
done chan struct{}
3636
cancel func()
37+
38+
// usage is a experiment flag to enable new workspace usage tracking behavior and will be
39+
// removed when the experiment is complete.
40+
usage bool
3741
}
3842

3943
type Intervals struct {
4044
TemplateBuildTimes time.Duration
4145
DeploymentStats time.Duration
4246
}
4347

44-
func New(db database.Store, log slog.Logger, intervals Intervals) *Cache {
48+
func New(db database.Store, log slog.Logger, intervals Intervals, usage bool) *Cache {
4549
if intervals.TemplateBuildTimes <= 0 {
4650
intervals.TemplateBuildTimes = time.Hour
4751
}
@@ -56,6 +60,7 @@ func New(db database.Store, log slog.Logger, intervals Intervals) *Cache {
5660
log: log,
5761
done: make(chan struct{}),
5862
cancel: cancel,
63+
usage: usage,
5964
}
6065
go func() {
6166
var wg sync.WaitGroup
@@ -125,11 +130,25 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error {
125130
}
126131

127132
func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
128-
from := dbtime.Now().Add(-15 * time.Minute)
129-
agentStats, err := c.database.GetDeploymentWorkspaceAgentStats(ctx, from)
130-
if err != nil {
131-
return err
133+
var (
134+
from = dbtime.Now().Add(-15 * time.Minute)
135+
agentStats database.GetDeploymentWorkspaceAgentStatsRow
136+
err error
137+
)
138+
139+
if c.usage {
140+
agentUsageStats, err := c.database.GetDeploymentWorkspaceAgentUsageStats(ctx, from)
141+
if err != nil {
142+
return err
143+
}
144+
agentStats = database.GetDeploymentWorkspaceAgentStatsRow(agentUsageStats)
145+
} else {
146+
agentStats, err = c.database.GetDeploymentWorkspaceAgentStats(ctx, from)
147+
if err != nil {
148+
return err
149+
}
132150
}
151+
133152
workspaceStats, err := c.database.GetDeploymentWorkspaceStats(ctx)
134153
if err != nil {
135154
return err

coderd/metricscache/metricscache_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestCache_TemplateWorkspaceOwners(t *testing.T) {
3232
db = dbmem.New()
3333
cache = metricscache.New(db, slogtest.Make(t, nil), metricscache.Intervals{
3434
TemplateBuildTimes: testutil.IntervalFast,
35-
})
35+
}, false)
3636
)
3737

3838
defer cache.Close()
@@ -183,7 +183,7 @@ func TestCache_BuildTime(t *testing.T) {
183183
db = dbmem.New()
184184
cache = metricscache.New(db, slogtest.Make(t, nil), metricscache.Intervals{
185185
TemplateBuildTimes: testutil.IntervalFast,
186-
})
186+
}, false)
187187
)
188188

189189
defer cache.Close()
@@ -278,7 +278,7 @@ func TestCache_DeploymentStats(t *testing.T) {
278278
db := dbmem.New()
279279
cache := metricscache.New(db, slogtest.Make(t, nil), metricscache.Intervals{
280280
DeploymentStats: testutil.IntervalFast,
281-
})
281+
}, false)
282282
defer cache.Close()
283283

284284
err := db.InsertWorkspaceAgentStats(context.Background(), database.InsertWorkspaceAgentStatsParams{

coderd/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ func (api *API) postWorkspaceUsage(rw http.ResponseWriter, r *http.Request) {
13401340
return
13411341
}
13421342

1343-
err = api.statsReporter.ReportAgentStats(ctx, dbtime.Now(), workspace, agent, template.Name, stat)
1343+
err = api.statsReporter.ReportAgentStats(ctx, dbtime.Now(), workspace, agent, template.Name, stat, true)
13441344
if err != nil {
13451345
httpapi.InternalServerError(rw, err)
13461346
return

0 commit comments

Comments
 (0)