Skip to content

Commit befb4dc

Browse files
committed
feat(coderd): track coder_app usage
Updates #8658
1 parent 31b7de6 commit befb4dc

23 files changed

+715
-3
lines changed

coderd/apidoc/docs.go

+79
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+73
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderd.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,9 @@ func New(options *Options) *API {
417417
}
418418
}
419419

420+
workspaceAppsLogger := options.Logger.Named("workspaceapps")
420421
api.workspaceAppServer = &workspaceapps.Server{
421-
Logger: options.Logger.Named("workspaceapps"),
422+
Logger: workspaceAppsLogger,
422423

423424
DashboardURL: api.AccessURL,
424425
AccessURL: api.AccessURL,
@@ -429,6 +430,11 @@ func New(options *Options) *API {
429430
SignedTokenProvider: api.WorkspaceAppsProvider,
430431
AgentProvider: api.agentProvider,
431432
AppSecurityKey: options.AppSecurityKey,
433+
StatsCollector: workspaceapps.NewStatsCollector(
434+
workspaceAppsLogger.Named("stats_collector"),
435+
workspaceapps.NewStatsDBReporter(options.Database),
436+
workspaceapps.DefaultStatsCollectorReportInterval,
437+
),
432438

433439
DisablePathApps: options.DeploymentValues.DisablePathApps.Value(),
434440
SecureAuthCookie: options.DeploymentValues.SecureAuthCookie.Value(),

coderd/database/dbauthz/dbauthz.go

+5
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,11 @@ func (q *querier) InsertWorkspaceApp(ctx context.Context, arg database.InsertWor
20312031
return q.db.InsertWorkspaceApp(ctx, arg)
20322032
}
20332033

2034+
func (q *querier) InsertWorkspaceAppStats(ctx context.Context, arg database.InsertWorkspaceAppStatsParams) error {
2035+
// TODO(mafredri): Add auth.
2036+
return q.db.InsertWorkspaceAppStats(ctx, arg)
2037+
}
2038+
20342039
func (q *querier) InsertWorkspaceBuild(ctx context.Context, arg database.InsertWorkspaceBuildParams) error {
20352040
w, err := q.db.GetWorkspaceByID(ctx, arg.WorkspaceID)
20362041
if err != nil {

coderd/database/dbfake/dbfake.go

+9
Original file line numberDiff line numberDiff line change
@@ -4260,6 +4260,15 @@ func (q *FakeQuerier) InsertWorkspaceApp(_ context.Context, arg database.InsertW
42604260
return workspaceApp, nil
42614261
}
42624262

4263+
func (q *FakeQuerier) InsertWorkspaceAppStats(ctx context.Context, arg database.InsertWorkspaceAppStatsParams) error {
4264+
err := validateDatabaseType(arg)
4265+
if err != nil {
4266+
return err
4267+
}
4268+
4269+
panic("not implemented")
4270+
}
4271+
42634272
func (q *FakeQuerier) InsertWorkspaceBuild(_ context.Context, arg database.InsertWorkspaceBuildParams) error {
42644273
if err := validateDatabaseType(arg); err != nil {
42654274
return err

coderd/database/dbmetrics/dbmetrics.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dump.sql

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE workspace_app_stats;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
CREATE TABLE workspace_app_stats (
2+
id uuid PRIMARY KEY,
3+
user_id uuid NOT NULL REFERENCES users (id),
4+
workspace_id uuid NOT NULL REFERENCES workspaces (id),
5+
agent_id uuid NOT NULL REFERENCES workspace_agents (id),
6+
access_method text NOT NULL,
7+
slug_or_port text NOT NULL,
8+
session_id uuid NOT NULL,
9+
session_started_at timestamp with time zone NOT NULL,
10+
session_ended_at timestamp with time zone
11+
);
12+
13+
COMMENT ON TABLE workspace_app_stats IS 'A record of workspace app usage statistics';
14+
15+
COMMENT ON COLUMN workspace_app_stats.id IS 'The unique identifier for the workspace app stat record';
16+
COMMENT ON COLUMN workspace_app_stats.user_id IS 'The user who used the workspace app';
17+
COMMENT ON COLUMN workspace_app_stats.workspace_id IS 'The workspace that the workspace app was used in';
18+
COMMENT ON COLUMN workspace_app_stats.agent_id IS 'The workspace agent that was used';
19+
COMMENT ON COLUMN workspace_app_stats.access_method IS 'The method used to access the workspace app';
20+
COMMENT ON COLUMN workspace_app_stats.slug_or_port IS 'The slug or port used to to identify the app';
21+
COMMENT ON COLUMN workspace_app_stats.session_id IS 'The unique identifier for the session';
22+
COMMENT ON COLUMN workspace_app_stats.session_started_at IS 'The time the session started';
23+
COMMENT ON COLUMN workspace_app_stats.session_ended_at IS 'The time the session ended';
24+
25+
-- Create a unique index to prevent duplicate records (scoped to agent to ensure no collisions).
26+
CREATE UNIQUE INDEX workspace_app_stats_user_agent_session_idx ON workspace_app_stats (agent_id, session_id);
27+
28+
-- Create index on workspace_id for joining/filtering by templates.
29+
CREATE INDEX workspace_app_stats_workspace_id_idx ON workspace_app_stats (workspace_id);

0 commit comments

Comments
 (0)