Skip to content

feat: Runtime metrics integration - #1400

Open
aldy505 wants to merge 3 commits into
getsentry:masterfrom
aldy505:feat/runtime-metrics
Open

feat: Runtime metrics integration#1400
aldy505 wants to merge 3 commits into
getsentry:masterfrom
aldy505:feat/runtime-metrics

Conversation

@aldy505

@aldy505 aldy505 commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

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 SetHubOnContext should be sentry.SetHubOnContext, etc). Then use it, like so:

// optional, but recommended. otherwise how would you handle graceful shutdown?
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

if err := sentry.Init(sentry.ClientOptions{}); err != nil {
  panic("yada yada yada")
}
defer sentry.FlushWithContext(ctx)
go sentry.StartRuntimeMetrics(sentry.RuntimeMetricsConfig{Context: ctx})

// the rest of your program

Issues

Changelog Entry Instructions

To add a custom changelog entry, uncomment the section above. Supports:

  • Single entry: just write text
  • Multiple entries: use bullet points
  • Nested bullets: indent 4+ spaces

For more details: custom changelog entries

Reminders

Comment thread runtime_metrics.go Outdated
Comment thread runtime_metrics.go
Comment thread runtime_metrics.go
Comment thread runtime_metrics.go Outdated
Comment thread runtime_metrics.go
Comment on lines +126 to +132
if runtimeMetricsRunning {
return
}

onceRuntimeMetrics.Do(func() {
runtimeMetricsRunning = true
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread runtime_metrics.go

onceRuntimeMetrics.Do(func() {
runtimeMetricsRunning = true
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0aeba9f. Configure here.

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.

1 participant