-
Notifications
You must be signed in to change notification settings - Fork 839
Fix KeyNotFoundException on HttpRequestLatencyListener.OnEventWritten for uknown event sources #6821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix KeyNotFoundException on HttpRequestLatencyListener.OnEventWritten for uknown event sources #6821
Conversation
… for unknown event sources
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a KeyNotFoundException that occurs in HttpRequestLatencyListener.OnEventWritten when handling unknown event sources. The issue arises when events like EventCounters bypass the enabled events check, causing the method to attempt dictionary access with a non-existent key.
- Adds safe dictionary access to prevent KeyNotFoundException when unknown event sources are encountered
- Includes test coverage to verify the fix handles unknown events correctly
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
HttpRequestLatencyListener.cs | Implements safe dictionary access using TryGetValue to prevent KeyNotFoundException |
HttpRequestLatencyListenerTest.cs | Adds test to verify unknown events don't cause exceptions or add checkpoints |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
...crosoft.Extensions.Http.Diagnostics.Tests/Latency/Internal/HttpRequestLatencyListenerTest.cs
Outdated
Show resolved
Hide resolved
…ency/Internal/HttpRequestLatencyListenerTest.cs Co-authored-by: Copilot <[email protected]>
…n_for_unknown_events
@dotnet-policy-service agree company="Microsoft" |
Description
The current implementation of HttpRequestLatencyListener.OnEventWritten always expects that eventSourceName should be available in the _eventToTokenMap dictionary.
Certain events (e.g., EventCounters) bypass the enabled events check, which results in OnEventWritten throwing a KeyNotFoundException.
https://github.com/dotnet/runtime/blob/ac8354e3e94f04fae2d6d942427d025395197f76/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs#L2187
Microsoft Reviewers: Open in CodeFlow
Fixes #6822