Skip to content

Commit 32a3518

Browse files
authored
Remove OptionalHub (replace with a static method) (#534)
1 parent 7166cc2 commit 32a3518

File tree

5 files changed

+9
-54
lines changed

5 files changed

+9
-54
lines changed

src/Sentry.Extensions.Logging/Extensions/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public static IServiceCollection AddSentry<TOptions>(this IServiceCollection ser
2525
services.TryAddSingleton<SentryOptions>(
2626
c => c.GetRequiredService<IOptions<TOptions>>().Value);
2727

28-
services.TryAddSingleton<OptionalHub>();
29-
3028
services.TryAddTransient<ISentryClient>(c => c.GetRequiredService<IHub>());
3129
services.TryAddTransient(c => c.GetRequiredService<Func<IHub>>()());
3230

@@ -36,7 +34,7 @@ public static IServiceCollection AddSentry<TOptions>(this IServiceCollection ser
3634

3735
if (options.InitializeSdk)
3836
{
39-
var hub = c.GetRequiredService<OptionalHub>();
37+
var hub = OptionalHub.FromOptions(options);
4038
_ = SentrySdk.UseHub(hub);
4139
}
4240

src/Sentry.Extensions.Logging/SentryLoggerFactoryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static ILoggerFactory AddSentry(
5151
else
5252
{
5353
options.DiagnosticLogger?.LogDebug("Initializing from {0} and swapping current Hub.", nameof(SentryLoggerFactoryExtensions));
54-
hub = new OptionalHub(options);
54+
hub = OptionalHub.FromOptions(options);
5555
_ = SentrySdk.UseHub(hub);
5656
}
5757
}

src/Sentry/Internal/OptionalHub.cs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,17 @@
1-
using System;
2-
using System.Threading.Tasks;
31
using Sentry.Extensibility;
4-
using Sentry.Protocol;
52

63
namespace Sentry.Internal
74
{
85
// Depending on Options:
96
// - A proxy to a new Hub instance
107
// or
118
// - A proxy to SentrySdk which could hold a Hub if SentrySdk.Init was called, or a disabled Hub
12-
internal class OptionalHub : IHub, IDisposable
9+
internal static class OptionalHub
1310
{
14-
private readonly IHub _hub;
15-
16-
public bool IsEnabled => _hub.IsEnabled;
17-
18-
public OptionalHub(SentryOptions options)
11+
public static IHub FromOptions(SentryOptions options)
1912
{
2013
options.SetupLogging();
21-
_hub = GetHub(options);
22-
}
23-
24-
public SentryId CaptureEvent(SentryEvent evt, Scope? scope = null) => _hub.CaptureEvent(evt, scope);
25-
26-
public Task FlushAsync(TimeSpan timeout) => _hub.FlushAsync(timeout);
27-
28-
public void ConfigureScope(Action<Scope> configureScope) => _hub.ConfigureScope(configureScope);
29-
30-
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope) => _hub.ConfigureScopeAsync(configureScope);
3114

32-
public void BindClient(ISentryClient client) => _hub.BindClient(client);
33-
34-
public IDisposable PushScope() => _hub.PushScope();
35-
36-
public IDisposable PushScope<TState>(TState state)
37-
=> _hub.PushScope(state);
38-
39-
public void WithScope(Action<Scope> scopeCallback) => _hub.WithScope(scopeCallback);
40-
41-
public SentryId LastEventId => _hub.LastEventId;
42-
43-
public void Dispose() => (_hub as IDisposable)?.Dispose();
44-
45-
private static IHub GetHub(SentryOptions options)
46-
{
4715
var dsn = options.Dsn ?? DsnLocator.FindDsnStringOrDisable();
4816

4917
if (Dsn.IsDisabled(dsn))

test/Sentry.Extensions.Logging.Tests/SentryLoggerFactoryExtensionsTests.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@ public void AddSentry_InitializeSdkFalse_HubAdapter()
6363
.AddProvider(Arg.Is<SentryLoggerProvider>(p => p.Hub == HubAdapter.Instance));
6464
}
6565

66-
[Fact]
67-
public void AddSentry_DefaultOptions_InstantiateOptionalHub()
68-
{
69-
var sut = Substitute.For<ILoggerFactory>();
70-
71-
_ = sut.AddSentry();
72-
73-
sut.Received(1)
74-
.AddProvider(Arg.Is<SentryLoggerProvider>(p => p.Hub is OptionalHub));
75-
}
76-
7766
[Fact]
7867
public void AddSentry_NoDiagnosticSet_MelSet()
7968
{

test/Sentry.Tests/Internals/OptionalHubTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ public class OptionalHubTests
99
{
1010
// Issue: https://github.com/getsentry/sentry-dotnet/issues/123
1111
[Fact]
12-
public void Ctor_NoDsn_DisposeDoesNotThrow()
12+
public void FromOptions_NoDsn_DisposeDoesNotThrow()
1313
{
14-
var sut = new OptionalHub(new SentryOptions());
15-
sut.Dispose();
14+
var sut = OptionalHub.FromOptions(new SentryOptions()) as IDisposable;
15+
sut?.Dispose();
1616
}
1717

1818
[Fact]
19-
public Task Ctor_NoDsn_FlushAsyncDoesNotThrow()
19+
public Task FromOptions_NoDsn_FlushAsyncDoesNotThrow()
2020
{
21-
var sut = new OptionalHub(new SentryOptions());
21+
var sut = OptionalHub.FromOptions(new SentryOptions());
2222
return sut.FlushAsync(TimeSpan.FromDays(1));
2323
}
2424
}

0 commit comments

Comments
 (0)