Skip to content

clustermetrics: add support for stopwatch type#164371

Open
kyle-a-wong wants to merge 1 commit intocockroachdb:masterfrom
kyle-a-wong:support_stopwatch_cmreader
Open

clustermetrics: add support for stopwatch type#164371
kyle-a-wong wants to merge 1 commit intocockroachdb:masterfrom
kyle-a-wong:support_stopwatch_cmreader

Conversation

@kyle-a-wong
Copy link
Contributor

Adds support for stopwatch type metrics on the cluster metric read path. Stopwatch metrics are gauges that report the duration of time (in unix nanos) that has passed since it was last updated.

Epic: https://cockroachlabs.atlassian.net/browse/CRDB-58342
Resolves: https://cockroachlabs.atlassian.net/browse/CRDB-60768
Release note: None

@kyle-a-wong kyle-a-wong requested review from a team as code owners February 25, 2026 18:23
@kyle-a-wong kyle-a-wong requested review from arjunmahishi and jasonlmfong and removed request for a team February 25, 2026 18:23
@trunk-io
Copy link
Contributor

trunk-io bot commented Feb 25, 2026

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@jasonlmfong jasonlmfong left a comment

Choose a reason for hiding this comment

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

looks good

Comment on lines 148 to 159
swTimestamp2 := time.Now().UnixNano()
r.Exec(t, fmt.Sprintf(`INSERT INTO system.cluster_metrics
(id, name, labels, type, value, node_id)
VALUES (601, 'test.stopwatch_labeled', '{"store": "2"}', 'STOPWATCH', %d, 1)`,
swTimestamp2))

testutils.SucceedsSoon(t, func() error {
return checkStopwatchVecElapsed(
reg, "test.stopwatch_labeled",
map[string]string{"store": "2"}, 0,
)
})
Copy link
Member

Choose a reason for hiding this comment

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

maybe also put this in the past for like 5 seconds and check that, since checking elapsed 0 seems too trivial

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines +537 to +558
// requireStopwatchElapsed asserts that a scalar *metric.Gauge (backed by a
// functional gauge) reports an elapsed time of at least minElapsed.
func requireStopwatchElapsed(
t *testing.T, reg metric.RegistryReader, name string, minElapsed time.Duration,
) {
t.Helper()
var g *metric.Gauge
reg.Each(func(n string, v interface{}) {
if n == name {
if gauge, ok := v.(*metric.Gauge); ok {
g = gauge
}
}
})
require.NotNilf(t, g, "stopwatch gauge %q not found in registry", name)
elapsedNanos := g.Value()
require.Greater(t, elapsedNanos, int64(0),
"stopwatch %q should report positive elapsed time", name)
require.GreaterOrEqual(t, elapsedNanos, int64(minElapsed),
"stopwatch %q elapsed %s should be >= %s",
name, time.Duration(elapsedNanos), minElapsed)
}
Copy link
Member

Choose a reason for hiding this comment

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

i see, this threw me off a bit but then i see that the call to g.Value() hits the code to do the subtraction

@kyle-a-wong kyle-a-wong force-pushed the support_stopwatch_cmreader branch from faaa5f2 to 743860a Compare February 26, 2026 22:20
Copy link
Contributor Author

@kyle-a-wong kyle-a-wong left a comment

Choose a reason for hiding this comment

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

Rebased on master which added new metric.Gauge types to use and removed the stopwatch.go files from the previous commit

Comment on lines 148 to 159
swTimestamp2 := time.Now().UnixNano()
r.Exec(t, fmt.Sprintf(`INSERT INTO system.cluster_metrics
(id, name, labels, type, value, node_id)
VALUES (601, 'test.stopwatch_labeled', '{"store": "2"}', 'STOPWATCH', %d, 1)`,
swTimestamp2))

testutils.SucceedsSoon(t, func() error {
return checkStopwatchVecElapsed(
reg, "test.stopwatch_labeled",
map[string]string{"store": "2"}, 0,
)
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Adds support for stopwatch type metrics on the cluster
metric read path. Stopwatch metrics are gauges that
report the duration of time (in unix nanos) that has
passed since it was last updated.

Epic: https://cockroachlabs.atlassian.net/browse/CRDB-58342
Resolves: https://cockroachlabs.atlassian.net/browse/CRDB-60768
Release note: None
@kyle-a-wong kyle-a-wong force-pushed the support_stopwatch_cmreader branch from 743860a to 1af2348 Compare February 26, 2026 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants