Cache request values when hosting metrics are enabled #55351
Merged
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.
Fixes #55280
Hosting metrics use data on
HttpContext
when writing tag values. For example, the HTTP method of a request is metadata when writing metrics.However, the context is mutable, which means that someone can modify the values used by hosting metrics during the request pipeline. This has the worst impact on the active requests counter, which depends on tags being the same for the start and the end of the request. If tags don't match then you end up with two dimensions, one going up and the other going down:
Fix is to stash the initial values on the context (this PR puts them on the metrics tags feature which is on the context) when the request starts and then use the stashed values at the end of the request.
There is slightly more memory used per request when metrics are enabled (3 additional fields), and no impact when metrics are disabled.