-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenTelemetry integration throws an exception when Sentry is disabled (…
- Loading branch information
1 parent
8ad81aa
commit 818e404
Showing
5 changed files
with
148 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using OpenTelemetry; | ||
|
||
namespace Sentry.OpenTelemetry; | ||
|
||
internal class DisabledSpanProcessor : BaseProcessor<Activity> | ||
{ | ||
private static readonly Lazy<DisabledSpanProcessor> LazyInstance = new(); | ||
internal static DisabledSpanProcessor Instance => LazyInstance.Value; | ||
|
||
public override void OnStart(Activity activity) | ||
{ | ||
// No-Op | ||
} | ||
|
||
public override void OnEnd(Activity activity) | ||
{ | ||
// No-Op | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
test/Sentry.OpenTelemetry.Tests/TracerProviderBuilderExtensionsTests.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,94 @@ | ||
namespace Sentry.OpenTelemetry.Tests; | ||
|
||
public class TracerProviderBuilderExtensionsTests | ||
{ | ||
private class Fixture | ||
{ | ||
public IServiceProvider ServiceProvider { get; } = Substitute.For<IServiceProvider>(); | ||
public IHub Hub { get; } = Substitute.For<IHub>(); | ||
|
||
public Fixture() | ||
{ | ||
ServiceProvider.GetService(typeof(IHub)).Returns(Hub); | ||
} | ||
|
||
public SentryOptions GetOptions(string dsn = "https://[email protected]/789") => new() | ||
{ | ||
Instrumenter = Instrumenter.OpenTelemetry, | ||
Dsn = dsn | ||
}; | ||
|
||
public IServiceProvider GetServiceProvider() => ServiceProvider; | ||
} | ||
|
||
|
||
[Fact] | ||
public void ImplementationFactory_WithUserFactory_AddsAspNetCoreEnricher() | ||
{ | ||
// Arrange | ||
var fixture = new Fixture(); | ||
SentryClientExtensions.SentryOptionsForTestingOnly = fixture.GetOptions(); | ||
fixture.Hub.IsEnabled.Returns(true); | ||
var userFactory = Substitute.For<ISentryUserFactory>(); | ||
fixture.ServiceProvider.GetService(typeof(ISentryUserFactory)).Returns(userFactory); | ||
var services = fixture.GetServiceProvider(); | ||
|
||
// Act | ||
var result = TracerProviderBuilderExtensions.ImplementationFactory(services); | ||
|
||
// Assert | ||
result.Should().BeOfType<SentrySpanProcessor>(); // FluentAssertions | ||
var spanProcessor = (SentrySpanProcessor)result; | ||
spanProcessor._enrichers.Should().NotBeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void ImplementationFactory_WithoutUserFactory_DoesNotAddAspNetCoreEnricher() | ||
{ | ||
// Arrange | ||
var fixture = new Fixture(); | ||
SentryClientExtensions.SentryOptionsForTestingOnly = fixture.GetOptions(); | ||
fixture.Hub.IsEnabled.Returns(true); | ||
var services = fixture.GetServiceProvider(); | ||
|
||
// Act | ||
var result = TracerProviderBuilderExtensions.ImplementationFactory(services); | ||
|
||
// Assert | ||
result.Should().BeOfType<SentrySpanProcessor>(); // FluentAssertions | ||
var spanProcessor = (SentrySpanProcessor)result; | ||
spanProcessor._enrichers.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void ImplementationFactory_WithEnabledHub_ReturnsSentrySpanProcessor() | ||
{ | ||
// Arrange | ||
var fixture = new Fixture(); | ||
SentryClientExtensions.SentryOptionsForTestingOnly = fixture.GetOptions(); | ||
fixture.Hub.IsEnabled.Returns(true); | ||
var services = fixture.GetServiceProvider(); | ||
|
||
// Act | ||
var result = TracerProviderBuilderExtensions.ImplementationFactory(services); | ||
|
||
// Assert | ||
result.Should().BeOfType<SentrySpanProcessor>(); // FluentAssertions | ||
} | ||
|
||
[Fact] | ||
public void ImplementationFactory_WithDisabledHub_ReturnsDisabledSpanProcessor() | ||
{ | ||
// Arrange | ||
var fixture = new Fixture(); | ||
SentryClientExtensions.SentryOptionsForTestingOnly = fixture.GetOptions(); | ||
fixture.Hub.IsEnabled.Returns(false); | ||
var services = fixture.GetServiceProvider(); | ||
|
||
// Act | ||
var result = TracerProviderBuilderExtensions.ImplementationFactory(services); | ||
|
||
// Assert | ||
result.Should().BeOfType<DisabledSpanProcessor>(); // FluentAssertions | ||
} | ||
} |