@@ -672,6 +672,9 @@ public async Task AttemptingToWriteZeroContentLengthFor2xxResponsesOnConnect_Con
672
672
673
673
private async Task AttemptingToWriteNonzeroContentLengthFails ( int statusCode , HttpMethod method )
674
674
{
675
+ var testMeterFactory = new TestMeterFactory ( ) ;
676
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
677
+
675
678
var responseWriteTcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
676
679
677
680
await using ( var server = new TestServer ( async httpContext =>
@@ -690,7 +693,7 @@ private async Task AttemptingToWriteNonzeroContentLengthFails(int statusCode, Ht
690
693
}
691
694
692
695
responseWriteTcs . TrySetResult ( ) ;
693
- } , new TestServiceContext ( LoggerFactory ) ) )
696
+ } , new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) ) ) )
694
697
{
695
698
using ( var connection = server . CreateConnection ( ) )
696
699
{
@@ -704,6 +707,11 @@ await connection.Send(
704
707
Assert . Equal ( CoreStrings . FormatHeaderNotAllowedOnResponse ( "Content-Length" , statusCode ) , ex . Message ) ;
705
708
}
706
709
}
710
+
711
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
712
+ {
713
+ Assert . DoesNotContain ( KestrelMetrics . ErrorType , m . Tags . Keys ) ;
714
+ } ) ;
707
715
}
708
716
709
717
private async Task AttemptingToWriteZeroContentLength_ContentLengthRemoved ( int statusCode , HttpMethod method )
@@ -944,7 +952,10 @@ await connection.Receive(
944
952
[ Fact ]
945
953
public async Task ThrowsAndClosesConnectionWhenAppWritesMoreThanContentLengthWrite ( )
946
954
{
947
- var serviceContext = new TestServiceContext ( LoggerFactory )
955
+ var testMeterFactory = new TestMeterFactory ( ) ;
956
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
957
+
958
+ var serviceContext = new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) )
948
959
{
949
960
ServerOptions = { AllowSynchronousIO = true }
950
961
} ;
@@ -979,12 +990,19 @@ await connection.Receive(
979
990
Assert . Equal (
980
991
$ "Response Content-Length mismatch: too many bytes written (12 of 11).",
981
992
logMessage . Exception . Message ) ;
993
+
994
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
995
+ {
996
+ Assert . Equal ( KestrelMetrics . GetErrorType ( ConnectionEndReason . ResponseContentLengthMismatch ) , m . Tags [ KestrelMetrics . ErrorType ] ) ;
997
+ } ) ;
982
998
}
983
999
984
1000
[ Fact ]
985
1001
public async Task ThrowsAndClosesConnectionWhenAppWritesMoreThanContentLengthWriteAsync ( )
986
1002
{
987
- var serviceContext = new TestServiceContext ( LoggerFactory ) ;
1003
+ var testMeterFactory = new TestMeterFactory ( ) ;
1004
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
1005
+ var serviceContext = new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) ) ;
988
1006
989
1007
await using ( var server = new TestServer ( async httpContext =>
990
1008
{
@@ -1013,6 +1031,11 @@ await connection.ReceiveEnd(
1013
1031
Assert . Equal (
1014
1032
$ "Response Content-Length mismatch: too many bytes written (12 of 11).",
1015
1033
logMessage . Exception . Message ) ;
1034
+
1035
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
1036
+ {
1037
+ Assert . Equal ( KestrelMetrics . GetErrorType ( ConnectionEndReason . ResponseContentLengthMismatch ) , m . Tags [ KestrelMetrics . ErrorType ] ) ;
1038
+ } ) ;
1016
1039
}
1017
1040
1018
1041
[ Fact ]
@@ -2404,7 +2427,9 @@ await connection.ReceiveEnd(
2404
2427
[ Fact ]
2405
2428
public async Task ThrowingResultsIn500Response ( )
2406
2429
{
2407
- var testContext = new TestServiceContext ( LoggerFactory ) ;
2430
+ var testMeterFactory = new TestMeterFactory ( ) ;
2431
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
2432
+ var testContext = new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) ) ;
2408
2433
2409
2434
bool onStartingCalled = false ;
2410
2435
@@ -2449,6 +2474,11 @@ await connection.ReceiveEnd(
2449
2474
2450
2475
Assert . False ( onStartingCalled ) ;
2451
2476
Assert . Equal ( 2 , LogMessages . Where ( message => message . LogLevel == LogLevel . Error ) . Count ( ) ) ;
2477
+
2478
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
2479
+ {
2480
+ Assert . DoesNotContain ( KestrelMetrics . ErrorType , m . Tags . Keys ) ;
2481
+ } ) ;
2452
2482
}
2453
2483
2454
2484
[ Fact ]
@@ -2603,7 +2633,9 @@ await connection.Receive(
2603
2633
[ Fact ]
2604
2634
public async Task ThrowingInOnCompletedIsLogged ( )
2605
2635
{
2606
- var testContext = new TestServiceContext ( LoggerFactory ) ;
2636
+ var testMeterFactory = new TestMeterFactory ( ) ;
2637
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
2638
+ var testContext = new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) ) ;
2607
2639
2608
2640
var onCompletedCalled1 = false ;
2609
2641
var onCompletedCalled2 = false ;
@@ -2647,12 +2679,19 @@ await connection.Receive(
2647
2679
Assert . Equal ( 2 , LogMessages . Where ( message => message . LogLevel == LogLevel . Error ) . Count ( ) ) ;
2648
2680
Assert . True ( onCompletedCalled1 ) ;
2649
2681
Assert . True ( onCompletedCalled2 ) ;
2682
+
2683
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
2684
+ {
2685
+ Assert . DoesNotContain ( KestrelMetrics . ErrorType , m . Tags . Keys ) ;
2686
+ } ) ;
2650
2687
}
2651
2688
2652
2689
[ Fact ]
2653
2690
public async Task ThrowingAfterWritingKillsConnection ( )
2654
2691
{
2655
- var testContext = new TestServiceContext ( LoggerFactory ) ;
2692
+ var testMeterFactory = new TestMeterFactory ( ) ;
2693
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
2694
+ var testContext = new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) ) ;
2656
2695
2657
2696
bool onStartingCalled = false ;
2658
2697
@@ -2688,12 +2727,19 @@ await connection.ReceiveEnd(
2688
2727
2689
2728
Assert . True ( onStartingCalled ) ;
2690
2729
Assert . Single ( LogMessages , message => message . LogLevel == LogLevel . Error ) ;
2730
+
2731
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
2732
+ {
2733
+ Assert . Equal ( KestrelMetrics . GetErrorType ( ConnectionEndReason . ErrorAfterStartingResponse ) , m . Tags [ KestrelMetrics . ErrorType ] ) ;
2734
+ } ) ;
2691
2735
}
2692
2736
2693
2737
[ Fact ]
2694
2738
public async Task ThrowingAfterPartialWriteKillsConnection ( )
2695
2739
{
2696
- var testContext = new TestServiceContext ( LoggerFactory ) ;
2740
+ var testMeterFactory = new TestMeterFactory ( ) ;
2741
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
2742
+ var testContext = new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) ) ;
2697
2743
2698
2744
bool onStartingCalled = false ;
2699
2745
@@ -2729,6 +2775,11 @@ await connection.ReceiveEnd(
2729
2775
2730
2776
Assert . True ( onStartingCalled ) ;
2731
2777
Assert . Single ( LogMessages , message => message . LogLevel == LogLevel . Error ) ;
2778
+
2779
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
2780
+ {
2781
+ Assert . Equal ( KestrelMetrics . GetErrorType ( ConnectionEndReason . ErrorAfterStartingResponse ) , m . Tags [ KestrelMetrics . ErrorType ] ) ;
2782
+ } ) ;
2732
2783
}
2733
2784
2734
2785
[ Fact ]
@@ -2788,7 +2839,9 @@ await connection.Send(
2788
2839
[ Fact ]
2789
2840
public async Task AppAbortIsLogged ( )
2790
2841
{
2791
- var testContext = new TestServiceContext ( LoggerFactory ) ;
2842
+ var testMeterFactory = new TestMeterFactory ( ) ;
2843
+ using var connectionDuration = new MetricCollector < double > ( testMeterFactory , "Microsoft.AspNetCore.Server.Kestrel" , "kestrel.connection.duration" ) ;
2844
+ var testContext = new TestServiceContext ( LoggerFactory , metrics : new KestrelMetrics ( testMeterFactory ) ) ;
2792
2845
2793
2846
await using ( var server = new TestServer ( httpContext =>
2794
2847
{
@@ -2808,6 +2861,11 @@ await connection.Send(
2808
2861
}
2809
2862
2810
2863
Assert . Single ( LogMessages . Where ( m => m . Message . Contains ( CoreStrings . ConnectionAbortedByApplication ) ) ) ;
2864
+
2865
+ Assert . Collection ( connectionDuration . GetMeasurementSnapshot ( ) , m =>
2866
+ {
2867
+ Assert . Equal ( KestrelMetrics . GetErrorType ( ConnectionEndReason . AbortedByApp ) , m . Tags [ KestrelMetrics . ErrorType ] ) ;
2868
+ } ) ;
2811
2869
}
2812
2870
2813
2871
[ Fact ]
0 commit comments