fix: Close metric scope after tests finish #5889
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
what
Add a helper to close close metrics scope when tests are finished, use it throughout tests.
why
I was trying to use go's race detector on the unit tests, and running into these errors:
I dug into it and the issue wasn't with the race detector itself, it's because the race detector reordered some code exposing a bug where we in many places create a new scope but then discard its
closer. Metrics are emitted during the tests, so if we don't close it, with the reordering, the metrics try to write to t.Log() in test that has already ended.This code consolidates the places where we create a new metrics scope, and properly calls a Cleanup() handler to close the scope when the test is done.
I did most of the conversions
tests
Running CI
references
N/A