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

Commitaec1146

Browse files
author
Callum Styan
committed
do workspace -> agent join in a single query instead of via Go code
Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent63808d0 commitaec1146

File tree

7 files changed

+195
-67
lines changed

7 files changed

+195
-67
lines changed

‎coderd/database/dbauthz/dbauthz.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3552,6 +3552,13 @@ func (q *querier) GetWorkspaceAgentsCreatedAfter(ctx context.Context, createdAt
35523552
returnq.db.GetWorkspaceAgentsCreatedAfter(ctx,createdAt)
35533553
}
35543554

3555+
func (q*querier)GetWorkspaceAgentsForMetrics(ctx context.Context,deletedbool) ([]database.GetWorkspaceAgentsForMetricsRow,error) {
3556+
iferr:=q.authorizeContext(ctx,policy.ActionRead,rbac.ResourceSystem);err!=nil {
3557+
returnnil,err
3558+
}
3559+
returnq.db.GetWorkspaceAgentsForMetrics(ctx,deleted)
3560+
}
3561+
35553562
func (q*querier)GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx context.Context,workspaceID uuid.UUID) ([]database.WorkspaceAgent,error) {
35563563
workspace,err:=q.GetWorkspaceByID(ctx,workspaceID)
35573564
iferr!=nil {

‎coderd/database/dbmetrics/querymetrics.go‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/dbmock/dbmock.go‎

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

‎coderd/database/querier.go‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries.sql.go‎

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries/workspaceagents.sql‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,27 @@ WHERE
365365
id= $1
366366
AND parent_idIS NOT NULL
367367
AND deleted= FALSE;
368+
369+
-- name: GetWorkspaceAgentsForMetrics :many
370+
SELECT
371+
w.idas workspace_id,
372+
w.nameas workspace_name,
373+
u.usernameas owner_username,
374+
t.nameas template_name,
375+
tv.nameas template_version_name,
376+
wb.build_number,
377+
sqlc.embed(workspace_agents)
378+
FROM workspaces w
379+
JOIN users uONw.owner_id=u.id
380+
JOIN templates tONw.template_id=t.id
381+
JOIN workspace_builds wbONw.id=wb.workspace_id
382+
LEFT JOIN template_versions tvONwb.template_version_id=tv.id
383+
JOIN workspace_resources wrONwb.job_id=wr.job_id
384+
JOIN workspace_agentsONwr.id=workspace_agents.resource_id
385+
WHEREw.deleted= @deleted
386+
ANDwb.build_number= (
387+
SELECTMAX(wb2.build_number)
388+
FROM workspace_builds wb2
389+
WHEREwb2.workspace_id=w.id
390+
)
391+
ANDworkspace_agents.deleted= FALSE;

‎coderd/prometheusmetrics/prometheusmetrics.go‎

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -344,87 +344,62 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
344344
timer:=prometheus.NewTimer(metricsCollectorAgents)
345345
derpMap:=derpMapFn()
346346

347-
workspaceRows,err:=db.GetWorkspacesForAgentMetrics(ctx,false)
347+
workspaceAgents,err:=db.GetWorkspaceAgentsForMetrics(ctx,false)
348348
iferr!=nil {
349-
logger.Error(ctx,"can't get workspacerows",slog.Error(err))
349+
logger.Error(ctx,"can't get workspaceagents",slog.Error(err))
350350
goto done
351351
}
352352

353-
for_,workspace:=rangeworkspaceRows {
354-
templateName:=workspace.TemplateName
355-
templateVersionName:=workspace.TemplateVersionName.String
356-
if!workspace.TemplateVersionName.Valid {
357-
templateVersionName="unknown"
358-
}
353+
for_,agent:=rangeworkspaceAgents {
354+
// Collect information about agents
355+
agentsGauge.WithLabelValues(VectorOperationAdd,1,agent.OwnerUsername,agent.WorkspaceName,agent.TemplateName,agent.TemplateVersionName.String)
359356

360-
agents,err:=db.GetWorkspaceAgentsByWorkspaceAndBuildNumber(ctx,
361-
database.GetWorkspaceAgentsByWorkspaceAndBuildNumberParams{
362-
WorkspaceID:workspace.ID,
363-
BuildNumber:workspace.BuildNumber,
364-
})
365-
iferr!=nil {
366-
logger.Error(ctx,"can't get workspace agents",slog.F("workspace_id",workspace.ID),slog.Error(err))
367-
agentsGauge.WithLabelValues(VectorOperationAdd,0,workspace.OwnerUsername,workspace.Name,templateName,templateVersionName)
368-
continue
369-
}
357+
connectionStatus:=agent.WorkspaceAgent.Status(agentInactiveDisconnectTimeout)
358+
node:= (*coordinator.Load()).Node(agent.WorkspaceAgent.ID)
370359

371-
iflen(agents)==0 {
372-
logger.Debug(ctx,"workspace agents are unavailable",slog.F("workspace_id",workspace.ID))
373-
agentsGauge.WithLabelValues(VectorOperationAdd,0,workspace.OwnerUsername,workspace.Name,templateName,templateVersionName)
374-
continue
360+
tailnetNode:="unknown"
361+
ifnode!=nil {
362+
tailnetNode=node.ID.String()
375363
}
376364

377-
for_,agent:=rangeagents {
378-
// Collect information about agents
379-
agentsGauge.WithLabelValues(VectorOperationAdd,1,workspace.OwnerUsername,workspace.Name,templateName,templateVersionName)
380-
381-
connectionStatus:=agent.Status(agentInactiveDisconnectTimeout)
382-
node:= (*coordinator.Load()).Node(agent.ID)
383-
384-
tailnetNode:="unknown"
385-
ifnode!=nil {
386-
tailnetNode=node.ID.String()
387-
}
388-
389-
agentsConnectionsGauge.WithLabelValues(VectorOperationSet,1,agent.Name,workspace.OwnerUsername,workspace.Name,string(connectionStatus.Status),string(agent.LifecycleState),tailnetNode)
390-
391-
ifnode==nil {
392-
logger.Debug(ctx,"can't read in-memory node for agent",slog.F("agent_id",agent.ID))
393-
}else {
394-
// Collect information about connection latencies
395-
forrawRegion,latency:=rangenode.DERPLatency {
396-
regionParts:=strings.SplitN(rawRegion,"-",2)
397-
regionID,err:=strconv.Atoi(regionParts[0])
398-
iferr!=nil {
399-
logger.Error(ctx,"can't convert DERP region",slog.F("agent_id",agent.ID),slog.F("raw_region",rawRegion),slog.Error(err))
400-
continue
401-
}
365+
agentsConnectionsGauge.WithLabelValues(VectorOperationSet,1,agent.WorkspaceAgent.Name,agent.OwnerUsername,agent.WorkspaceName,string(connectionStatus.Status),string(agent.WorkspaceAgent.LifecycleState),tailnetNode)
366+
367+
ifnode==nil {
368+
logger.Debug(ctx,"can't read in-memory node for agent",slog.F("agent_id",agent.WorkspaceAgent.ID))
369+
}else {
370+
// Collect information about connection latencies
371+
forrawRegion,latency:=rangenode.DERPLatency {
372+
regionParts:=strings.SplitN(rawRegion,"-",2)
373+
regionID,err:=strconv.Atoi(regionParts[0])
374+
iferr!=nil {
375+
logger.Error(ctx,"can't convert DERP region",slog.F("agent_id",agent.WorkspaceAgent.ID),slog.F("raw_region",rawRegion),slog.Error(err))
376+
continue
377+
}
402378

403-
region,found:=derpMap.Regions[regionID]
404-
if!found {
405-
// It's possible that a workspace agent is using an old DERPMap
406-
// and reports regions that do not exist. If that's the case,
407-
// report the region as unknown!
408-
region=&tailcfg.DERPRegion{
409-
RegionID:regionID,
410-
RegionName:fmt.Sprintf("Unnamed %d",regionID),
411-
}
379+
region,found:=derpMap.Regions[regionID]
380+
if!found {
381+
// It's possible that a workspace agent is using an old DERPMap
382+
// and reports regions that do not exist. If that's the case,
383+
// report the region as unknown!
384+
region=&tailcfg.DERPRegion{
385+
RegionID:regionID,
386+
RegionName:fmt.Sprintf("Unnamed %d",regionID),
412387
}
413-
414-
agentsConnectionLatenciesGauge.WithLabelValues(VectorOperationSet,latency,agent.Name,workspace.OwnerUsername,workspace.Name,region.RegionName,fmt.Sprintf("%v",node.PreferredDERP==regionID))
415388
}
416-
}
417389

418-
// Collect information about registered applications
419-
apps,err:=db.GetWorkspaceAppsByAgentID(ctx,agent.ID)
420-
iferr!=nil&&!errors.Is(err,sql.ErrNoRows) {
421-
logger.Error(ctx,"can't get workspace apps",slog.F("agent_id",agent.ID),slog.Error(err))
422-
continue
390+
agentsConnectionLatenciesGauge.WithLabelValues(VectorOperationSet,latency,agent.WorkspaceAgent.Name,agent.OwnerUsername,agent.WorkspaceName,region.RegionName,fmt.Sprintf("%v",node.PreferredDERP==regionID))
423391
}
392+
}
424393

425-
for_,app:=rangeapps {
426-
agentsAppsGauge.WithLabelValues(VectorOperationAdd,1,agent.Name,workspace.OwnerUsername,workspace.Name,app.DisplayName,string(app.Health))
427-
}
394+
// Collect information about registered applications
395+
apps,err:=db.GetWorkspaceAppsByAgentID(ctx,agent.WorkspaceAgent.ID)
396+
iferr!=nil&&!errors.Is(err,sql.ErrNoRows) {
397+
logger.Error(ctx,"can't get workspace apps",slog.F("agent_id",agent.WorkspaceAgent.ID),slog.Error(err))
398+
continue
399+
}
400+
401+
for_,app:=rangeapps {
402+
agentsAppsGauge.WithLabelValues(VectorOperationAdd,1,agent.WorkspaceAgent.Name,agent.OwnerUsername,agent.WorkspaceName,app.DisplayName,string(app.Health))
428403
}
429404
}
430405

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp