Skip to content

Commit

Permalink
[ASP.NET Core] Clear logging for testing scenarios (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishweshbankwar authored Nov 11, 2022
1 parent 2fce583 commit b5e50b2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 33 deletions.
112 changes: 81 additions & 31 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
Expand Down Expand Up @@ -57,8 +59,10 @@ public void AddAspNetCoreInstrumentation_BadArgs()
Assert.Throws<ArgumentNullException>(() => builder.AddAspNetCoreInstrumentation());
}

[Fact]
public async Task StatusIsUnsetOn200Response()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task StatusIsUnsetOn200Response(bool disableLogging)
{
var exportedItems = new List<Activity>();
void ConfigureTestServices(IServiceCollection services)
Expand All @@ -72,7 +76,13 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
if (disableLogging)
{
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -116,7 +126,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -150,12 +163,17 @@ public async Task SuccessfulTemplateControllerCallUsesParentContext()
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder().AddAspNetCoreInstrumentation()
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
})))
});

builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "/api/values/2");
Expand Down Expand Up @@ -203,14 +221,14 @@ public async Task CustomPropagator()
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
Sdk.SetDefaultTextMapPropagator(propagator.Object);
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
})))
builder.ConfigureTestServices(services =>
{
Sdk.SetDefaultTextMapPropagator(propagator.Object);
this.tracerProvider = Sdk.CreateTracerProviderBuilder().AddAspNetCoreInstrumentation().AddInMemoryExporter(exportedItems).Build();
});
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();
var response = await client.GetAsync("/api/values/2");
Expand Down Expand Up @@ -256,7 +274,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices)))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();

Expand Down Expand Up @@ -303,7 +324,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices)))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();

Expand Down Expand Up @@ -347,13 +371,10 @@ public async Task ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision
// Arrange
using var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new TestSampler(samplingDecision))
.AddAspNetCoreInstrumentation()
.Build();
}));
builder.ConfigureTestServices(services => { this.tracerProvider = Sdk.CreateTracerProviderBuilder().SetSampler(new TestSampler(samplingDecision)).AddAspNetCoreInstrumentation().Build(); });
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
});
using var client = testFactory.CreateClient();

// Test TraceContext Propagation
Expand Down Expand Up @@ -404,6 +425,7 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied()
bool isFilterCalled = false;
using var testFactory = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
Expand All @@ -416,7 +438,9 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied()
};
})
.Build();
}));
});
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
});
using var client = testFactory.CreateClient();

// Test TraceContext Propagation
Expand Down Expand Up @@ -494,7 +518,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/values");
Expand Down Expand Up @@ -549,7 +576,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient();

// Act
Expand Down Expand Up @@ -582,7 +612,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
var response = await client.GetAsync("/api/values/2");
Expand Down Expand Up @@ -618,13 +651,16 @@ public async Task ActivitiesStartedInMiddlewareBySettingHostActivityToNullShould
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices((IServiceCollection services) =>
{
services.AddSingleton<ActivityMiddleware.ActivityMiddlewareImpl>(new TestNullHostActivityMiddlewareImpl(activitySourceName, activityName));
services.AddOpenTelemetryTracing((builder) => builder.AddAspNetCoreInstrumentation()
.AddSource(activitySourceName)
.AddInMemoryExporter(exportedItems));
}))
});
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
var response = await client.GetAsync("/api/values/2");
Expand Down Expand Up @@ -671,7 +707,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -699,8 +738,12 @@ public async Task ShouldExportActivityWithOneOrMoreExceptionFilters(int mode)

// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder => builder.ConfigureTestServices(
(s) => this.ConfigureExceptionFilters(s, mode, ref exportedItems)))
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(
(s) => this.ConfigureExceptionFilters(s, mode, ref exportedItems));
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -745,7 +788,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/values");
Expand Down Expand Up @@ -790,7 +836,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
try
Expand Down Expand Up @@ -836,6 +885,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
.Build();

var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
var app = builder.Build();

app.UseExceptionHandler(handler =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Trace;
using Xunit;

Expand All @@ -37,6 +38,7 @@ public InProcServerTests()
{
this.exportedItems = new List<Activity>();
var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
var app = builder.Build();

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Trace;
using TestApp.AspNetCore;
using Xunit;
Expand Down Expand Up @@ -58,12 +60,15 @@ public async Task SuccessfulTemplateControllerCallGeneratesASpan(
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices((IServiceCollection services) =>
{
services.AddSingleton<CallbackMiddleware.CallbackMiddlewareImpl>(new TestCallbackMiddlewareImpl(statusCode, reasonPhrase));
services.AddOpenTelemetryTracing((builder) => builder.AddAspNetCoreInstrumentation(options => options.RecordException = recordException)
.AddInMemoryExporter(exportedItems));
}))
});
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using Xunit;
Expand Down Expand Up @@ -53,7 +55,12 @@ public async Task RequestMetricIsCaptured()
.AddInMemoryExporter(metricItems)
.Build();

using (var client = this.factory.CreateClient())
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
var response = await client.GetAsync("/api/values");
response.EnsureSuccessStatusCode();
Expand Down

0 comments on commit b5e50b2

Please sign in to comment.