Skip to content

Commit e5b7f5d

Browse files
ssmelovdariusclayCopilot
authored
Fix KeyNotFoundException on HttpRequestLatencyListener.OnEventWritten for uknown event sources (#6821)
* Fix KeyNotFoundException on HttpRequestLatencyListener.OnEventWritten for unknown event sources * Update test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Latency/Internal/HttpRequestLatencyListenerTest.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Darius Letterman <dcletterman@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 6fb8ab7 commit e5b7f5d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Libraries/Microsoft.Extensions.Http.Diagnostics/Latency/Internal/HttpRequestLatencyListener.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public void Enable()
4949
internal void OnEventWritten(string eventSourceName, string? eventName)
5050
{
5151
// If event of interest, add a checkpoint for it.
52-
if (eventName != null && _eventToTokenMap[eventSourceName].TryGetValue(eventName, out var token))
52+
if (eventName != null
53+
&& _eventToTokenMap.TryGetValue(eventSourceName, out var tokenMap)
54+
&& tokenMap.TryGetValue(eventName, out var token))
5355
{
5456
LatencyContext.Get()?.AddCheckpoint(token);
5557
}

test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Latency/Internal/HttpRequestLatencyListenerTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,19 @@ public void HttpRequestLatencyListener_OnEventWritten_AddsCheckpoints_Http()
191191
lc.Verify(a => a.AddCheckpoint(It.IsAny<CheckpointToken>()),
192192
Times.Exactly(numHttpEvents + numSocketEvents + numDnsEvents));
193193
}
194+
195+
[Fact]
196+
public void HttpRequestLatencyListener_OnEventWritten_DoesNotAddCheckpoints_UnknownEventSource()
197+
{
198+
var lcti = HttpMockProvider.GetTokenIssuer();
199+
var lc = HttpMockProvider.GetLatencyContext();
200+
var context = new HttpClientLatencyContext();
201+
context.Set(lc.Object);
202+
203+
using var listener = HttpMockProvider.GetListener(context, lcti.Object);
204+
205+
listener.OnEventWritten("System.Runtime", "EventCounters");
206+
207+
lc.Verify(a => a.AddCheckpoint(It.IsAny<CheckpointToken>()), Times.Never);
208+
}
194209
}

0 commit comments

Comments
 (0)