From 19aa0a4c1873419a044be543cf3027ccd9693202 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Sat, 17 Aug 2019 20:04:27 +0200 Subject: [PATCH] ref: Don't use ValueTuple --- src/Sentry.AspNetCore/SentryMiddleware.cs | 3 ++- .../SentryLoggerProvider.cs | 3 ++- src/Sentry.Log4Net/SentryAppender.cs | 2 +- src/Sentry.NLog/SentryTarget.cs | 2 +- src/Sentry.Serilog/SentrySink.cs | 2 +- src/Sentry/Internal/IInternalScopeManager.cs | 4 ++- .../Internal/MainSentryEventProcessor.cs | 2 +- src/Sentry/Internal/SentryScopeManager.cs | 27 ++++++++++--------- src/Sentry/PublicAPI.Shipped.txt | 1 - src/Sentry/PublicAPI.Unshipped.txt | 2 +- src/Sentry/Reflection/AssemblyExtensions.cs | 7 ++--- test/Sentry.Tests/HubTests.cs | 4 +-- .../Internals/SentryScopeManagerTests.cs | 10 +++---- 13 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/Sentry.AspNetCore/SentryMiddleware.cs b/src/Sentry.AspNetCore/SentryMiddleware.cs index adf536a3d1..cb2ef088eb 100644 --- a/src/Sentry.AspNetCore/SentryMiddleware.cs +++ b/src/Sentry.AspNetCore/SentryMiddleware.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Sentry.Extensibility; +using Sentry.Protocol; using Sentry.Reflection; namespace Sentry.AspNetCore @@ -23,7 +24,7 @@ internal class SentryMiddleware private readonly IHostingEnvironment _hostingEnvironment; private readonly ILogger _logger; - internal static readonly (string Name, string Version) NameAndVersion + internal static readonly SdkVersion NameAndVersion = typeof(SentryMiddleware).Assembly.GetNameAndVersion(); private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name; diff --git a/src/Sentry.Extensions.Logging/SentryLoggerProvider.cs b/src/Sentry.Extensions.Logging/SentryLoggerProvider.cs index 467f07ad6b..89d856dd86 100644 --- a/src/Sentry.Extensions.Logging/SentryLoggerProvider.cs +++ b/src/Sentry.Extensions.Logging/SentryLoggerProvider.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Sentry.Infrastructure; +using Sentry.Protocol; using Sentry.Reflection; namespace Sentry.Extensions.Logging @@ -20,7 +21,7 @@ public class SentryLoggerProvider : ILoggerProvider internal IHub Hub { get; } - internal static readonly (string Name, string Version) NameAndVersion + internal static readonly SdkVersion NameAndVersion = typeof(SentryLogger).Assembly.GetNameAndVersion(); private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name; diff --git a/src/Sentry.Log4Net/SentryAppender.cs b/src/Sentry.Log4Net/SentryAppender.cs index 9bcd3ff130..b06c532722 100644 --- a/src/Sentry.Log4Net/SentryAppender.cs +++ b/src/Sentry.Log4Net/SentryAppender.cs @@ -19,7 +19,7 @@ public class SentryAppender : AppenderSkeleton private readonly object _initSync = new object(); - internal static readonly (string Name, string Version) NameAndVersion + internal static readonly SdkVersion NameAndVersion = typeof(SentryAppender).Assembly.GetNameAndVersion(); private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name; diff --git a/src/Sentry.NLog/SentryTarget.cs b/src/Sentry.NLog/SentryTarget.cs index c7c45f613a..2f84b90408 100644 --- a/src/Sentry.NLog/SentryTarget.cs +++ b/src/Sentry.NLog/SentryTarget.cs @@ -30,7 +30,7 @@ public sealed class SentryTarget : TargetWithContext private readonly ISystemClock _clock; private IDisposable _sdkDisposable; - internal static readonly (string Name, string Version) NameAndVersion = typeof(SentryTarget).Assembly.GetNameAndVersion(); + internal static readonly SdkVersion NameAndVersion = typeof(SentryTarget).Assembly.GetNameAndVersion(); private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name; diff --git a/src/Sentry.Serilog/SentrySink.cs b/src/Sentry.Serilog/SentrySink.cs index cce2884b6c..ac0f07f3f6 100644 --- a/src/Sentry.Serilog/SentrySink.cs +++ b/src/Sentry.Serilog/SentrySink.cs @@ -20,7 +20,7 @@ internal sealed class SentrySink : ILogEventSink, IDisposable private readonly IDisposable _sdkDisposable; private readonly SentrySerilogOptions _options; - internal static readonly (string Name, string Version) NameAndVersion + internal static readonly SdkVersion NameAndVersion = typeof(SentrySink).Assembly.GetNameAndVersion(); private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name; diff --git a/src/Sentry/Internal/IInternalScopeManager.cs b/src/Sentry/Internal/IInternalScopeManager.cs index 7f5bf45710..df0e177ebf 100644 --- a/src/Sentry/Internal/IInternalScopeManager.cs +++ b/src/Sentry/Internal/IInternalScopeManager.cs @@ -1,7 +1,9 @@ +using System; + namespace Sentry.Internal { internal interface IInternalScopeManager : ISentryScopeManager { - (Scope Scope, ISentryClient Client) GetCurrent(); + Tuple GetCurrent(); } } diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index d822b996f4..5434d154fa 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -29,7 +29,7 @@ internal class MainSentryEventProcessor : ISentryEventProcessor : null; }); - private static readonly (string Name, string Version) NameAndVersion + private static readonly SdkVersion NameAndVersion = typeof(ISentryClient).Assembly.GetNameAndVersion(); private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name; diff --git a/src/Sentry/Internal/SentryScopeManager.cs b/src/Sentry/Internal/SentryScopeManager.cs index 385ba8549f..be83a001d2 100644 --- a/src/Sentry/Internal/SentryScopeManager.cs +++ b/src/Sentry/Internal/SentryScopeManager.cs @@ -10,15 +10,15 @@ namespace Sentry.Internal internal class SentryScopeManager : IInternalScopeManager, IDisposable { private readonly SentryOptions _options; - private readonly AsyncLocal> _asyncLocalScope = new AsyncLocal>(); + private readonly AsyncLocal>> _asyncLocalScope = new AsyncLocal>>(); - internal ImmutableStack<(Scope scope, ISentryClient client)> ScopeAndClientStack + internal ImmutableStack> ScopeAndClientStack { get => _asyncLocalScope.Value ?? (_asyncLocalScope.Value = NewStack()); set => _asyncLocalScope.Value = value; } - private Func> NewStack { get; } + private Func>> NewStack { get; } public SentryScopeManager( SentryOptions options, @@ -26,23 +26,23 @@ public SentryScopeManager( { Debug.Assert(rootClient != null); _options = options; - NewStack = () => ImmutableStack.Create((new Scope(options), rootClient)); + NewStack = () => ImmutableStack.Create(new Tuple(new Scope(options), rootClient)); } - public (Scope Scope, ISentryClient Client) GetCurrent() => ScopeAndClientStack.Peek(); + public Tuple GetCurrent() => ScopeAndClientStack.Peek(); public void ConfigureScope(Action configureScope) { _options?.DiagnosticLogger?.LogDebug("Configuring the scope."); var scope = GetCurrent(); - configureScope?.Invoke(scope.Scope); + configureScope?.Invoke(scope.Item1); } public Task ConfigureScopeAsync(Func configureScope) { _options?.DiagnosticLogger?.LogDebug("Configuring the scope asynchronously."); var scope = GetCurrent(); - return configureScope?.Invoke(scope.Scope) ?? Task.CompletedTask; + return configureScope?.Invoke(scope.Item1) ?? Task.CompletedTask; } public IDisposable PushScope() => PushScope(null); @@ -67,7 +67,7 @@ public IDisposable PushScope(TState state) } var scopeSnapshot = new ScopeSnapshot(_options, currentScopeAndClientStack, this); _options?.DiagnosticLogger?.LogDebug("New scope pushed."); - ScopeAndClientStack = currentScopeAndClientStack.Push((clonedScope, client)); + ScopeAndClientStack = currentScopeAndClientStack.Push(new Tuple(clonedScope, client)); return scopeSnapshot; } @@ -77,7 +77,7 @@ public void WithScope(Action scopeCallback) using (PushScope()) { var scope = GetCurrent(); - scopeCallback?.Invoke(scope.Scope); + scopeCallback?.Invoke(scope.Item1); } } @@ -87,19 +87,20 @@ public void BindClient(ISentryClient client) var currentScopeAndClientStack = ScopeAndClientStack; currentScopeAndClientStack = currentScopeAndClientStack.Pop(out var top); - currentScopeAndClientStack = currentScopeAndClientStack.Push((top.scope, client ?? DisabledHub.Instance)); + currentScopeAndClientStack = currentScopeAndClientStack.Push( + new Tuple(top.Item1, client ?? DisabledHub.Instance)); ScopeAndClientStack = currentScopeAndClientStack; } private class ScopeSnapshot : IDisposable { private readonly SentryOptions _options; - private readonly ImmutableStack<(Scope scope, ISentryClient client)> _snapshot; + private readonly ImmutableStack> _snapshot; private readonly SentryScopeManager _scopeManager; public ScopeSnapshot( SentryOptions options, - ImmutableStack<(Scope, ISentryClient)> snapshot, + ImmutableStack> snapshot, SentryScopeManager scopeManager) { Debug.Assert(snapshot != null); @@ -116,7 +117,7 @@ public void Dispose() // Only reset the parent if this is still the current scope foreach (var (scope, _) in _scopeManager.ScopeAndClientStack) { - if (ReferenceEquals(scope, _snapshot.Peek().scope)) + if (ReferenceEquals(scope, _snapshot.Peek().Item1)) { _scopeManager.ScopeAndClientStack = _snapshot; break; diff --git a/src/Sentry/PublicAPI.Shipped.txt b/src/Sentry/PublicAPI.Shipped.txt index b5bb955132..3a58575037 100644 --- a/src/Sentry/PublicAPI.Shipped.txt +++ b/src/Sentry/PublicAPI.Shipped.txt @@ -178,7 +178,6 @@ static Sentry.HubExtensions.AddBreadcrumb(this Sentry.IHub hub, string message, static Sentry.HubExtensions.LockScope(this Sentry.IHub hub) -> void static Sentry.HubExtensions.PushAndLockScope(this Sentry.IHub hub) -> System.IDisposable static Sentry.HubExtensions.UnlockScope(this Sentry.IHub hub) -> void -static Sentry.Reflection.AssemblyExtensions.GetNameAndVersion(this System.Reflection.Assembly asm) -> (string Name, string Version) static Sentry.ScopeExtensions.AddEventProcessor(this Sentry.Scope scope, Sentry.Extensibility.ISentryEventProcessor processor) -> void static Sentry.ScopeExtensions.AddEventProcessor(this Sentry.Scope scope, System.Func processor) -> void static Sentry.ScopeExtensions.AddEventProcessors(this Sentry.Scope scope, System.Collections.Generic.IEnumerable processors) -> void diff --git a/src/Sentry/PublicAPI.Unshipped.txt b/src/Sentry/PublicAPI.Unshipped.txt index 5f282702bb..192b3d777e 100644 --- a/src/Sentry/PublicAPI.Unshipped.txt +++ b/src/Sentry/PublicAPI.Unshipped.txt @@ -1 +1 @@ - \ No newline at end of file +static Sentry.Reflection.AssemblyExtensions.GetNameAndVersion(this System.Reflection.Assembly asm) -> Sentry.Protocol.SdkVersion diff --git a/src/Sentry/Reflection/AssemblyExtensions.cs b/src/Sentry/Reflection/AssemblyExtensions.cs index 92cb9220f9..4a9836d0cb 100644 --- a/src/Sentry/Reflection/AssemblyExtensions.cs +++ b/src/Sentry/Reflection/AssemblyExtensions.cs @@ -1,5 +1,6 @@ using System.ComponentModel; using System.Reflection; +using Sentry.Protocol; namespace Sentry.Reflection { @@ -17,8 +18,8 @@ public static class AssemblyExtensions /// If not available, falls back to /// /// The assembly to get the name and version from - /// A tuple of name and version - public static (string Name, string Version) GetNameAndVersion(this Assembly asm) + /// The SdkVersion. + public static SdkVersion GetNameAndVersion(this Assembly asm) { var asmName = asm.GetName(); var name = asmName.Name; @@ -26,7 +27,7 @@ public static (string Name, string Version) GetNameAndVersion(this Assembly asm) ?.InformationalVersion ?? asmName.Version.ToString(); - return (name, version); + return new SdkVersion { Name = name, Version = version }; } } } diff --git a/test/Sentry.Tests/HubTests.cs b/test/Sentry.Tests/HubTests.cs index e31cfd4a7b..5635c34899 100644 --- a/test/Sentry.Tests/HubTests.cs +++ b/test/Sentry.Tests/HubTests.cs @@ -36,10 +36,10 @@ public void PushScope_BreadcrumbWithinScope_NotVisibleOutside() using (sut.PushScope()) { sut.ConfigureScope(s => s.AddBreadcrumb("test")); - Assert.Single(sut.ScopeManager.GetCurrent().Scope.Breadcrumbs); + Assert.Single(sut.ScopeManager.GetCurrent().Item1.Breadcrumbs); } - Assert.Empty(sut.ScopeManager.GetCurrent().Scope.Breadcrumbs); + Assert.Empty(sut.ScopeManager.GetCurrent().Item1.Breadcrumbs); } [Fact] diff --git a/test/Sentry.Tests/Internals/SentryScopeManagerTests.cs b/test/Sentry.Tests/Internals/SentryScopeManagerTests.cs index 5580b36ecd..c539eb2104 100644 --- a/test/Sentry.Tests/Internals/SentryScopeManagerTests.cs +++ b/test/Sentry.Tests/Internals/SentryScopeManagerTests.cs @@ -21,10 +21,10 @@ private class Fixture private readonly Fixture _fixture = new Fixture(); [Fact] - public void GetCurrent_Scope_ReturnsInstance() => Assert.NotNull(_fixture.GetSut().GetCurrent().Scope); + public void GetCurrent_Scope_ReturnsInstance() => Assert.NotNull(_fixture.GetSut().GetCurrent().Item1); [Fact] - public void GetCurrent_Client_ReturnsInstance() => Assert.NotNull(_fixture.GetSut().GetCurrent().Client); + public void GetCurrent_Client_ReturnsInstance() => Assert.NotNull(_fixture.GetSut().GetCurrent().Item2); [Fact] public void GetCurrent_Equality_SameOnInstance() @@ -76,7 +76,7 @@ public void BindClient_Scope_StaysTheSame() var (scope, _) = sut.GetCurrent(); sut.BindClient(Substitute.For()); - Assert.Same(scope, sut.GetCurrent().Scope); + Assert.Same(scope, sut.GetCurrent().Item1); } [Fact] @@ -255,8 +255,8 @@ public async Task Async_IsolatedScopes() { var sut = _fixture.GetSut(); var root = sut.GetCurrent(); - void AddRandomTag() => sut.GetCurrent().Scope.SetTag(Guid.NewGuid().ToString(), "1"); - void AssertTagCount(int count) => Assert.Equal(count, sut.GetCurrent().Scope.Tags.Count); + void AddRandomTag() => sut.GetCurrent().Item1.SetTag(Guid.NewGuid().ToString(), "1"); + void AssertTagCount(int count) => Assert.Equal(count, sut.GetCurrent().Item1.Tags.Count); AddRandomTag(); AssertTagCount(1);