Skip to content

Closing async instrument removes it from registry - #148411

Merged
mateuszrzeszutek merged 8 commits into
elastic:mainfrom
mateuszrzeszutek:fix/closing-instrument-removes-it-from-the-registry
May 11, 2026
Merged

mateuszrzeszutek merged 8 commits into
elastic:mainfrom
mateuszrzeszutek:fix/closing-instrument-removes-it-from-the-registry

Conversation

@mateuszrzeszutek

Copy link
Copy Markdown
Contributor

This allows the users of the async instruments to close them and stop them from recording measurements when the underlying conditions change (e.g. master-specific metrics can be stopped when a new master is elected). This way, the async instruments can be GC'd, and won't block the measured object from being GC'd as well.

This allows the users of the async instruments to close them and stop
them from recording measurements when the underlying conditions change
(e.g. master-specific metrics can be stopped when a new master is
elected). This way, the async instruments can be GC'd, and won't block
the measured object from being GC'd as well.
@mateuszrzeszutek mateuszrzeszutek added >non-issue :Core/Infra/Metrics Metrics and metering infrastructure labels May 6, 2026
@elasticsearchmachine elasticsearchmachine added v9.5.0 Team:Core/Infra Meta label for core/infra team labels May 6, 2026
@elasticsearchmachine

Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

public void close() {
// deregister this instrument first and close the underlying one second: this avoids the setProvider() method being called in the
// meantime and creating a new OTel instrument that'd leak out
deregisterFunc.accept(this);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: close() in the four adapters has no closed guard, so a double-close runs deregisterFunc getInstrument().close() twice. This is benign today because these calls here are idempotent, but a closed flag in AbstractInstrument would future-proof that idempotency. Perhaps I'm over-thinking this though - I'll leave it up to your discretion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've thought about this, but ultimately decided not to do that -- as you're saying, the current close() logic is idempotent, the probability that someone calls close() more than once is somewhat small, and it's simpler to keep it as it is (and keep the wrapper over OTel reasonably thin). We can always add it later, if/when there's something that'll require that.

@JVerwolf JVerwolf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall this looks good to me. I've left a few comments/thoughts, but they can be addressed at your discretion.

@elasticsearchmachine

Copy link
Copy Markdown
Collaborator

Hi @mateuszrzeszutek, I've created a changelog YAML for you.

@github-actions

Copy link
Copy Markdown
Contributor

🔍 Preview links for changed docs

⏳ Building and deploying preview... View progress

This comment will be updated with preview links when the build is complete.

@github-actions

Copy link
Copy Markdown
Contributor

ℹ️ Important: Docs version tagging

👋 Thanks for updating the docs! Just a friendly reminder that our docs are now cumulative. This means all 9.x versions are documented on the same page and published off of the main branch, instead of creating separate pages for each minor version.

We use applies_to tags to mark version-specific features and changes.

Expand for a quick overview

When to use applies_to tags:

✅ At the page level to indicate which products/deployments the content applies to (mandatory)
✅ When features change state (e.g. preview, ga) in a specific version
✅ When availability differs across deployments and environments

What NOT to do:

❌ Don't remove or replace information that applies to an older version
❌ Don't add new information that applies to a specific version without an applies_to tag
❌ Don't forget that applies_to tags can be used at the page, section, and inline level

🤔 Need help?

@mateuszrzeszutek
mateuszrzeszutek merged commit 4351de7 into elastic:main May 11, 2026
38 checks passed
drempapis pushed a commit to drempapis/elasticsearch that referenced this pull request May 13, 2026
* Closing async instrument removes it from registry

This allows the users of the async instruments to close them and stop
them from recording measurements when the underlying conditions change
(e.g. master-specific metrics can be stopped when a new master is
elected). This way, the async instruments can be GC'd, and won't block
the measured object from being GC'd as well.

* Apply code review comments

* Extract common async logic to a base class

* Update docs/changelog/148411.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>bug :Core/Infra/Metrics Metrics and metering infrastructure Team:Core/Infra Meta label for core/infra team v9.5.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants