-
-
Notifications
You must be signed in to change notification settings - Fork 226
Added StringStackTraceFactory #4362
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2dff37f
Added SimpleStackTraceFactory
jamescrosswell 02e0a99
Update CHANGELOG.md
jamescrosswell d9dd0aa
Update ApiApprovalTests.Run.Net4_8.verified.txt
getsentry-bot d52b2ad
Added a simple verify test
jamescrosswell 9796a17
Merge branch 'main' into simple-stack-traces
jamescrosswell 703c19f
Update SimpleStackTraceFactory.cs
jamescrosswell 357ad20
Update SimpleStackTraceFactoryTests.cs
jamescrosswell 9a83f63
.
jamescrosswell 4e47a58
Update GlobalUsings.cs
jamescrosswell 16a54ee
.
jamescrosswell 400f5ba
Review feedback
jamescrosswell da2f702
.
jamescrosswell 0223cf9
Dropped obsolete regexes
jamescrosswell 45b3258
Update ApiApprovalTests.Run.Net4_8.verified.txt
getsentry-bot e4120a9
Update CHANGELOG.md
jamescrosswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Sentry.Extensibility; | ||
|
|
||
| /// <summary> | ||
| /// A rudimentary implementation of <see cref="ISentryStackTraceFactory"/> that simply parses the | ||
| /// string representation of the stack trace from an exception. This lacks many of the features | ||
| /// off the full <see cref="SentryStackTraceFactory"/>, however it may be useful in AOT compiled | ||
| /// applications where the full factory is not returning a useful stack trace. | ||
| /// <remarks>SimpleStackTraceFactory is currently EXPERIMENTAL.</remarks> | ||
| /// </summary> | ||
| public partial class SimpleStackTraceFactory : ISentryStackTraceFactory | ||
jamescrosswell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jamescrosswell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| private readonly SentryOptions _options; | ||
| private const string FullStackTraceLinePattern = @"at (?<Module>[^\.]+)\.(?<Function>.*?) in (?<FileName>.*?):line (?<LineNo>\d+)"; | ||
| private const string StackTraceLinePattern = @"at (.+)\.(.+) \+"; | ||
|
|
||
| #if NET9_0_OR_GREATER | ||
| [GeneratedRegex(FullStackTraceLinePattern)] | ||
| private static partial Regex FullStackTraceLine { get; } | ||
| #elif NET8_0 | ||
| private static readonly Regex FullStackTraceLine = FullStackTraceLineRegex(); | ||
|
|
||
| [GeneratedRegex(FullStackTraceLinePattern)] | ||
| private static partial Regex FullStackTraceLineRegex(); | ||
| #else | ||
| private static readonly Regex FullStackTraceLine = new (FullStackTraceLinePattern, RegexOptions.Compiled); | ||
| #endif | ||
jamescrosswell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #if NET9_0_OR_GREATER | ||
| [GeneratedRegex(StackTraceLinePattern)] | ||
| private static partial Regex StackTraceLine { get; } | ||
| #elif NET8_0 | ||
| private static readonly Regex StackTraceLine = StackTraceLineRegex(); | ||
|
|
||
| [GeneratedRegex(StackTraceLinePattern)] | ||
| private static partial Regex StackTraceLineRegex(); | ||
| #else | ||
| private static readonly Regex StackTraceLine = new (StackTraceLinePattern, RegexOptions.Compiled); | ||
| #endif | ||
|
|
||
| /// <summary> | ||
| /// Creates a new instance of <see cref="SimpleStackTraceFactory"/>. | ||
| /// </summary> | ||
| /// <param name="options">The sentry options</param> | ||
| public SimpleStackTraceFactory(SentryOptions options) | ||
| { | ||
| _options = options; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="ISentryStackTraceFactory.Create"/> | ||
| public SentryStackTrace? Create(Exception? exception = null) | ||
| { | ||
| _options.LogDebug("Source Stack Trace: {0}", exception?.StackTrace); | ||
Flash0ver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var trace = new SentryStackTrace(); | ||
| var frames = new List<SentryStackFrame>(); | ||
|
|
||
| var newlines = new[] { Environment.NewLine }; | ||
jamescrosswell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var lines = exception?.StackTrace?.Split(newlines, StringSplitOptions.RemoveEmptyEntries) ?? []; | ||
| foreach (var line in lines) | ||
| { | ||
| var fullMatch = FullStackTraceLine.Match(line); | ||
| if (fullMatch.Success) | ||
| { | ||
| frames.Add(new SentryStackFrame() | ||
| { | ||
| Module = fullMatch.Groups[1].Value, | ||
| Function = fullMatch.Groups[2].Value, | ||
| FileName = fullMatch.Groups[3].Value, | ||
| LineNumber = int.Parse(fullMatch.Groups[4].Value), | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| _options.LogDebug("Full stack frame match failed for: {0}", line); | ||
| var lineMatch = StackTraceLine.Match(line); | ||
| if (lineMatch.Success) | ||
| { | ||
| frames.Add(new SentryStackFrame() | ||
| { | ||
| Module = lineMatch.Groups[1].Value, | ||
| Function = lineMatch.Groups[2].Value | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| _options.LogDebug("Stack frame match failed for: {0}", line); | ||
| frames.Add(new SentryStackFrame() | ||
| { | ||
| Function = line | ||
| }); | ||
| } | ||
|
|
||
| trace.Frames = frames; | ||
| return trace; | ||
jamescrosswell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
test/Sentry.Tests/Internals/SimpleStackTraceFactoryTests.MethodGeneric.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| FileName: {ProjectDirectory}Internals/SimpleStackTraceFactoryTests.cs, | ||
| Function: Tests.Internals.SimpleStackTraceFactoryTests.GenericMethodThatThrows[T](T value), | ||
| Module: Other | ||
| } |
42 changes: 42 additions & 0 deletions
42
test/Sentry.Tests/Internals/SimpleStackTraceFactoryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // ReSharper disable once CheckNamespace | ||
| // Stack trace filters out Sentry frames by namespace | ||
| namespace Other.Tests.Internals; | ||
|
|
||
| #if PLATFORM_NEUTRAL | ||
|
|
||
| public class SimpleStackTraceFactoryTests | ||
| { | ||
| [Fact] | ||
| public Task MethodGeneric() | ||
| { | ||
| // Arrange | ||
| const int i = 5; | ||
| var exception = Record.Exception(() => GenericMethodThatThrows(i)); | ||
|
|
||
| var options = new SentryOptions | ||
| { | ||
| AttachStacktrace = true | ||
jamescrosswell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| var factory = new SimpleStackTraceFactory(options); | ||
|
|
||
| // Act | ||
| var stackTrace = factory.Create(exception); | ||
|
|
||
| // Assert; | ||
| var frame = stackTrace!.Frames.Single(x => x.Function!.Contains("GenericMethodThatThrows")); | ||
| return Verify(frame) | ||
| .IgnoreMembers<SentryStackFrame>( | ||
| x => x.Package, | ||
| x => x.LineNumber, | ||
| x => x.ColumnNumber, | ||
| x => x.InstructionAddress, | ||
| x => x.FunctionId) | ||
| .AddScrubber(x => x.Replace(@"\", @"/")); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void GenericMethodThatThrows<T>(T value) => | ||
| throw new Exception(); | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it on by default on AOT then?
How do I turn it on?
Would be nice to have a mention of that in the changelog too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps something we could also document in
Troubleshootingwhen released?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah when @Flash0ver and I discussed we were thinking we wouldn't promote this too widely, since it's quite experimental. However there are some additional docs on
SentryOptions.UseStackTraceFactory(which is how you'd add this):sentry-dotnet/src/Sentry/SentryOptions.cs
Lines 1637 to 1643 in e4120a9
And we figured we'd add something to our troubleshooting docs as well...
I'll add a blurb in the description of this PR as well.