From 6a783fc5c79fc7690de2093fabe5297a58c6bea2 Mon Sep 17 00:00:00 2001 From: Zach <3724288+zedkipp@users.noreply.github.com> Date: Fri, 20 Feb 2026 16:12:17 -0700 Subject: [PATCH] fix: floor provisioner job queue wait metric (#22184) After a PostgreSQL round-trip, job timestamps lose their monotonic clock component, making the subtraction susceptible to wall-clock adjustments producing a small negative delta. Floor at 1ms since a zero or negative queue wait is meaningless. Fixes TestProvisionerJobQueueWaitMetric flakes where small negative values (~ -2ms) are observed. --- coderd/provisionerdserver/provisionerdserver.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/coderd/provisionerdserver/provisionerdserver.go b/coderd/provisionerdserver/provisionerdserver.go index 23200bfa89..0e4dd20a05 100644 --- a/coderd/provisionerdserver/provisionerdserver.go +++ b/coderd/provisionerdserver/provisionerdserver.go @@ -840,7 +840,11 @@ func (s *server) acquireProtoJob(ctx context.Context, job database.ProvisionerJo // Record the time the job spent waiting in the queue. if s.metrics != nil && job.StartedAt.Valid && job.Provisioner.Valid() { - queueWaitSeconds := job.StartedAt.Time.Sub(job.CreatedAt).Seconds() + // These timestamps lose their monotonic clock component after a Postgres + // round-trip, so the subtraction is based purely on wall-clock time. Floor at + // 1ms as a defensive measure against clock adjustments producing a negative + // delta while acknowledging there's a non-zero queue time. + queueWaitSeconds := max(job.StartedAt.Time.Sub(job.CreatedAt).Seconds(), 0.001) s.metrics.ObserveJobQueueWait(string(job.Provisioner), string(job.Type), jobTransition, jobBuildReason, queueWaitSeconds) }