Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: SentryMiddleware re-entrant #3185

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions src/Sentry.AspNetCore/SentryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
var transactionContext = hub.ContinueTrace(traceHeader, baggageHeader);

// Adding the headers and the TransactionContext to the context to be picked up by the Sentry tracing middleware
context.Items.Add(TraceHeaderItemKey, traceHeader);
context.Items.Add(BaggageHeaderItemKey, baggageHeader);
context.Items.Add(TransactionContextItemKey, transactionContext);
var didAdd = context.Items.TryAdd(TraceHeaderItemKey, traceHeader);
if (!didAdd)
{
_options.LogDebug("Sentry trace was already added. Did you initialize Sentry twice?");
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
}
context.Items.TryAdd(BaggageHeaderItemKey, baggageHeader);
context.Items.TryAdd(TransactionContextItemKey, transactionContext);

hub.ConfigureScope(scope =>
{
Expand Down
28 changes: 28 additions & 0 deletions test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,34 @@
Assert.NotNull(transactionContext);
}

[Fact]
public async Task InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow()
{
var sut = _fixture.GetSut();
var request = Substitute.For<HttpRequest>();
var fakeHeaders = new HeaderDictionary
{
{ "Sentry-Trace", "4b4d2878507b43d3af7dd8c4ab7a96d9-3cc6fd1337d243de"},
{ "Baggage", "sentry-trace_id=4b4d2878507b43d3af7dd8c4ab7a96d9, sentry-public_key=eb18e953812b41c3aeb042e666fd3b5c"},
};
var contextItems = new Dictionary<object, object>();
_fixture.HttpContext.Items.When(items => items.Add(Arg.Any<object>(), Arg.Any<object>()))
.Do(info =>
{
contextItems.Add(info.Args()[0], info.Args()[1]);
});
_ = request.Headers.Returns(fakeHeaders);
_ = _fixture.HttpContext.Request.Returns(request);
_ = request.HttpContext.Returns(_fixture.HttpContext);

// Faking having added the middleware twice by invoking the request with the same context twice
await sut.InvokeAsync(_fixture.HttpContext, _fixture.RequestDelegate);
await sut.InvokeAsync(_fixture.HttpContext, _fixture.RequestDelegate);

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (ubuntu-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (ubuntu-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (ubuntu-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (ubuntu-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (ubuntu-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (ubuntu-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (windows-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (windows-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (windows-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (windows-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (windows-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (windows-latest)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (macos-13)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (macos-13)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (macos-13)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (macos-13)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (macos-13)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

Check failure on line 774 in test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

View workflow job for this annotation

GitHub Actions / .NET (macos-13)

Sentry.AspNetCore.Tests.SentryMiddlewareTests.InvokeAsync_InvokingWithTheSameContextTwice_DoesNotThrow

System.ArgumentException : An item with the same key has already been added. Key: System.Object

_fixture.HttpContext.Items.Received(3); // Sanity check
Assert.Equal(3, contextItems.Count);
}

[Fact]
public void PopulateScope_AddEventProcessors()
{
Expand Down
Loading