Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use test host in unit tests #5040

Merged
merged 41 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
df119ed
Added workflow to build examples separately
ngruson Nov 8, 2023
41e4055
cd examples
ngruson Nov 8, 2023
56099ba
Update examples.yml
ngruson Nov 8, 2023
5386959
Update examples.yml
ngruson Nov 8, 2023
77e9684
Update examples.yml
ngruson Nov 8, 2023
66d97cb
Added Examples solution
ngruson Nov 8, 2023
3e6af87
Merge branch 'main' of https://github.com/ngruson/opentelemetry-dotnet
ngruson Nov 8, 2023
1ecc29c
Merge branch 'open-telemetry:main' into main
ngruson Nov 9, 2023
e153d88
Build examples separately
ngruson Nov 9, 2023
dd5c16a
Fix merge conflicts
ngruson Nov 9, 2023
6561139
Merge pull request #1 from ngruson/3014-make-ci-checks-faster
ngruson Nov 9, 2023
ec2bb92
Update examples.yml
ngruson Nov 9, 2023
d508cac
Update examples.yml
ngruson Nov 9, 2023
02bcf0b
Merge branch 'open-telemetry:main' into main
ngruson Nov 10, 2023
56aa1db
UseHostBuilder to create client/server
ngruson Nov 10, 2023
1b69355
Refactored RouteInformationIsNotAddedToRequestsOutsideOfMVC
ngruson Nov 10, 2023
4f161df
Merge branch 'main' into 4976-improve-net8-metrics-tests
utpilla Nov 11, 2023
a60a8f5
Refactord BasicTests
ngruson Nov 11, 2023
54340de
Merge branch 'open-telemetry:main' into main
ngruson Nov 11, 2023
79660f6
Merge branch '4976-improve-net8-metrics-tests' of https://github.com/…
ngruson Nov 11, 2023
895da60
Run tests on variable port instead of 5000
ngruson Nov 12, 2023
12aeef1
Merge branch 'main' of https://github.com/ngruson/opentelemetry-dotne…
ngruson Nov 12, 2023
cdecae3
Removed unnecessary using
ngruson Nov 12, 2023
3e7c3bf
Added back blank line before EOF
ngruson Nov 12, 2023
6024f19
Processed review comments
ngruson Nov 17, 2023
dd15e63
Merge branch '4976-improve-net8-metrics-tests' of https://github.com/…
ngruson Nov 17, 2023
fabc1c6
Resolved merge conflicts
ngruson Nov 18, 2023
cbb5320
Solved merge conflicts
ngruson Nov 18, 2023
cf9bc01
Synced fork with main and merged conflicts
ngruson Nov 18, 2023
44639b4
Removed unnecessary usings
ngruson Nov 18, 2023
8952aca
Fixed ValidateNet8RateLimitingMetricsAsync
ngruson Nov 18, 2023
f8c43fb
Removed files that were not supposed to come along in this PR
ngruson Nov 18, 2023
7add987
Merge branch 'main' into 4976-improve-net8-metrics-tests
vishweshbankwar Nov 20, 2023
6227ab0
Fixed DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHand…
ngruson Nov 21, 2023
bac5a61
Merge branch '4976-improve-net8-metrics-tests' of https://github.com/…
ngruson Nov 21, 2023
6524198
Removed skip of unit test DiagnosticSourceExceptionCallBackIsNotRecei…
ngruson Nov 22, 2023
10c93cc
Reverted sln changes
ngruson Nov 22, 2023
ebe17a2
Resolved merge conflict in MetricTests.cs
ngruson Nov 22, 2023
2ff66d5
Conditional using directive
ngruson Nov 22, 2023
bbe10ec
Missing blank line before EOF
ngruson Nov 22, 2023
292da0f
Processed review comments
ngruson Nov 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 103 additions & 147 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Moq;
using OpenTelemetry.Context.Propagation;
Expand Down Expand Up @@ -840,45 +841,42 @@ public async Task DiagnosticSourceCallbacksAreReceivedOnlyForSubscribedEvents()
{
int numberOfUnSubscribedEvents = 0;
int numberofSubscribedEvents = 0;
void ConfigureTestServices(IServiceCollection services)
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation(
new TestHttpInListener(new AspNetCoreInstrumentationOptions())

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation(
new TestHttpInListener(new AspNetCoreInstrumentationOptions())
{
OnEventWrittenCallback = (name, payload) =>
{
OnEventWrittenCallback = (name, payload) =>
switch (name)
{
switch (name)
{
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
}
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
}

break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
}
break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
}

break;
default:
{
numberOfUnSubscribedEvents++;
}
break;
default:
{
numberOfUnSubscribedEvents++;
}

break;
}
},
})
.Build();
}
break;
}
},
})
.Build();

// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
Expand All @@ -899,56 +897,53 @@ public async Task DiagnosticSourceExceptionCallbackIsReceivedForUnHandledExcepti
int numberOfUnSubscribedEvents = 0;
int numberofSubscribedEvents = 0;
int numberOfExceptionCallbacks = 0;
void ConfigureTestServices(IServiceCollection services)
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation(
new TestHttpInListener(new AspNetCoreInstrumentationOptions())

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation(
new TestHttpInListener(new AspNetCoreInstrumentationOptions())
{
OnEventWrittenCallback = (name, payload) =>
{
OnEventWrittenCallback = (name, payload) =>
switch (name)
{
switch (name)
{
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
}
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
}

break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
}
break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
}

break;
break;

// TODO: Add test case for validating name for both the types
// of exception event.
case HttpInListener.OnUnhandledHostingExceptionEvent:
case HttpInListener.OnUnHandledDiagnosticsExceptionEvent:
{
numberofSubscribedEvents++;
numberOfExceptionCallbacks++;
}
// TODO: Add test case for validating name for both the types
// of exception event.
case HttpInListener.OnUnhandledHostingExceptionEvent:
case HttpInListener.OnUnHandledDiagnosticsExceptionEvent:
{
numberofSubscribedEvents++;
numberOfExceptionCallbacks++;
}

break;
default:
{
numberOfUnSubscribedEvents++;
}
break;
default:
{
numberOfUnSubscribedEvents++;
}

break;
}
},
})
.Build();
}
break;
}
},
})
.Build();

// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
Expand All @@ -971,15 +966,15 @@ void ConfigureTestServices(IServiceCollection services)
Assert.Equal(3, numberofSubscribedEvents);
}

[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/4884")]
[Fact]
public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHandledInMiddleware()
{
int numberOfUnSubscribedEvents = 0;
int numberofSubscribedEvents = 0;
int numberOfSubscribedEvents = 0;
int numberOfExceptionCallbacks = 0;

// configure SDK
using var tracerprovider = Sdk.CreateTracerProviderBuilder()
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation(
new TestHttpInListener(new AspNetCoreInstrumentationOptions())
{
Expand All @@ -989,13 +984,13 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
{
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
numberOfSubscribedEvents++;
}

break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
numberOfSubscribedEvents++;
}

break;
Expand All @@ -1005,7 +1000,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
case HttpInListener.OnUnhandledHostingExceptionEvent:
case HttpInListener.OnUnHandledDiagnosticsExceptionEvent:
{
numberofSubscribedEvents++;
numberOfSubscribedEvents++;
numberOfExceptionCallbacks++;
}

Expand All @@ -1021,45 +1016,35 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
})
.Build();

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

app.UseExceptionHandler(handler =>
{
handler.Run(async (ctx) =>
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
await ctx.Response.WriteAsync("handled");
});
});

app.Map("/error", ThrowException);

static void ThrowException(IApplicationBuilder app)
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
builder.Configure(app => app
.UseExceptionHandler(handler =>
{
handler.Run(async (ctx) =>
{
await ctx.Response.WriteAsync("handled");
});
}));
})
.CreateClient())
{
app.Run(context =>
try
{
throw new Exception("CustomException");
});
}

