Skip to content
New issue

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

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

Already on GitHub? # to your account

ref: Don't use ValueTuple #263

Merged
merged 1 commit into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/Sentry.AspNetCore/SentryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sentry.Extensibility;
using Sentry.Protocol;
using Sentry.Reflection;

namespace Sentry.AspNetCore
Expand All @@ -23,7 +24,7 @@ internal class SentryMiddleware
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ILogger<SentryMiddleware> _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;
Expand Down
3 changes: 2 additions & 1 deletion src/Sentry.Extensions.Logging/SentryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.Log4Net/SentryAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.NLog/SentryTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.Serilog/SentrySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Sentry/Internal/IInternalScopeManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;

namespace Sentry.Internal
{
internal interface IInternalScopeManager : ISentryScopeManager
{
(Scope Scope, ISentryClient Client) GetCurrent();
Tuple<Scope, ISentryClient> GetCurrent();
}
}
2 changes: 1 addition & 1 deletion src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 14 additions & 13 deletions src/Sentry/Internal/SentryScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ namespace Sentry.Internal
internal class SentryScopeManager : IInternalScopeManager, IDisposable
{
private readonly SentryOptions _options;
private readonly AsyncLocal<ImmutableStack<(Scope, ISentryClient)>> _asyncLocalScope = new AsyncLocal<ImmutableStack<(Scope, ISentryClient)>>();
private readonly AsyncLocal<ImmutableStack<Tuple<Scope, ISentryClient>>> _asyncLocalScope = new AsyncLocal<ImmutableStack<Tuple<Scope, ISentryClient>>>();

internal ImmutableStack<(Scope scope, ISentryClient client)> ScopeAndClientStack
internal ImmutableStack<Tuple<Scope, ISentryClient>> ScopeAndClientStack
{
get => _asyncLocalScope.Value ?? (_asyncLocalScope.Value = NewStack());
set => _asyncLocalScope.Value = value;
}

private Func<ImmutableStack<(Scope, ISentryClient)>> NewStack { get; }
private Func<ImmutableStack<Tuple<Scope, ISentryClient>>> NewStack { get; }

public SentryScopeManager(
SentryOptions options,
ISentryClient rootClient)
{
Debug.Assert(rootClient != null);
_options = options;
NewStack = () => ImmutableStack.Create((new Scope(options), rootClient));
NewStack = () => ImmutableStack.Create(new Tuple<Scope, ISentryClient>(new Scope(options), rootClient));
}

public (Scope Scope, ISentryClient Client) GetCurrent() => ScopeAndClientStack.Peek();
public Tuple<Scope, ISentryClient> GetCurrent() => ScopeAndClientStack.Peek();

public void ConfigureScope(Action<Scope> configureScope)
{
_options?.DiagnosticLogger?.LogDebug("Configuring the scope.");
var scope = GetCurrent();
configureScope?.Invoke(scope.Scope);
configureScope?.Invoke(scope.Item1);
}

public Task ConfigureScopeAsync(Func<Scope, Task> 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<object>(null);
Expand All @@ -67,7 +67,7 @@ public IDisposable PushScope<TState>(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<Scope, ISentryClient>(clonedScope, client));

return scopeSnapshot;
}
Expand All @@ -77,7 +77,7 @@ public void WithScope(Action<Scope> scopeCallback)
using (PushScope())
{
var scope = GetCurrent();
scopeCallback?.Invoke(scope.Scope);
scopeCallback?.Invoke(scope.Item1);
}
}

Expand All @@ -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<Scope, ISentryClient>(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<Tuple<Scope, ISentryClient>> _snapshot;
private readonly SentryScopeManager _scopeManager;

public ScopeSnapshot(
SentryOptions options,
ImmutableStack<(Scope, ISentryClient)> snapshot,
ImmutableStack<Tuple<Scope, ISentryClient>> snapshot,
SentryScopeManager scopeManager)
{
Debug.Assert(snapshot != null);
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/Sentry/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SentryEvent, Sentry.SentryEvent> processor) -> void
static Sentry.ScopeExtensions.AddEventProcessors(this Sentry.Scope scope, System.Collections.Generic.IEnumerable<Sentry.Extensibility.ISentryEventProcessor> processors) -> void
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@

static Sentry.Reflection.AssemblyExtensions.GetNameAndVersion(this System.Reflection.Assembly asm) -> Sentry.Protocol.SdkVersion
7 changes: 4 additions & 3 deletions src/Sentry/Reflection/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Reflection;
using Sentry.Protocol;

namespace Sentry.Reflection
{
Expand All @@ -17,16 +18,16 @@ public static class AssemblyExtensions
/// If not available, falls back to <see cref="AssemblyName.Version"/>
/// </remarks>
/// <param name="asm">The assembly to get the name and version from</param>
/// <returns>A tuple of name and version</returns>
public static (string Name, string Version) GetNameAndVersion(this Assembly asm)
/// <returns>The SdkVersion.</returns>
public static SdkVersion GetNameAndVersion(this Assembly asm)
{
var asmName = asm.GetName();
var name = asmName.Name;
var version = asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion
?? asmName.Version.ToString();

return (name, version);
return new SdkVersion { Name = name, Version = version };
}
}
}
4 changes: 2 additions & 2 deletions test/Sentry.Tests/HubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions test/Sentry.Tests/Internals/SentryScopeManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -76,7 +76,7 @@ public void BindClient_Scope_StaysTheSame()
var (scope, _) = sut.GetCurrent();

sut.BindClient(Substitute.For<ISentryClient>());
Assert.Same(scope, sut.GetCurrent().Scope);
Assert.Same(scope, sut.GetCurrent().Item1);
}

[Fact]
Expand Down Expand Up @@ -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);
Expand Down