-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SentryOptions.AutoRegisterTracing (#2871)
- Loading branch information
1 parent
bbd42fb
commit 65a7bba
Showing
10 changed files
with
174 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
test/Sentry.AspNetCore.Tests/SentryTracingBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#if NETCOREAPP3_0_OR_GREATER | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Sentry.AspNetCore.TestUtils; | ||
|
||
namespace Sentry.AspNetCore.Tests; | ||
|
||
public class SentryTracingBuilderTests | ||
{ | ||
class Fixture | ||
{ | ||
public Action<IServiceCollection> ConfigureServices { get; set; } | ||
public Action<IApplicationBuilder> Configure { get; set; } | ||
public Action<SentryAspNetCoreOptions> ConfigureOptions { get; set; } = _ => { }; | ||
|
||
public (IServiceCollection services, IApplicationBuilder builder) GetSut() | ||
{ | ||
IServiceCollection servicesCollection = null; | ||
IApplicationBuilder applicationBuilder = null; | ||
_ = new TestServer(new WebHostBuilder() | ||
.UseDefaultServiceProvider(di => di.EnableValidation()) | ||
.UseSentry(ConfigureOptions) | ||
.ConfigureServices(services => | ||
{ | ||
ConfigureServices?.Invoke(services); | ||
servicesCollection = services; | ||
}) | ||
.Configure(app => | ||
{ | ||
Configure?.Invoke(app); | ||
applicationBuilder = app; | ||
})); | ||
return (servicesCollection, applicationBuilder); | ||
} | ||
} | ||
|
||
private readonly Fixture _fixture = new(); | ||
|
||
[Fact] | ||
public void UseRouting_AutoRegisterTracingDisabled_SentryTracingNotRegistered() | ||
{ | ||
// Arrange | ||
_fixture.ConfigureServices = services => services.AddRouting(); | ||
_fixture.Configure = applicationBuilder => applicationBuilder.UseRouting(); | ||
_fixture.ConfigureOptions = options => options.AutoRegisterTracing = false; | ||
|
||
// Act - implicit | ||
var (_, builder) = _fixture.GetSut(); | ||
|
||
// Assert | ||
builder.IsSentryTracingRegistered().Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void UseRouting_OtelInstrumentation_SentryTracingNotRegistered() | ||
{ | ||
// Arrange | ||
_fixture.ConfigureServices = services => services.AddRouting(); | ||
_fixture.Configure = applicationBuilder => applicationBuilder.UseRouting(); | ||
_fixture.ConfigureOptions = options => options.Instrumenter = Instrumenter.OpenTelemetry; | ||
|
||
// Act - implicit | ||
var (_, builder) = _fixture.GetSut(); | ||
|
||
// Assert | ||
builder.IsSentryTracingRegistered().Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void UseRouting_SentryTracingRegisteredWithoutWarning() | ||
{ | ||
// Arrange | ||
var logger = Substitute.For<ILogger<SentryTracingMiddleware>>(); | ||
var loggerFactory = Substitute.For<ILoggerFactory>(); | ||
loggerFactory.CreateLogger<SentryTracingMiddleware>().Returns(logger); | ||
_fixture.ConfigureServices = services => | ||
{ | ||
services.AddSingleton(loggerFactory); | ||
services.AddRouting(); | ||
}; | ||
_fixture.Configure = applicationBuilder => applicationBuilder.UseRouting(); | ||
|
||
// Act | ||
var (_, builder) = _fixture.GetSut(); | ||
|
||
builder.IsSentryTracingRegistered().Should().BeTrue(); | ||
logger.Received(0).LogWarning(SentryTracingMiddlewareExtensions.AlreadyRegisteredWarning); | ||
} | ||
|
||
[Fact] | ||
public void UseSentryTracing_AutoRegisterTracing_Warning() | ||
{ | ||
// Arrange | ||
var logger = Substitute.For<ILogger<SentryTracingMiddleware>>(); | ||
var loggerFactory = Substitute.For<ILoggerFactory>(); | ||
loggerFactory.CreateLogger<SentryTracingMiddleware>().Returns(logger); | ||
_fixture.ConfigureServices = services => | ||
{ | ||
services.AddSingleton(loggerFactory); | ||
services.AddRouting(); | ||
}; | ||
_fixture.Configure = applicationBuilder => | ||
{ | ||
applicationBuilder.UseRouting(); | ||
applicationBuilder.UseSentryTracing(); | ||
}; | ||
|
||
// Act | ||
var _ = _fixture.GetSut(); | ||
|
||
// Assert | ||
logger.Received(1).LogWarning(SentryTracingMiddlewareExtensions.AlreadyRegisteredWarning); | ||
} | ||
} | ||
#endif |