Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: introduce dedicated queries for workspaces and workspace agents metrics#19786

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
cstyan merged 8 commits intomainfromcallum/get-workspaces-metrics-calls
Oct 17, 2025

Conversation

@cstyan
Copy link
Contributor

Over the last 7d, theGetWorkspaces query is our most expensive:

Screenshot 2025-09-11 at 7 10 49 PM

At least 60k of those 600k calls are from theprometheusmetrics.Workspaces andprometheusmetrics.Agents paths just to generate some Prometheus metrics.

TheGetWorkspaces query is very generic, supporting a range of end use cases in our application by joining on other tables to fill out details about each workspace. In the case of theWorkspaces andAgents metrics, we didn't actually need the vast majority of the data being returned in each row.

In this PR I'm introducing new queries for these paths that only retrieve the workspaces/agents and the required rows for each as they're used within those code paths. This should make these queries significantly less expensive, but at the very least will help us more accurately attribute query cost more to its actual end use case.

As a quick example, anEXPLAIN of a simplified version of the original query has at it's top level:

 Nested Loop Left Join  (cost=118.41..8478.57 rows=368 width=50) (actual time=2.904..24.491 rows=368 loops=1)

while the new query forGetWorkspacesForWorkspaceMetrics has:

 Nested Loop Left Join  (cost=33.91..3568.64 rows=2 width=50) (actual time=0.208..7.242 rows=368 loops=1)

Besides the tests, I ran som queries to confirm that the results of the new query lined up with the currently exported values for the Prometheus metrics on our own internal deployment.

Screenshot 2025-09-11 at 3 29 57 PMScreenshot 2025-09-11 at 3 29 43 PM

@cstyancstyan changed the titlebug: introduce dedicated queries for workspaces and workspace agents metricsfix: introduce dedicated queries for workspaces and workspace agents metricsSep 12, 2025
@cstyancstyanforce-pushed thecallum/get-workspaces-metrics-calls branch from2974bfc toe4265acCompareSeptember 17, 2025 19:03
Comment on lines 3810 to 3868
iferr:=q.authorizeContext(ctx,policy.ActionRead,rbac.ResourceSystem);err!=nil {
returnnil,err
}
returnq.db.GetWorkspacesForAgentMetrics(ctx,deleted)
}

