@@ -845,7 +845,8 @@ public async Task ShouldExportActivityWithOneOrMoreExceptionFilters(int mode)
845845 public async Task DiagnosticSourceCallbacksAreReceivedOnlyForSubscribedEvents ( )
846846 {
847847 int numberOfUnSubscribedEvents = 0 ;
848- int numberofSubscribedEvents = 0 ;
848+ int numberOfSubscribedEvents = 0 ;
849+
849850 void ConfigureTestServices ( IServiceCollection services )
850851 {
851852 this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
@@ -858,19 +859,19 @@ void ConfigureTestServices(IServiceCollection services)
858859 {
859860 case HttpInListener . OnStartEvent :
860861 {
861- numberofSubscribedEvents ++ ;
862+ numberOfSubscribedEvents ++ ;
862863 }
863864
864865 break ;
865866 case HttpInListener . OnStopEvent :
866867 {
867- numberofSubscribedEvents ++ ;
868+ numberOfSubscribedEvents ++ ;
868869 }
869870
870871 break ;
871872 case HttpInListener . OnMvcBeforeActionEvent :
872873 {
873- numberofSubscribedEvents ++ ;
874+ numberOfSubscribedEvents ++ ;
874875 }
875876
876877 break ;
@@ -884,6 +885,10 @@ void ConfigureTestServices(IServiceCollection services)
884885 } ,
885886 } )
886887 . Build ( ) ;
888+
889+ services . AddOpenTelemetry ( )
890+ . WithTracing ( builder => builder
891+ . AddInstrumentation ( ( ) => this . tracerProvider ) ) ;
887892 }
888893
889894 // Arrange
@@ -902,15 +907,16 @@ void ConfigureTestServices(IServiceCollection services)
902907 }
903908
904909 Assert . Equal ( 0 , numberOfUnSubscribedEvents ) ;
905- Assert . Equal ( 3 , numberofSubscribedEvents ) ;
910+ Assert . Equal ( 3 , numberOfSubscribedEvents ) ;
906911 }
907912
908913 [ Fact ]
909914 public async Task DiagnosticSourceExceptionCallbackIsReceivedForUnHandledException ( )
910915 {
911916 int numberOfUnSubscribedEvents = 0 ;
912- int numberofSubscribedEvents = 0 ;
917+ int numberOfSubscribedEvents = 0 ;
913918 int numberOfExceptionCallbacks = 0 ;
919+
914920 void ConfigureTestServices ( IServiceCollection services )
915921 {
916922 this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
@@ -923,19 +929,19 @@ void ConfigureTestServices(IServiceCollection services)
923929 {
924930 case HttpInListener . OnStartEvent :
925931 {
926- numberofSubscribedEvents ++ ;
932+ numberOfSubscribedEvents ++ ;
927933 }
928934
929935 break ;
930936 case HttpInListener . OnStopEvent :
931937 {
932- numberofSubscribedEvents ++ ;
938+ numberOfSubscribedEvents ++ ;
933939 }
934940
935941 break ;
936942 case HttpInListener . OnMvcBeforeActionEvent :
937943 {
938- numberofSubscribedEvents ++ ;
944+ numberOfSubscribedEvents ++ ;
939945 }
940946
941947 break ;
@@ -945,7 +951,7 @@ void ConfigureTestServices(IServiceCollection services)
945951 case HttpInListener . OnUnhandledHostingExceptionEvent :
946952 case HttpInListener . OnUnHandledDiagnosticsExceptionEvent :
947953 {
948- numberofSubscribedEvents ++ ;
954+ numberOfSubscribedEvents ++ ;
949955 numberOfExceptionCallbacks ++ ;
950956 }
951957
@@ -960,6 +966,10 @@ void ConfigureTestServices(IServiceCollection services)
960966 } ,
961967 } )
962968 . Build ( ) ;
969+
970+ services . AddOpenTelemetry ( )
971+ . WithTracing ( builder => builder
972+ . AddInstrumentation ( ( ) => this . tracerProvider ) ) ;
963973 }
964974
965975 // Arrange
@@ -986,18 +996,20 @@ void ConfigureTestServices(IServiceCollection services)
986996
987997 Assert . Equal ( 1 , numberOfExceptionCallbacks ) ;
988998 Assert . Equal ( 0 , numberOfUnSubscribedEvents ) ;
989- Assert . Equal ( 4 , numberofSubscribedEvents ) ;
999+ Assert . Equal ( 4 , numberOfSubscribedEvents ) ;
9901000 }
9911001
9921002 [ Fact ( Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/4884" ) ]
9931003 public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHandledInMiddleware ( )
9941004 {
9951005 int numberOfUnSubscribedEvents = 0 ;
996- int numberofSubscribedEvents = 0 ;
1006+ int numberOfSubscribedEvents = 0 ;
9971007 int numberOfExceptionCallbacks = 0 ;
9981008
999- // configure SDK
1000- using var tracerprovider = Sdk . CreateTracerProviderBuilder ( )
1009+ void ConfigureTestServices ( IServiceCollection services )
1010+ {
1011+ // configure SDK
1012+ this . tracerProvider = Sdk . CreateTracerProviderBuilder ( )
10011013 . AddAspNetCoreInstrumentation (
10021014 new TestHttpInListener ( new AspNetCoreInstrumentationOptions ( ) )
10031015 {
@@ -1007,13 +1019,13 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
10071019 {
10081020 case HttpInListener . OnStartEvent :
10091021 {
1010- numberofSubscribedEvents ++ ;
1022+ numberOfSubscribedEvents ++ ;
10111023 }
10121024
10131025 break ;
10141026 case HttpInListener . OnStopEvent :
10151027 {
1016- numberofSubscribedEvents ++ ;
1028+ numberOfSubscribedEvents ++ ;
10171029 }
10181030
10191031 break ;
@@ -1023,7 +1035,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
10231035 case HttpInListener . OnUnhandledHostingExceptionEvent :
10241036 case HttpInListener . OnUnHandledDiagnosticsExceptionEvent :
10251037 {
1026- numberofSubscribedEvents ++ ;
1038+ numberOfSubscribedEvents ++ ;
10271039 numberOfExceptionCallbacks ++ ;
10281040 }
10291041
@@ -1039,45 +1051,43 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
10391051 } )
10401052 . Build ( ) ;
10411053
1042- var builder = WebApplication . CreateBuilder ( ) ;
1043- builder . Logging . ClearProviders ( ) ;
1044- var app = builder . Build ( ) ;
1054+ services . AddOpenTelemetry ( )
1055+ . WithTracing ( builder => builder
1056+ . AddInstrumentation ( ( ) => this . tracerProvider ) ) ;
1057+ }
10451058
1046- app . UseExceptionHandler ( handler =>
1047- {
1048- handler . Run ( async ( ctx ) =>
1059+ using ( var client = this . factory
1060+ . WithWebHostBuilder ( builder =>
10491061 {
1050- await ctx . Response . WriteAsync ( "handled" ) . ConfigureAwait ( false ) ;
1051- } ) ;
1052- } ) ;
1053-
1054- app . Map ( "/error" , ThrowException ) ;
1055-
1056- static void ThrowException ( IApplicationBuilder app )
1062+ builder . ConfigureTestServices ( ConfigureTestServices ) ;
1063+ builder . ConfigureLogging ( loggingBuilder => loggingBuilder . ClearProviders ( ) ) ;
1064+ builder . Configure ( app => app
1065+ . UseExceptionHandler ( handler =>
1066+ {
1067+ handler . Run ( async ( ctx ) =>
1068+ {
1069+ await ctx . Response . WriteAsync ( "handled" ) . ConfigureAwait ( false ) ;
1070+ } ) ;
1071+ } ) ) ;
1072+ } )
1073+ . CreateClient ( ) )
10571074 {
1058- app . Run ( context =>
1075+ try
10591076 {
1060- throw new Exception ( "CustomException" ) ;
1061- } ) ;
1062- }
1063-
1064- _ = app . RunAsync ( ) ;
1077+ using var request = new HttpRequestMessage ( HttpMethod . Get , "/api/error" ) ;
10651078
1066- using var client = new HttpClient ( ) ;
1067- try
1068- {
1069- await client . GetStringAsync ( "http://localhost:5000/error" ) . ConfigureAwait ( false ) ;
1070- }
1071- catch
1072- {
1073- // ignore 500 error.
1079+ // Act
1080+ using var response = await client . SendAsync ( request ) . ConfigureAwait ( false ) ;
1081+ }
1082+ catch
1083+ {
1084+ // ignore exception
1085+ }
10741086 }
10751087
10761088 Assert . Equal ( 0 , numberOfExceptionCallbacks ) ;
10771089 Assert . Equal ( 0 , numberOfUnSubscribedEvents ) ;
1078- Assert . Equal ( 2 , numberofSubscribedEvents ) ;
1079-
1080- await app . DisposeAsync ( ) . ConfigureAwait ( false ) ;
1090+ Assert . Equal ( 2 , numberOfSubscribedEvents ) ;
10811091 }
10821092
10831093 [ Fact ]
@@ -1093,8 +1103,6 @@ void ConfigureTestServices(IServiceCollection services)
10931103 . AddInMemoryExporter ( exportedItems )
10941104 . Build ( ) ;
10951105
1096- var meterFactory = this . factory . Services . GetRequiredService < IMeterFactory > ( ) ;
1097- services . AddSingleton ( meterFactory ) ;
10981106 services . AddRouting ( ) ;
10991107 services . AddOpenTelemetry ( )
11001108 . WithTracing ( builder =>
@@ -1220,16 +1228,10 @@ private void ConfigureExceptionFilters(IServiceCollection services, int mode, re
12201228 . Build ( ) ;
12211229 }
12221230
1223- private class ExtractOnlyPropagator : TextMapPropagator
1231+ private class ExtractOnlyPropagator ( ActivityContext activityContext , Baggage baggage ) : TextMapPropagator
12241232 {
1225- private readonly ActivityContext activityContext ;
1226- private readonly Baggage baggage ;
1227-
1228- public ExtractOnlyPropagator ( ActivityContext activityContext , Baggage baggage )
1229- {
1230- this . activityContext = activityContext ;
1231- this . baggage = baggage ;
1232- }
1233+ private readonly ActivityContext activityContext = activityContext ;
1234+ private readonly Baggage baggage = baggage ;
12331235
12341236 public override ISet < string > Fields => throw new NotImplementedException ( ) ;
12351237
@@ -1244,32 +1246,21 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
12441246 }
12451247 }
12461248
1247- private class TestSampler : Sampler
1249+ private class TestSampler ( SamplingDecision samplingDecision , IEnumerable < KeyValuePair < string , object > > attributes = null ) : Sampler
12481250 {
1249- private readonly SamplingDecision samplingDecision ;
1250- private readonly IEnumerable < KeyValuePair < string , object > > attributes ;
1251-
1252- public TestSampler ( SamplingDecision samplingDecision , IEnumerable < KeyValuePair < string , object > > attributes = null )
1253- {
1254- this . samplingDecision = samplingDecision ;
1255- this . attributes = attributes ;
1256- }
1251+ private readonly SamplingDecision samplingDecision = samplingDecision ;
1252+ private readonly IEnumerable < KeyValuePair < string , object > > attributes = attributes ;
12571253
12581254 public override SamplingResult ShouldSample ( in SamplingParameters samplingParameters )
12591255 {
12601256 return new SamplingResult ( this . samplingDecision , this . attributes ) ;
12611257 }
12621258 }
12631259
1264- private class TestHttpInListener : HttpInListener
1260+ private class TestHttpInListener ( AspNetCoreInstrumentationOptions options ) : HttpInListener ( options )
12651261 {
12661262 public Action < string , object > OnEventWrittenCallback ;
12671263
1268- public TestHttpInListener ( AspNetCoreInstrumentationOptions options )
1269- : base ( options )
1270- {
1271- }
1272-
12731264 public override void OnEventWritten ( string name , object payload )
12741265 {
12751266 base . OnEventWritten ( name , payload ) ;
@@ -1278,17 +1269,11 @@ public override void OnEventWritten(string name, object payload)
12781269 }
12791270 }
12801271
1281- private class TestNullHostActivityMiddlewareImpl : ActivityMiddleware . ActivityMiddlewareImpl
1272+ private class TestNullHostActivityMiddlewareImpl ( string activitySourceName , string activityName ) : ActivityMiddleware . ActivityMiddlewareImpl
12821273 {
1283- private ActivitySource activitySource ;
1274+ private readonly ActivitySource activitySource = new ( activitySourceName ) ;
1275+ private readonly string activityName = activityName ;
12841276 private Activity activity ;
1285- private string activityName ;
1286-
1287- public TestNullHostActivityMiddlewareImpl ( string activitySourceName , string activityName )
1288- {
1289- this . activitySource = new ActivitySource ( activitySourceName ) ;
1290- this . activityName = activityName ;
1291- }
12921277
12931278 public override void PreProcess ( HttpContext context )
12941279 {
@@ -1306,17 +1291,11 @@ public override void PostProcess(HttpContext context)
13061291 }
13071292 }
13081293
1309- private class TestActivityMiddlewareImpl : ActivityMiddleware . ActivityMiddlewareImpl
1294+ private class TestActivityMiddlewareImpl ( string activitySourceName , string activityName ) : ActivityMiddleware . ActivityMiddlewareImpl
13101295 {
1311- private ActivitySource activitySource ;
1296+ private readonly ActivitySource activitySource = new ( activitySourceName ) ;
1297+ private readonly string activityName = activityName ;
13121298 private Activity activity ;
1313- private string activityName ;
1314-
1315- public TestActivityMiddlewareImpl ( string activitySourceName , string activityName )
1316- {
1317- this . activitySource = new ActivitySource ( activitySourceName ) ;
1318- this . activityName = activityName ;
1319- }
13201299
13211300 public override void PreProcess ( HttpContext context )
13221301 {
0 commit comments