Skip to content

Commit

Permalink
Add fallback to Scope Stack from AspNet. (#1180)
Browse files Browse the repository at this point in the history
* Add fallback to scopeStack.

* Add changelog.
  • Loading branch information
lucas-zimerman authored Sep 2, 2021
1 parent 5a305f8 commit f47367c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Add fallback to Scope Stack from AspNet ([#1180](https://github.com/getsentry/sentry-dotnet/pull/1180))

## 3.9.0

### Features
Expand Down
24 changes: 22 additions & 2 deletions src/Sentry.AspNet/Internal/HttpContextScopeStackContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,30 @@ internal class HttpContextScopeStackContainer : IScopeStackContainer
{
private const string FieldName = "__SentryScopeStack";

//Internal for testing
internal KeyValuePair<Scope, ISentryClient>[]? FallbackStack;

public KeyValuePair<Scope, ISentryClient>[]? Stack
{
get => HttpContext.Current.Items[FieldName] as KeyValuePair<Scope, ISentryClient>[];
set => HttpContext.Current.Items[FieldName] = value;
get
{
if (HttpContext.Current is { } currentContext)
{
return currentContext.Items[FieldName] as KeyValuePair<Scope, ISentryClient>[];
}
return FallbackStack;
}
set
{
if (HttpContext.Current is { } currentContext)
{
currentContext.Items[FieldName] = value;
}
else
{
FallbackStack = value;
}
}
}
}
}
43 changes: 43 additions & 0 deletions test/Sentry.AspNet.Tests/Internal/SentryScopeManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NSubstitute;
using Sentry.AspNet.Internal;
using Sentry.Internal;
using Xunit;

namespace Sentry.AspNet.Tests.Internal
{
public class SentryScopeManagerTests
{
private class Fixture
{
public SentryScopeManager GetSut() => new SentryScopeManager(
new HttpContextScopeStackContainer(),
new SentryOptions(),
Substitute.For<ISentryClient>());
}

private Fixture _fixture => new();

[Fact]
public void SetScopeStack_NoHttpContext_FallbackSet()
{
// Arrange
var scopeManager = _fixture.GetSut();

// Act
scopeManager.PushScope();

// Assert
Assert.NotNull(scopeManager.ScopeStackContainer.Stack);
}

[Fact]
public void GetScopeStack_NoHttpContext_Null()
{
// Arrange
var scopeManager = _fixture.GetSut();

// Assert
Assert.Null(scopeManager.ScopeStackContainer.Stack);
}
}
}

0 comments on commit f47367c

Please sign in to comment.