10000 feat(coderd): return agent script timings by BrunoQuaresma · Pull Request #14923 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(coderd): return agent script timings #14923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(coderd): return agent script timings
Add the agent script timings into the `/workspaces/:workspaceId/timings`
response.
  • Loading branch information
BrunoQuaresma committed Oct 1, 2024
commit 1b62a395bd644b5685fb2eb89304748e2ad031e5
2 changes: 1 addition & 1 deletion coderd/agentapi/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *ScriptsAPI) ScriptCompleted(ctx context.Context, req *agentproto.Worksp

//nolint:gocritic // We need permissions to write to the DB here and we are in the context of the agent.
ctx = dbauthz.AsProvisionerd(ctx)
err = s.Database.InsertWorkspaceAgentScriptTimings(ctx, database.InsertWorkspaceAgentScriptTimingsParams{
_, err = s.Database.InsertWorkspaceAgentScriptTimings(ctx, database.InsertWorkspaceAgentScriptTimingsParams{
ScriptID: scriptID,
Stage: stage,
Status: status,
Expand Down
32 changes: 32 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,13 @@ func (q *querier) GetWorkspaceAgentPortShare(ctx context.Context, arg database.G
return q.db.GetWorkspaceAgentPortShare(ctx, arg)
}

func (q *querier) GetWorkspaceAgentScriptTimingsByWorkspaceID(ctx context.Context, workspaceID uuid.UUID) ([]database.GetWorkspaceAgentScriptTimingsByWorkspaceIDRow, error) {
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {
return nil, err
}
return q.db.GetWorkspaceAgentScriptTimingsByWorkspaceID(ctx, workspaceID)
}

func (q *querier) GetWorkspaceAgentScriptsByAgentIDs(ctx context.Context, ids []uuid.UUID) ([]database.WorkspaceAgentScript, error) {
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {
return nil, err
Expand Down Expand Up @@ -3034,9 +3041,9 @@ func (q *querier) InsertWorkspaceAgentMetadata(ctx context.Context, arg database
return q.db.InsertWorkspaceAgentMetadata(ctx, arg)
}

func (q *querier) InsertWorkspaceAgentScriptTimings(ctx context.Context, arg database.InsertWorkspaceAgentScriptTimingsParams) error {
func (q *querier) InsertWorkspaceAgentScriptTimings(ctx context.Context, arg database.InsertWorkspaceAgentScriptTimingsParams) (database.WorkspaceAgentScriptTiming, error) {
if err := q.authorizeContext(ctx, policy.ActionCreate, rbac.ResourceSystem); err != nil {
return err
return database.WorkspaceAgentScriptTiming{}, err
}
return q.db.InsertWorkspaceAgentScriptTimings(ctx, arg)
}
Expand Down
12 changes: 12 additions & 0 deletions coderd/database/dbgen/dbgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ func WorkspaceAgent(t testing.TB, db database.Store, orig database.WorkspaceAgen
return agt
}

func WorkspaceAgentScripts(t testing.TB, db database.Store, orig database.InsertWorkspaceAgentScriptsParams) []database.WorkspaceAgentScript {
scripts, err := db.InsertWorkspaceAgentScripts(genCtx, orig)
require.NoError(t, err, "insert workspace agent script")
return scripts
}

func WorkspaceAgentScriptTiming(t testing.TB, db database.Store, orig database.InsertWorkspaceAgentScriptTimingsParams) database.WorkspaceAgentScriptTiming {
timing, err := db.InsertWorkspaceAgentScriptTimings(genCtx, orig)
require.NoError(t, err, "insert workspace agent script")
return timing
}

func Workspace(t testing.TB, db database.Store, orig database.Workspace) database.Workspace {
t.Helper()

Expand Down
87 changes: 73 additions & 14 deletions coderd/database/dbmem/dbmem.go
CF59
Original file line number Diff line number Diff line change
Expand Up @@ -5793,6 +5793,67 @@
return database.WorkspaceAgentPortShare{}, sql.ErrNoRows
}

func (q *FakeQuerier) GetWorkspaceAgentScriptTimingsByWorkspaceID(ctx context.Context, workspaceID uuid.UUID) ([]database.GetWorkspaceAgentScriptTimingsByWorkspaceIDRow, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, workspaceID)
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap these errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by " wrap these errors"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, err
return nil, xerrors.Errorf("failed to retrieve workspace build: %w", err)

This adds context for what might be a fairly generic error.

}

resources, err := q.GetWorkspaceResourcesByJobID(ctx, build.JobID)
if err != nil {
return nil, err
}
resourceIDs := make([]uuid.UUID, 0, len(resources))
for _, res := range resources {
resourceIDs = append(resourceIDs, res.ID)
}

agents, err := q.GetWorkspaceAgentsByResourceIDs(ctx, resourceIDs)
if err != nil {
return nil, err
}
agentIDs := make([]uuid.UUID, 0, len(agents))
for _, agent := range agents {
agentIDs = append(agentIDs, agent.ID)
}

scripts, err := q.GetWorkspaceAgentScriptsByAgentIDs(ctx, agentIDs)
if err != nil {
return nil, err
}
scriptIDs := make([]uuid.UUID, 0, len(scripts))
for _, script := range scripts {
scriptIDs = append(scriptIDs, script.ID)
}

rows := []database.GetWorkspaceAgentScriptTimingsByWorkspaceIDRow{}
for _, t := range q.workspaceAgentScriptTimings {
if slices.Contains(scriptIDs, t.ScriptID) {
var script database.WorkspaceAgentScript
for _, s := range scripts {
if s.ID == t.ScriptID {
script = s
break
}
}

rows = append(rows, database.GetWorkspaceAgentScriptTimingsByWorkspaceIDRow{
ScriptID: t.ScriptID,
StartedAt: t.StartedAt,
EndedAt: t.EndedAt,
ExitCode: t.ExitCode,
Stage: t.Stage,
Status: t.Status,
DisplayName: script.DisplayName,
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: reduce nesting by keeping the happy path unindented.

i.e. continue if the slice does not contain what you're looking for.

}
return rows, nil
}

func (q *FakeQuerier) GetWorkspaceAgentScriptsByAgentIDs(_ context.Context, ids []uuid.UUID) ([]database.WorkspaceAgentScript, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down Expand Up @@ -7844,28 +7905,26 @@
return nil
}

func (q *FakeQuerier) InsertWorkspaceAgentScriptTimings(_ context.Context, arg database.InsertWorkspaceAgentScriptTimingsParams) error {
func (q *FakeQuerier) InsertWorkspaceAgentScriptTimings(_ context.Context, arg database.InsertWorkspaceAgentScriptTimingsParams) (database.WorkspaceAgentScriptTiming, error) {
err := validateDatabaseType(arg)
if err != nil {
return err
return database.WorkspaceAgentScriptTiming{}, err
}

q.mutex.Lock()
defer q.mutex.Unlock()

q.workspaceAgentScriptTimings = append(q.workspaceAgentScriptTimings,
//nolint:gosimple // Stop the linter complaining about changing the type of `arg`.
database.WorkspaceAgentScriptTiming{
ScriptID: arg.ScriptID,
StartedAt: arg.StartedAt,
EndedAt: arg.EndedAt,
ExitCode: arg.ExitCode,
Stage: arg.Stage,
Status: arg.Status,
},
)
timing := database.WorkspaceAgentScriptTiming{

Check failure on line 7917 in coderd/database/dbmem/dbmem.go

View workflow job for this annotation

GitHub Actions / lint

S1016: should convert arg (type github.com/coder/coder/v2/coderd/database.InsertWorkspaceAgentScriptTimingsParams) to github.com/coder/coder/v2/coderd/database.WorkspaceAgentScriptTiming instead of using struct literal (gosimple)
ScriptID: arg.ScriptID,
StartedAt: arg.StartedAt,
EndedAt: arg.EndedAt,
ExitCode: arg.ExitCode,
Stage: arg.Stage,
Status: arg.Status,
}
q.workspaceAgentScriptTimings = append(q.workspaceAgentScriptTimings, timing)

return nil
return timing, nil
}

func (q *FakeQuerier) InsertWorkspaceAgentScripts(_ context.Context, arg database.InsertWorkspaceAgentScriptsParams) ([]database.WorkspaceAgentScript, error) {
Expand Down
13 changes: 10 additions & 3 deletions coderd/database/dbmetrics/dbmetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading
0