2323using Microsoft . AspNetCore . TestHost ;
2424using Microsoft . Extensions . Configuration ;
2525using Microsoft . Extensions . DependencyInjection ;
26+ using Microsoft . Extensions . Hosting ;
2627using Microsoft . Extensions . Logging ;
2728using Moq ;
2829using OpenTelemetry . Context . Propagation ;
@@ -840,45 +841,42 @@ public async Task DiagnosticSourceCallbacksAreReceivedOnlyForSubscribedEvents()
840841 {
841842 int numberOfUnSubscribedEvents = 0 ;
842843 int numberofSubscribedEvents = 0 ;
843- void ConfigureTestServices ( IServiceCollection services )
844- {
845- this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
846- . AddAspNetCoreInstrumentation (
847- new TestHttpInListener ( new AspNetCoreInstrumentationOptions ( ) )
844+
845+ this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
846+ . AddAspNetCoreInstrumentation (
847+ new TestHttpInListener ( new AspNetCoreInstrumentationOptions ( ) )
848+ {
849+ OnEventWrittenCallback = ( name , payload ) =>
848850 {
849- OnEventWrittenCallback = ( name , payload ) =>
851+ switch ( name )
850852 {
851- switch ( name )
852- {
853- case HttpInListener . OnStartEvent :
854- {
855- numberofSubscribedEvents ++ ;
856- }
853+ case HttpInListener . OnStartEvent :
854+ {
855+ numberofSubscribedEvents ++ ;
856+ }
857857
858- break ;
859- case HttpInListener . OnStopEvent :
860- {
861- numberofSubscribedEvents ++ ;
862- }
858+ break ;
859+ case HttpInListener . OnStopEvent :
860+ {
861+ numberofSubscribedEvents ++ ;
862+ }
863863
864- break ;
865- default :
866- {
867- numberOfUnSubscribedEvents ++ ;
868- }
864+ break ;
865+ default :
866+ {
867+ numberOfUnSubscribedEvents ++ ;
868+ }
869869
870- break ;
871- }
872- } ,
873- } )
874- . Build ( ) ;
875- }
870+ break ;
871+ }
872+ } ,
873+ } )
874+ . Build ( ) ;
876875
877876 // Arrange
878877 using ( var client = this . factory
879878 . WithWebHostBuilder ( builder =>
880879 {
881- builder . ConfigureTestServices ( ConfigureTestServices ) ;
882880 builder . ConfigureLogging ( loggingBuilder => loggingBuilder . ClearProviders ( ) ) ;
883881 } )
884882 . CreateClient ( ) )
@@ -899,56 +897,53 @@ public async Task DiagnosticSourceExceptionCallbackIsReceivedForUnHandledExcepti
899897 int numberOfUnSubscribedEvents = 0 ;
900898 int numberofSubscribedEvents = 0 ;
901899 int numberOfExceptionCallbacks = 0 ;
902- void ConfigureTestServices ( IServiceCollection services )
903- {
904- this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
905- . AddAspNetCoreInstrumentation (
906- new TestHttpInListener ( new AspNetCoreInstrumentationOptions ( ) )
900+
901+ this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
902+ . AddAspNetCoreInstrumentation (
903+ new TestHttpInListener ( new AspNetCoreInstrumentationOptions ( ) )
904+ {
905+ OnEventWrittenCallback = ( name , payload ) =>
907906 {
908- OnEventWrittenCallback = ( name , payload ) =>
907+ switch ( name )
909908 {
910- switch ( name )
911- {
912- case HttpInListener . OnStartEvent :
913- {
914- numberofSubscribedEvents ++ ;
915- }
909+ case HttpInListener . OnStartEvent :
910+ {
911+ numberofSubscribedEvents ++ ;
912+ }
916913
917- break ;
918- case HttpInListener . OnStopEvent :
919- {
920- numberofSubscribedEvents ++ ;
921- }
914+ break ;
915+ case HttpInListener . OnStopEvent :
916+ {
917+ numberofSubscribedEvents ++ ;
918+ }
922919
923- break ;
920+ break ;
924921
925- // TODO: Add test case for validating name for both the types
926- // of exception event.
927- case HttpInListener . OnUnhandledHostingExceptionEvent :
928- case HttpInListener . OnUnHandledDiagnosticsExceptionEvent :
929- {
930- numberofSubscribedEvents ++ ;
931- numberOfExceptionCallbacks ++ ;
932- }
922+ // TODO: Add test case for validating name for both the types
923+ // of exception event.
924+ case HttpInListener . OnUnhandledHostingExceptionEvent :
925+ case HttpInListener . OnUnHandledDiagnosticsExceptionEvent :
926+ {
927+ numberofSubscribedEvents ++ ;
928+ numberOfExceptionCallbacks ++ ;
929+ }
933930
934- break ;
935- default :
936- {
937- numberOfUnSubscribedEvents ++ ;
938- }
931+ break ;
932+ default :
933+ {
934+ numberOfUnSubscribedEvents ++ ;
935+ }
939936
940- break ;
941- }
942- } ,
943- } )
944- . Build ( ) ;
945- }
937+ break ;
938+ }
939+ } ,
940+ } )
941+ . Build ( ) ;
946942
947943 // Arrange
948944 using ( var client = this . factory
949945 . WithWebHostBuilder ( builder =>
950946 {
951- builder . ConfigureTestServices ( ConfigureTestServices ) ;
952947 builder . ConfigureLogging ( loggingBuilder => loggingBuilder . ClearProviders ( ) ) ;
953948 } )
954949 . CreateClient ( ) )
@@ -971,15 +966,15 @@ void ConfigureTestServices(IServiceCollection services)
971966 Assert . Equal ( 3 , numberofSubscribedEvents ) ;
972967 }
973968
974- [ Fact ( Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/4884" ) ]
969+ [ Fact ]
975970 public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHandledInMiddleware ( )
976971 {
977972 int numberOfUnSubscribedEvents = 0 ;
978- int numberofSubscribedEvents = 0 ;
973+ int numberOfSubscribedEvents = 0 ;
979974 int numberOfExceptionCallbacks = 0 ;
980975
981976 // configure SDK
982- using var tracerprovider = Sdk . CreateTracerProviderBuilder ( )
977+ this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
983978 . AddAspNetCoreInstrumentation (
984979 new TestHttpInListener ( new AspNetCoreInstrumentationOptions ( ) )
985980 {
@@ -989,13 +984,13 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
989984 {
990985 case HttpInListener . OnStartEvent :
991986 {
992- numberofSubscribedEvents ++ ;
987+ numberOfSubscribedEvents ++ ;
993988 }
994989
995990 break ;
996991 case HttpInListener . OnStopEvent :
997992 {
998- numberofSubscribedEvents ++ ;
993+ numberOfSubscribedEvents ++ ;
999994 }
1000995
1001996 break ;
@@ -1005,7 +1000,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
10051000 case HttpInListener . OnUnhandledHostingExceptionEvent :
10061001 case HttpInListener . OnUnHandledDiagnosticsExceptionEvent :
10071002 {
1008- numberofSubscribedEvents ++ ;
1003+ numberOfSubscribedEvents ++ ;
10091004 numberOfExceptionCallbacks ++ ;
10101005 }
10111006
@@ -1021,45 +1016,35 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
10211016 } )
10221017 . Build ( ) ;
10231018
1024- var builder = WebApplication . CreateBuilder ( ) ;
1025- builder . Logging . ClearProviders ( ) ;
1026- var app = builder . Build ( ) ;
1027-
1028- app . UseExceptionHandler ( handler =>
1029- {
1030- handler . Run ( async ( ctx ) =>
1019+ using ( var client = this . factory
1020+ . WithWebHostBuilder ( builder =>
10311021 {
1032- await ctx . Response . WriteAsync ( "handled" ) ;
1033- } ) ;
1034- } ) ;
1035-
1036- app . Map ( "/error" , ThrowException ) ;
1037-
1038- static void ThrowException ( IApplicationBuilder app )
1022+ builder . ConfigureLogging ( loggingBuilder => loggingBuilder . ClearProviders ( ) ) ;
1023+ builder . Configure ( app => app
1024+ . UseExceptionHandler ( handler =>
1025+ {
1026+ handler . Run ( async ( ctx ) =>
1027+ {
1028+ await ctx . Response . WriteAsync ( "handled" ) ;
1029+ } ) ;
1030+ } ) ) ;
1031+ } )
1032+ . CreateClient ( ) )
10391033 {
1040- app . Run ( context =>
1034+ try
10411035 {
1042- throw new Exception ( "CustomException" ) ;
1043- } ) ;
1044- }
1045-
1046- _ = app . RunAsync ( ) ;
1047-
1048- using var client = new HttpClient ( ) ;
1049- try
1050- {
1051- await client . GetStringAsync ( "http://localhost:5000/error" ) ;
1052- }
1053- catch
1054- {
1055- // ignore 500 error.
1036+ using var request = new HttpRequestMessage ( HttpMethod . Get , "/api/error" ) ;
1037+ using var response = await client . SendAsync ( request ) ;
1038+ }
1039+ catch
1040+ {
1041+ // ignore exception
1042+ }
10561043 }
10571044
10581045 Assert . Equal ( 0 , numberOfExceptionCallbacks ) ;
10591046 Assert . Equal ( 0 , numberOfUnSubscribedEvents ) ;
1060- Assert . Equal ( 2 , numberofSubscribedEvents ) ;
1061-
1062- await app . DisposeAsync ( ) ;
1047+ Assert . Equal ( 2 , numberOfSubscribedEvents ) ;
10631048 }
10641049
10651050 public void Dispose ( )
@@ -1128,16 +1113,10 @@ private void ConfigureExceptionFilters(IServiceCollection services, int mode, re
11281113 . Build ( ) ;
11291114 }
11301115
1131- private class ExtractOnlyPropagator : TextMapPropagator
1116+ private class ExtractOnlyPropagator ( ActivityContext activityContext , Baggage baggage ) : TextMapPropagator
11321117 {
1133- private readonly ActivityContext activityContext ;
1134- private readonly Baggage baggage ;
1135-
1136- public ExtractOnlyPropagator ( ActivityContext activityContext , Baggage baggage )
1137- {
1138- this . activityContext = activityContext ;
1139- this . baggage = baggage ;
1140- }
1118+ private readonly ActivityContext activityContext = activityContext ;
1119+ private readonly Baggage baggage = baggage ;
11411120
11421121 public override ISet < string > Fields => throw new NotImplementedException ( ) ;
11431122
@@ -1152,32 +1131,21 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
11521131 }
11531132 }
11541133
1155- private class TestSampler : Sampler
1134+ private class TestSampler ( SamplingDecision samplingDecision , IEnumerable < KeyValuePair < string , object > > attributes = null ) : Sampler
11561135 {
1157- private readonly SamplingDecision samplingDecision ;
1158- private readonly IEnumerable < KeyValuePair < string , object > > attributes ;
1159-
1160- public TestSampler ( SamplingDecision samplingDecision , IEnumerable < KeyValuePair < string , object > > attributes = null )
1161- {
1162- this . samplingDecision = samplingDecision ;
1163- this . attributes = attributes ;
1164- }
1136+ private readonly SamplingDecision samplingDecision = samplingDecision ;
1137+ private readonly IEnumerable < KeyValuePair < string , object > > attributes = attributes ;
11651138
11661139 public override SamplingResult ShouldSample ( in SamplingParameters samplingParameters )
11671140 {
11681141 return new SamplingResult ( this . samplingDecision , this . attributes ) ;
11691142 }
11701143 }
11711144
1172- private class TestHttpInListener : HttpInListener
1145+ private class TestHttpInListener ( AspNetCoreInstrumentationOptions options ) : HttpInListener ( options )
11731146 {
11741147 public Action < string , object > OnEventWrittenCallback ;
11751148
1176- public TestHttpInListener ( AspNetCoreInstrumentationOptions options )
1177- : base ( options )
1178- {
1179- }
1180-
11811149 public override void OnEventWritten ( string name , object payload )
11821150 {
11831151 base . OnEventWritten ( name , payload ) ;
@@ -1186,17 +1154,11 @@ public override void OnEventWritten(string name, object payload)
11861154 }
11871155 }
11881156
1189- private class TestNullHostActivityMiddlewareImpl : ActivityMiddleware . ActivityMiddlewareImpl
1157+ private class TestNullHostActivityMiddlewareImpl ( string activitySourceName , string activityName ) : ActivityMiddleware . ActivityMiddlewareImpl
11901158 {
1191- private ActivitySource activitySource ;
1159+ private readonly ActivitySource activitySource = new ( activitySourceName ) ;
1160+ private readonly string activityName = activityName ;
11921161 private Activity activity ;
1193- private string activityName ;
1194-
1195- public TestNullHostActivityMiddlewareImpl ( string activitySourceName , string activityName )
1196- {
1197- this . activitySource = new ActivitySource ( activitySourceName ) ;
1198- this . activityName = activityName ;
1199- }
12001162
12011163 public override void PreProcess ( HttpContext context )
12021164 {
@@ -1214,17 +1176,11 @@ public override void PostProcess(HttpContext context)
12141176 }
12151177 }
12161178
1217- private class TestActivityMiddlewareImpl : ActivityMiddleware . ActivityMiddlewareImpl
1179+ private class TestActivityMiddlewareImpl ( string activitySourceName , string activityName ) : ActivityMiddleware . ActivityMiddlewareImpl
12181180 {
1219- private ActivitySource activitySource ;
1181+ private readonly ActivitySource activitySource = new ( activitySourceName ) ;
1182+ private readonly string activityName = activityName ;
12201183 private Activity activity ;
1221- private string activityName ;
1222-
1223- public TestActivityMiddlewareImpl ( string activitySourceName , string activityName )
1224- {
1225- this . activitySource = new ActivitySource ( activitySourceName ) ;
1226- this . activityName = activityName ;
1227- }
12281184
12291185 public override void PreProcess ( HttpContext context )
12301186 {
0 commit comments