Skip to content

feat: add explicit pool statistics registration cleanup - #93

Open
skarm wants to merge 7 commits into
exaring:mainfrom
skarm:stats-registration-lifecycle
Open

skarm wants to merge 7 commits into
exaring:mainfrom
skarm:stats-registration-lifecycle

Conversation

@skarm

@skarm skarm commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

RecordStats discards the callback registration, leaving callers without a way to stop observations or release the callback's reference to a pool before meter-provider shutdown.

Add RegisterStats(pool, options...) (func() error, error). Its cleanup function serializes concurrent calls, unregisters the callback and releases retained pool/stat references. Failed cleanup can be retried; successful cleanup is idempotent. Keep the existing RecordStats signature and provider-lifetime behavior.

Support callbacks invoked synchronously during registration. On registration failure, deactivate the callback and release pool references before attempting to unregister a partial registration. This also handles an unregister failure: the retained callback stops observing the pool, and both errors are returned.

Validation:

  • Tests in the existing meter_test.go cover independent registrations, concurrent cleanup, collection in progress, provider shutdown, cleanup retry, registration errors and failures at each of the 13 instrument creation sites.
  • The synchronous-registration test reproduces a deadlock on main; the partial-registration test reproduces continued pool reads on the initial branch. Both pass with the final code.
  • make test, go mod tidy -diff and golangci-lint 2.12.2 pass on Go 1.25.0/1.26.9; race tests pass on Go 1.25.0/1.27.2. Additional review diagnostics check concurrent readers and reference release while the callback or cleanup function remains live.
  • The README example compiles on Go 1.25/1.26, unregisters before closing the pool and returns cleanup errors.

pool.Close() does not automatically unregister statistics. The existing PoolStats interface is unchanged; no polling goroutine, finalizer or production dependency is added.

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.

2 participants