diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs index 03dc2b9d6..208b12da7 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs @@ -550,6 +550,39 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests Assert.Equal(shouldEnrichBeCalled, enrichCalled); } + [Fact(Skip = "Changes pending on instrumentation")] + public async Task ActivitiesStartedInMiddlewareShouldNotBeUpdatedByInstrumentation() + { + var exportedItems = new List(); + + var activitySourceName = "TestMiddlewareActivitySource"; + var activityName = "TestMiddlewareActivity"; + + // Arrange + using (var client = this.factory + .WithWebHostBuilder(builder => + builder.ConfigureTestServices((IServiceCollection services) => + { + services.AddSingleton(new TestActivityMiddlewareImpl(activitySourceName, activityName)); + services.AddOpenTelemetryTracing((builder) => builder.AddAspNetCoreInstrumentation() + .AddSource(activitySourceName) + .AddInMemoryExporter(exportedItems)); + })) + .CreateClient()) + { + var response = await client.GetAsync("/api/values/2"); + response.EnsureSuccessStatusCode(); + WaitForActivityExport(exportedItems, 2); + } + + Assert.Equal(2, exportedItems.Count); + + var middlewareActivity = exportedItems[0]; + + // Middleware activity name should not be changed + Assert.Equal(activityName, middlewareActivity.DisplayName); + } + public void Dispose() { this.tracerProvider?.Dispose(); @@ -661,5 +694,28 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests this.OnStopActivityCallback?.Invoke(activity, payload); } } + + private class TestActivityMiddlewareImpl : ActivityMiddleware.ActivityMiddlewareImpl + { + private ActivitySource activitySource; + private Activity activity; + private string activityName; + + public TestActivityMiddlewareImpl(string activitySourceName, string activityName) + { + this.activitySource = new ActivitySource(activitySourceName); + this.activityName = activityName; + } + + public override void PreProcess(HttpContext context) + { + this.activity = this.activitySource.StartActivity(this.activityName); + } + + public override void PostProcess(HttpContext context) + { + this.activity?.Stop(); + } + } } } diff --git a/test/TestApp.AspNetCore.3.1/ActivityMiddleware.cs b/test/TestApp.AspNetCore.3.1/ActivityMiddleware.cs new file mode 100644 index 000000000..44c1eff7d --- /dev/null +++ b/test/TestApp.AspNetCore.3.1/ActivityMiddleware.cs @@ -0,0 +1,61 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; + +namespace TestApp.AspNetCore._3._1 +{ + public class ActivityMiddleware + { + private readonly ActivityMiddlewareImpl impl; + private readonly RequestDelegate next; + + public ActivityMiddleware(RequestDelegate next, ActivityMiddlewareImpl impl) + { + this.next = next; + this.impl = impl; + } + + public async Task InvokeAsync(HttpContext context) + { + if (this.impl != null) + { + this.impl.PreProcess(context); + } + + await this.next(context); + + if (this.impl != null) + { + this.impl.PostProcess(context); + } + } + + public class ActivityMiddlewareImpl + { + public virtual void PreProcess(HttpContext context) + { + // do nothing + } + + public virtual void PostProcess(HttpContext context) + { + // do nothing + } + } + } +} diff --git a/test/TestApp.AspNetCore.3.1/Startup.cs b/test/TestApp.AspNetCore.3.1/Startup.cs index eaf926c8a..b8d84242a 100644 --- a/test/TestApp.AspNetCore.3.1/Startup.cs +++ b/test/TestApp.AspNetCore.3.1/Startup.cs @@ -39,6 +39,8 @@ namespace TestApp.AspNetCore._3._1 services.AddSingleton(); services.AddSingleton( new CallbackMiddleware.CallbackMiddlewareImpl()); + services.AddSingleton( + new ActivityMiddleware.ActivityMiddlewareImpl()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -50,6 +52,7 @@ namespace TestApp.AspNetCore._3._1 } app.UseMiddleware(); + app.UseMiddleware(); app.UseRouting(); app.UseAuthorization(); diff --git a/test/TestApp.AspNetCore.6.0/ActivityMiddleware.cs b/test/TestApp.AspNetCore.6.0/ActivityMiddleware.cs new file mode 100644 index 000000000..c45b5e828 --- /dev/null +++ b/test/TestApp.AspNetCore.6.0/ActivityMiddleware.cs @@ -0,0 +1,61 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; + +namespace TestApp.AspNetCore._6._0 +{ + public class ActivityMiddleware + { + private readonly ActivityMiddlewareImpl impl; + private readonly RequestDelegate next; + + public ActivityMiddleware(RequestDelegate next, ActivityMiddlewareImpl impl) + { + this.next = next; + this.impl = impl; + } + + public async Task InvokeAsync(HttpContext context) + { + if (this.impl != null) + { + this.impl.PreProcess(context); + } + + await this.next(context); + + if (this.impl != null) + { + this.impl.PostProcess(context); + } + } + + public class ActivityMiddlewareImpl + { + public virtual void PreProcess(HttpContext context) + { + // Do nothing + } + + public virtual void PostProcess(HttpContext context) + { + // Do nothing + } + } + } +} diff --git a/test/TestApp.AspNetCore.6.0/Startup.cs b/test/TestApp.AspNetCore.6.0/Startup.cs index 1da705364..988ee7470 100644 --- a/test/TestApp.AspNetCore.6.0/Startup.cs +++ b/test/TestApp.AspNetCore.6.0/Startup.cs @@ -39,6 +39,8 @@ namespace TestApp.AspNetCore._6._0 services.AddSingleton(); services.AddSingleton( new CallbackMiddleware.CallbackMiddlewareImpl()); + services.AddSingleton( + new ActivityMiddleware.ActivityMiddlewareImpl()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -50,6 +52,7 @@ namespace TestApp.AspNetCore._6._0 } app.UseMiddleware(); + app.UseMiddleware(); app.UseRouting(); app.UseAuthorization();