MCPcopy Index your code
hub / github.com/coder/coder / emitBuildDurationMetric

Method emitBuildDurationMetric

coderd/agentapi/metrics.go:63–97  ·  view source on GitHub ↗

emitBuildDurationMetric records the end-to-end workspace build duration from build creation to when all agents are ready.

(ctx context.Context, resourceID uuid.UUID)

Source from the content-addressed store, hash-verified

61// emitBuildDurationMetric records the end-to-end workspace build
62// duration from build creation to when all agents are ready.
63func (a *LifecycleAPI) emitBuildDurationMetric(ctx context.Context, resourceID uuid.UUID) {
64 if a.Metrics == nil {
65 return
66 }
67
68 buildInfo, err := a.Database.GetWorkspaceBuildMetricsByResourceID(ctx, resourceID)
69 if err != nil {
70 a.Log.Warn(ctx, "failed to get build info for metrics", slog.Error(err))
71 return
72 }
73
74 // Wait until all agents have reached a terminal startup state.
75 if !buildInfo.AllAgentsReady {
76 return
77 }
78
79 // LastAgentReadyAt is the MAX(ready_at) across all agents. Since
80 // we only get here when AllAgentsReady is true, this should always
81 // be valid.
82 if buildInfo.LastAgentReadyAt.IsZero() {
83 a.Log.Warn(ctx, "last_agent_ready_at is unexpectedly zero",
84 slog.F("last_agent_ready_at", buildInfo.LastAgentReadyAt))
85 return
86 }
87
88 duration := buildInfo.LastAgentReadyAt.Sub(buildInfo.CreatedAt).Seconds()
89
90 a.Metrics.BuildDuration.WithLabelValues(
91 buildInfo.TemplateName,
92 buildInfo.OrganizationName,
93 string(buildInfo.Transition),
94 buildInfo.WorstStatus,
95 strconv.FormatBool(buildInfo.IsPrebuild),
96 ).Observe(duration)
97}

Callers 1

UpdateLifecycleMethod · 0.95

Calls 4

WithLabelValuesMethod · 0.80
ErrorMethod · 0.45
IsZeroMethod · 0.45

Tested by

no test coverage detected