_ = app.RunAsync();

using var client = new HttpClient();
try
{
await client.GetStringAsync("http://localhost:5000/error");
}
catch
{
// ignore 500 error.
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/error");
using var response = await client.SendAsync(request);
}
catch
{
// ignore exception
}
}

Assert.Equal(0, numberOfExceptionCallbacks);
Assert.Equal(0, numberOfUnSubscribedEvents);
Assert.Equal(2, numberofSubscribedEvents);

await app.DisposeAsync();
Assert.Equal(2, numberOfSubscribedEvents);
}

public void Dispose()
Expand Down Expand Up @@ -1128,16 +1113,10 @@ private void ConfigureExceptionFilters(IServiceCollection services, int mode, re
.Build();
}

private class ExtractOnlyPropagator : TextMapPropagator
private class ExtractOnlyPropagator(ActivityContext activityContext, Baggage baggage) : TextMapPropagator
{
private readonly ActivityContext activityContext;
private readonly Baggage baggage;

public ExtractOnlyPropagator(ActivityContext activityContext, Baggage baggage)
{
this.activityContext = activityContext;
this.baggage = baggage;
}
private readonly ActivityContext activityContext = activityContext;
private readonly Baggage baggage = baggage;

public override ISet<string> Fields => throw new NotImplementedException();

Expand All @@ -1152,32 +1131,21 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
}
}

private class TestSampler : Sampler
private class TestSampler(SamplingDecision samplingDecision, IEnumerable<KeyValuePair<string, object>> attributes = null) : Sampler
{
private readonly SamplingDecision samplingDecision;
private readonly IEnumerable<KeyValuePair<string, object>> attributes;

public TestSampler(SamplingDecision samplingDecision, IEnumerable<KeyValuePair<string, object>> attributes = null)
{
this.samplingDecision = samplingDecision;
this.attributes = attributes;
}
private readonly SamplingDecision samplingDecision = samplingDecision;
private readonly IEnumerable<KeyValuePair<string, object>> attributes = attributes;

public override SamplingResult ShouldSample(in SamplingParameters samplingParameters)
{
return new SamplingResult(this.samplingDecision, this.attributes);
}
}

private class TestHttpInListener : HttpInListener
private class TestHttpInListener(AspNetCoreInstrumentationOptions options) : HttpInListener(options)
{
public Action<string, object> OnEventWrittenCallback;

public TestHttpInListener(AspNetCoreInstrumentationOptions options)
: base(options)
{
}

public override void OnEventWritten(string name, object payload)
{
base.OnEventWritten(name, payload);
Expand All @@ -1186,17 +1154,11 @@ public override void OnEventWritten(string name, object payload)
}
}

private class TestNullHostActivityMiddlewareImpl : ActivityMiddleware.ActivityMiddlewareImpl
private class TestNullHostActivityMiddlewareImpl(string activitySourceName, string activityName) : ActivityMiddleware.ActivityMiddlewareImpl
{
private ActivitySource activitySource;
private readonly ActivitySource activitySource = new(activitySourceName);
private readonly string activityName = activityName;
private Activity activity;
private string activityName;

public TestNullHostActivityMiddlewareImpl(string activitySourceName, string activityName)
{
this.activitySource = new ActivitySource(activitySourceName);
this.activityName = activityName;
}

public override void PreProcess(HttpContext context)
{
Expand All @@ -1214,17 +1176,11 @@ public override void PostProcess(HttpContext context)
}
}

private class TestActivityMiddlewareImpl : ActivityMiddleware.ActivityMiddlewareImpl
private class TestActivityMiddlewareImpl(string activitySourceName, string activityName) : ActivityMiddleware.ActivityMiddlewareImpl
{
private ActivitySource activitySource;
private readonly ActivitySource activitySource = new(activitySourceName);
private readonly string activityName = activityName;
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)
{
Expand Down
Loading