feat: Runtime metrics integration - #1400
Conversation
| if runtimeMetricsRunning { | ||
| return | ||
| } | ||
|
|
||
| onceRuntimeMetrics.Do(func() { | ||
| runtimeMetricsRunning = true | ||
| }) |
There was a problem hiding this comment.
Bug: A race condition allows multiple goroutines to start the metrics collection loop, causing a data race on the shared runtimeMetricsSamples slice.
Severity: HIGH
Suggested Fix
Use a mutex to protect the entire block that checks runtimeMetricsRunning and starts the collection loop. This will ensure that only one goroutine can enter the critical section to initialize and run the metrics collection, preventing the race condition.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: runtime_metrics.go#L126-L132
Potential issue: A TOCTOU race condition exists in the runtime metrics initialization.
Multiple goroutines can check the `runtimeMetricsRunning` flag as `false` before it is
set. While `sync.Once.Do()` ensures the flag is set only once, it does not prevent the
other goroutines from proceeding. This results in multiple goroutines entering the
collection loop and concurrently calling `runtime_metrics.Read` with the shared global
slice `runtimeMetricsSamples`. This is a data race, as the Go documentation for
`runtime/metrics` explicitly states that arguments to `Read` must not share underlying
memory.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 0aeba9f. Configure here.
|
|
||
| onceRuntimeMetrics.Do(func() { | ||
| runtimeMetricsRunning = true | ||
| }) |
There was a problem hiding this comment.
Broken single-start race
High Severity
StartRuntimeMetrics moved the collector loop out of sync.Once and now relies on an unsynchronized runtimeMetricsRunning check. Concurrent callers can both pass that check, then both start loops that share runtimeMetricsSamples, which runtime/metrics.Read forbids.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 0aeba9f. Configure here.


Description
This is a proof of concept that I talked really briefly on Slack. Might or might not be merged. NodeJS has this integration, and I thought, this is easy and really possible in Go. Let's just have it!
To use or test this feature/integration, just copy and paste the code into your codebase, and fix all the import bugs (stuff like
SetHubOnContextshould besentry.SetHubOnContext, etc). Then use it, like so:Issues
Changelog Entry Instructions
To add a custom changelog entry, uncomment the section above. Supports:
For more details: custom changelog entries
Reminders
feat:,fix:,ref:,meta:)