From 6f91824f98734a763a24bbdb79ef369f3a5154d4 Mon Sep 17 00:00:00 2001 From: CypherPotato Date: Mon, 16 Nov 2026 08:40:28 -0300 Subject: [PATCH] fix(monitoring): bound route key cardinality for unknown request methods --- .../Sisk.Monitoring/ApplicationMonitor.cs | 6 ++- .../MonitoringBuilderExtensionsTests.cs | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/extensions/Sisk.Monitoring/ApplicationMonitor.cs b/extensions/Sisk.Monitoring/ApplicationMonitor.cs index 198644d..619de6f 100644 --- a/extensions/Sisk.Monitoring/ApplicationMonitor.cs +++ b/extensions/Sisk.Monitoring/ApplicationMonitor.cs @@ -1170,7 +1170,11 @@ internal void TrackHttpRequestClose ( HttpServerExecutionResult result ) { if (statusCode is not { } code) return; - string routeKey = route is null ? "(unmatched)" : $"{context.Request.Method.Method} {route.Path}"; + string routeKey = (route, context.Request.Method.Method) switch { + (null, _) => "(unmatched)", + ({ } matched, "GET" or "POST" or "PUT" or "PATCH" or "DELETE" or "HEAD" or "OPTIONS" or "QUERY") => $"{context.Request.Method.Method} {matched.Path}", + ({ } matched, _) => $"OTHER {matched.Path}" + }; httpMetrics.Record ( routeKey, code, result.Elapsed.TotalMilliseconds, result.ServerException ); } diff --git a/tests/Sisk.Monitoring.Tests/MonitoringBuilderExtensionsTests.cs b/tests/Sisk.Monitoring.Tests/MonitoringBuilderExtensionsTests.cs index 4f84d9f..96c9661 100644 --- a/tests/Sisk.Monitoring.Tests/MonitoringBuilderExtensionsTests.cs +++ b/tests/Sisk.Monitoring.Tests/MonitoringBuilderExtensionsTests.cs @@ -131,6 +131,47 @@ public async Task UseMonitoring_ServesStaticDashboardAndCollectsHttpMetrics ( bo } } + [TestMethod] + public async Task UseMonitoring_DoesNotTrackClientMethodTokensAsRoutes () { + int port = GetFreePort (); + + using var server = CreateBuilder ( port ) + .UseMonitoring ( "/monitoring", monitor => { + monitor.CredentialValidator = _ => ValueTask.FromResult ( true ); + } ) + .UseRouter ( router => { + router.MapAny ( "/any", _ => new HttpResponse ( 200 ) ); + router.MapGet ( "/late", _ => new HttpResponse ( 200 ) ); + } ) + .Build (); + + server.Start ( verbose: false, preventHault: false ); + + using var client = new HttpClient { + BaseAddress = new Uri ( $"http://127.0.0.1:{port}/" ), + Timeout = TimeSpan.FromSeconds ( 3 ) + }; + + for (int i = 0; i < 230; i++) { + using HttpResponseMessage anyResponse = await client.SendAsync ( new HttpRequestMessage ( new HttpMethod ( $"X{i}" ), "/any" ) ); + Assert.AreEqual ( HttpStatusCode.OK, anyResponse.StatusCode ); + } + + using HttpResponseMessage lateResponse = await client.GetAsync ( "/late" ); + Assert.AreEqual ( HttpStatusCode.OK, lateResponse.StatusCode ); + + using HttpResponseMessage dataResponse = await client.SendAsync ( CreateAuthorizedRequest ( "/monitoring/data" ) ); + using JsonDocument payload = JsonDocument.Parse ( await dataResponse.Content.ReadAsStringAsync () ); + + string [] routes = payload.RootElement.GetProperty ( "http" ).GetProperty ( "routes" ).EnumerateArray () + .Select ( route => route.GetProperty ( "route" ).GetString ()! ) + .ToArray (); + + CollectionAssert.Contains ( routes, "GET /late" ); + CollectionAssert.Contains ( routes, "OTHER /any" ); + Assert.IsFalse ( routes.Any ( route => route.StartsWith ( 'X' ) ), "Client method tokens must not become route keys." ); + } + [TestMethod] public async Task UseMonitoring_ServesCounterAndLogStreamDataEndpoints () { int port = GetFreePort ();