func (q*querier)GetWorkspacesForWorkspaceMetrics(ctx context.Context,deletedbool) ([]database.GetWorkspacesForWorkspaceMetricsRow,error) {
iferr:=q.authorizeContext(ctx,policy.ActionRead,rbac.ResourceSystem);err!=nil {
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I wasn't sure ifResourceSystem was appropriate here, just following the pattern of other queries in the general vicinity of these ones. I don't know if we really want to go about adding a new resource type for all cases, but here we could probably just reuseResource.Workspace?

Copy link
Contributor

Choose a reason for hiding this comment

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

I really want us to avoid proliferating the use ofResourceSystem --- it's a kitchen sink of different objects right now, and they have very different security properties. We need to go back and break it up at some point, but in the mean time we need to not make the problem worse.

UsingResourceWorkspace would be fine here, IMO, as long as the check is whether the Subject can readall workspaces on the site (since that's what this query does), not just whether they can read any workspace.

Comment on lines 3810 to 3868
iferr:=q.authorizeContext(ctx,policy.ActionRead,rbac.ResourceSystem);err!=nil {
returnnil,err
}
returnq.db.GetWorkspacesForAgentMetrics(ctx,deleted)
}

func (q*querier)GetWorkspacesForWorkspaceMetrics(ctx context.Context,deletedbool) ([]database.GetWorkspacesForWorkspaceMetricsRow,error) {
iferr:=q.authorizeContext(ctx,policy.ActionRead,rbac.ResourceSystem);err!=nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I really want us to avoid proliferating the use ofResourceSystem --- it's a kitchen sink of different objects right now, and they have very different security properties. We need to go back and break it up at some point, but in the mean time we need to not make the problem worse.

UsingResourceWorkspace would be fine here, IMO, as long as the check is whether the Subject can readall workspaces on the site (since that's what this query does), not just whether they can read any workspace.

database.GetWorkspaceAgentsByWorkspaceAndBuildNumberParams{
WorkspaceID:workspace.ID,
BuildNumber:workspace.BuildNumber,
})
Copy link
Contributor

Choose a reason for hiding this comment

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

This construction appears to be doing a "join" but as a Go loop with multiple queries. Does it make sense to combine this with thedb.GetWorkspacesForAgentMetrics(ctx, false) call into a single query that just returns all the agents directly?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I was just focused on splitting out the queries so that they would be more easily identifiable/separate from other instances ofGetWorkspaces, but this change makes sense 👍 made it in some follow up commits.

@github-actionsgithub-actionsbot added the staleThis issue is like stale bread. labelSep 26, 2025
@cstyancstyan reopened thisOct 15, 2025
Signed-off-by: Callum Styan <callumstyan@gmail.com>
fix testsSigned-off-by: Callum Styan <callumstyan@gmail.com>
@cstyancstyanforce-pushed thecallum/get-workspaces-metrics-calls branch 2 times, most recently from299a3b3 toc34e3f7CompareOctober 15, 2025 22:31
Signed-off-by: Callum Styan <callumstyan@gmail.com>
@cstyancstyanforce-pushed thecallum/get-workspaces-metrics-calls branch fromc34e3f7 to63808d0CompareOctober 15, 2025 22:38
@github-actionsgithub-actionsbot removed the staleThis issue is like stale bread. labelOct 16, 2025
@github-actions
Copy link

github-actionsbot commentedOct 16, 2025
edited
Loading

All contributors have signed the CLA ✍️ ✅
Posted by theCLA Assistant Lite bot.

Signed-off-by: Callum Styan <callumstyan@gmail.com>
@cstyancstyanforce-pushed thecallum/get-workspaces-metrics-calls branch fromaec1146 toc5d04b5CompareOctober 16, 2025 23:16
@cstyan
Copy link
ContributorAuthor

@spikecurtis thanks for the review. Reopened and updated, I was away for a few weeks so this got hit by the stale bot.

Copy link
Contributor

@spikecurtisspikecurtis left a comment

Choose a reason for hiding this comment

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

Some inline comments about eliding some arguments and returned fields, but I don't need to review again.

JOIN workspace_builds wbONw.id=wb.workspace_id
JOIN provisioner_jobs pjONwb.job_id=pj.id
LEFT JOIN template_versions tvONwb.template_version_id=tv.id
WHEREw.deleted= @deleted
Copy link
Contributor

Choose a reason for hiding this comment

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

do we ever call this with deleted = true? Why would we? Maybe it would be better to eliminate this parameter as it might be confusing anyway --- if you set it to true youonly get the deleted workspaces, but it would be easy to think you'd get all workspaces.

u.usernameas owner_username,
t.nameas template_name,
tv.nameas template_version_name,
wb.build_number,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we care about this; we don't use it anywhere in the metrics.

LEFT JOIN template_versions tvONwb.template_version_id=tv.id
JOIN workspace_resources wrONwb.job_id=wr.job_id
JOIN workspace_agentsONwr.id=workspace_agents.resource_id
WHEREw.deleted= @deleted
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, I don't think there are any circumstances where we would ever set this to true, so we can eliminate it.

}
for_,agent:=rangeworkspaceAgents {
// Collect information about agents
agentsGauge.WithLabelValues(VectorOperationAdd,1,agent.OwnerUsername,agent.WorkspaceName,agent.TemplateName,agent.TemplateVersionName.String)
Copy link
Contributor

Choose a reason for hiding this comment

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

missing the check onTemplateVersionName.Valid from the original

Signed-off-by: Callum Styan <callumstyan@gmail.com>
@cstyan
Copy link
ContributorAuthor

Thanks for the follow up review 👍 addressed your comments, so going to merge this.

@cstyancstyan merged commit141ef23 intomainOct 17, 2025
30 checks passed
@cstyancstyan deleted the callum/get-workspaces-metrics-calls branchOctober 17, 2025 20:40
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsOct 17, 2025
}

for_,metric:=rangem.Metric {
fmt.Printf("metric: %+v\n",metric)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to remove this?

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@zedkippzedkippzedkipp left review comments

@spikecurtisspikecurtisspikecurtis approved these changes

Assignees

@cstyancstyan

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@cstyan@zedkipp@spikecurtis

[8]ページ先頭

©2009-2025 Movatter.jp