Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Prev Previous commit
Next Next commit
don't need fallback DB query anymore since we added username min length
constraint to users table

Signed-off-by: Callum Styan <[email protected]>
  • Loading branch information
cstyan committed Aug 21, 2025
commit 43b122879a2584e6d9280f582e03ec20c32194af
32 changes: 7 additions & 25 deletions coderd/prometheusmetrics/prometheusmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,42 +328,24 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
templateVersionName = "unknown"
}

username := workspace.OwnerUsername
// This should never be possible, but we'll guard against it just in case.
// 1. The owner_id field on the workspaces table is a reference to IDs in the users table and has a `NOT NULL` constraint
// 2. At user creation time our httpmw package enforces non-empty usernames
// 3. The workspaces_expanded view has an inner join on workspaces.owner_id = visible_users.id, and if the owner
// is valid in the users table (which visible_users is a view of) then they will have a username set
if username == "" {
logger.Warn(ctx, "in prometheusmetrics.Agents the username on workspace was empty string, this should not be possible",
slog.F("workspace_id", workspace.ID),
slog.F("template_id", workspace.TemplateID))
// Fallback to GetUserByID if OwnerUsername is empty (e.g., in tests)
user, err := db.GetUserByID(ctx, workspace.OwnerID)
if err != nil {
logger.Error(ctx, "can't get user from the database", slog.F("user_id", workspace.OwnerID), slog.Error(err))
agentsGauge.WithLabelValues(VectorOperationAdd, 0, user.Username, workspace.Name, templateName, templateVersionName)
continue
}
username = user.Username
}
// username :=

agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
if err != nil {
logger.Error(ctx, "can't get workspace agents", slog.F("workspace_id", workspace.ID), slog.Error(err))
agentsGauge.WithLabelValues(VectorOperationAdd, 0, username, workspace.Name, templateName, templateVersionName)
agentsGauge.WithLabelValues(VectorOperationAdd, 0, workspace.OwnerUsername, workspace.Name, templateName, templateVersionName)
continue
}

if len(agents) == 0 {
logger.Debug(ctx, "workspace agents are unavailable", slog.F("workspace_id", workspace.ID))
agentsGauge.WithLabelValues(VectorOperationAdd, 0, username, workspace.Name, templateName, templateVersionName)
agentsGauge.WithLabelValues(VectorOperationAdd, 0, workspace.OwnerUsername, workspace.Name, templateName, templateVersionName)
continue
}

for _, agent := range agents {
// Collect information about agents
agentsGauge.WithLabelValues(VectorOperationAdd, 1, username, workspace.Name, templateName, templateVersionName)
agentsGauge.WithLabelValues(VectorOperationAdd, 1, workspace.OwnerUsername, workspace.Name, templateName, templateVersionName)

connectionStatus := agent.Status(agentInactiveDisconnectTimeout)
node := (*coordinator.Load()).Node(agent.ID)
Expand All @@ -373,7 +355,7 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
tailnetNode = node.ID.String()
}

agentsConnectionsGauge.WithLabelValues(VectorOperationSet, 1, agent.Name, username, workspace.Name, string(connectionStatus.Status), string(agent.LifecycleState), tailnetNode)
agentsConnectionsGauge.WithLabelValues(VectorOperationSet, 1, agent.Name, workspace.OwnerUsername, workspace.Name, string(connectionStatus.Status), string(agent.LifecycleState), tailnetNode)

if node == nil {
logger.Debug(ctx, "can't read in-memory node for agent", slog.F("agent_id", agent.ID))
Expand All @@ -398,7 +380,7 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
}
}

agentsConnectionLatenciesGauge.WithLabelValues(VectorOperationSet, latency, agent.Name, username, workspace.Name, region.RegionName, fmt.Sprintf("%v", node.PreferredDERP == regionID))
agentsConnectionLatenciesGauge.WithLabelValues(VectorOperationSet, latency, agent.Name, workspace.OwnerUsername, workspace.Name, region.RegionName, fmt.Sprintf("%v", node.PreferredDERP == regionID))
}
}

Expand All @@ -410,7 +392,7 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
}

for _, app := range apps {
agentsAppsGauge.WithLabelValues(VectorOperationAdd, 1, agent.Name, username, workspace.Name, app.DisplayName, string(app.Health))
agentsAppsGauge.WithLabelValues(VectorOperationAdd, 1, agent.Name, workspace.OwnerUsername, workspace.Name, app.DisplayName, string(app.Health))
}
}
}
Expand Down
20 changes: 1 addition & 19 deletions coderd/workspacestats/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,8 @@ func (r *Reporter) ReportAgentStats(ctx context.Context, now time.Time, workspac

// update prometheus metrics
if r.opts.UpdateAgentMetricsFn != nil {
username := workspace.OwnerUsername
// This should never be possible, but we'll guard against it just in case.
// 1. The owner_id field on the workspaces table is a reference to IDs in the users table and has a `NOT NULL` constraint
// 2. At user creation time our httpmw package enforces non-empty usernames
// 3. The workspaces_expanded view has an inner join on workspaces.owner_id = visible_users.id, and if the owner
// is valid in the users table (which visible_users is a view of) then they will have a username set
if username == "" {
r.opts.Logger.Warn(ctx, "in ReportAgentStats the username on workspace was empty string, this should not be possible",
slog.F("workspace_id", workspace.ID),
slog.F("template_id", workspace.TemplateID))
// Fallback to GetUserByID if OwnerUsername is empty (e.g., in tests)
user, err := r.opts.Database.GetUserByID(ctx, workspace.OwnerID)
if err != nil {
return xerrors.Errorf("get user: %w", err)
}
username = user.Username
}

r.opts.UpdateAgentMetricsFn(ctx, prometheusmetrics.AgentMetricLabels{
Username: username,
Username: workspace.OwnerUsername,
WorkspaceName: workspace.Name,
AgentName: workspaceAgent.Name,
TemplateName: templateName,
Expand Down
Loading