feat(metrics): Stronger recursion protection#2426
Merged
Conversation
mitsuhiko
commented
Oct 10, 2023
| # or similar will be disabled. In the background thread this can | ||
| # never happen, but in the force flush case which happens in the | ||
| # foreground we might make it here unprotected. | ||
| with recursion_protection(): |
Contributor
Author
There was a problem hiding this comment.
In the background thread, this is basically always a no-op as in_metrics is forcefully set to True. In the force flush case we might be flipping it here. Why is flush itself unprotected? Two reasons:
- on the sentry side we currently patch it, so making it a noop would break the logic there
- it's potentially brittle since
_emitis invoked in more than one place, this should be safer.
| if callback is not None: | ||
| if not callback(key, updated_tags): | ||
| return None, updated_tags | ||
| with recursion_protection() as in_metrics: |
Contributor
Author
There was a problem hiding this comment.
Motivation here is to intentionally protect the callback. That way we only poke around on the thread local when we need to. This also means any user of the _get_aggregator_and_update_tags gets automatic protection and does not need to manually take care of marking their functions as noop.
iambriccardo
approved these changes
Oct 10, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This strengthens the recursion protection to cover two more cases:
before_emit_metricin itself reporting a metric. This was previously not protectedRefs getsentry/sentry#57791