Asp.Net Core Unit test [Activity started in middleware is modified by instrumentation] (#3490)

This commit is contained in:
Vishwesh Bankwar 2022-07-27 15:27:44 -07:00 committed by GitHub
parent f9ed3047b6
commit 6789efab53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 184 additions and 0 deletions

View File

@ -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<Activity>();
var activitySourceName = "TestMiddlewareActivitySource";
var activityName = "TestMiddlewareActivity";
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices((IServiceCollection services) =>
{
services.AddSingleton<ActivityMiddleware.ActivityMiddlewareImpl>(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();
}
}
}
}

View File

@ -0,0 +1,61 @@
// <copyright file="ActivityMiddleware.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
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
}
}
}
}

View File

@ -39,6 +39,8 @@ namespace TestApp.AspNetCore._3._1
services.AddSingleton<HttpClient>();
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<CallbackMiddleware>();
app.UseMiddleware<ActivityMiddleware>();
app.UseRouting();
app.UseAuthorization();

View File

@ -0,0 +1,61 @@
// <copyright file="ActivityMiddleware.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
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
}
}
}
}

View File

@ -39,6 +39,8 @@ namespace TestApp.AspNetCore._6._0
services.AddSingleton<HttpClient>();
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<CallbackMiddleware>();
app.UseMiddleware<ActivityMiddleware>();
app.UseRouting();
app.UseAuthorization();