Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion extensions/Sisk.Monitoring/ApplicationMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Sisk.Monitoring.Tests/MonitoringBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
Loading