Skip to content

Commit

Permalink
Asp.Net Core Unit test [Activity started in middleware is modified by…
Browse files Browse the repository at this point in the history
… instrumentation] (#3490)
  • Loading branch information
vishweshbankwar authored Jul 27, 2022
1 parent f9ed304 commit 6789efa
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,39 @@ void ConfigureTestServices(IServiceCollection services)
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();
Expand Down Expand Up @@ -661,5 +694,28 @@ public override void OnStopActivity(Activity activity, object payload)
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();
}
}
}
}
61 changes: 61 additions & 0 deletions test/TestApp.AspNetCore.3.1/ActivityMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
3 changes: 3 additions & 0 deletions test/TestApp.AspNetCore.3.1/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void ConfigureServices(IServiceCollection services)
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.
Expand All @@ -50,6 +52,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}

app.UseMiddleware<CallbackMiddleware>();
app.UseMiddleware<ActivityMiddleware>();
app.UseRouting();

app.UseAuthorization();
Expand Down
61 changes: 61 additions & 0 deletions test/TestApp.AspNetCore.6.0/ActivityMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
3 changes: 3 additions & 0 deletions test/TestApp.AspNetCore.6.0/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void ConfigureServices(IServiceCollection services)
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.
Expand All @@ -50,6 +52,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}

app.UseMiddleware<CallbackMiddleware>();
app.UseMiddleware<ActivityMiddleware>();
app.UseRouting();

app.UseAuthorization();
Expand Down

0 comments on commit 6789efa

Please sign in to comment.