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

stacktrace translation for .net - awsxrayexporter #3280

Merged
merged 5 commits into from
May 3, 2021
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
69 changes: 69 additions & 0 deletions exporter/awsxrayexporter/translator/cause.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func parseException(exceptionType string, message string, stacktrace string, lan
exceptions = fillPythonStacktrace(stacktrace, exceptions)
case "javascript":
exceptions = fillJavaScriptStacktrace(stacktrace, exceptions)
case "dotnet":
exceptions = fillDotnetStacktrace(stacktrace, exceptions)
}

return exceptions
Expand Down Expand Up @@ -405,6 +407,73 @@ func fillJavaScriptStacktrace(stacktrace string, exceptions []awsxray.Exception)
return exceptions
}

func fillDotnetStacktrace(stacktrace string, exceptions []awsxray.Exception) []awsxray.Exception {
r := textproto.NewReader(bufio.NewReader(strings.NewReader(stacktrace)))

// Skip first line containing top level exception / message
r.ReadLine()
exception := &exceptions[0]
var line string
line, err := r.ReadLine()
if err != nil {
return exceptions
}

exception.Stack = make([]awsxray.StackFrame, 0)
for {
if strings.HasPrefix(line, "\tat ") {
index := strings.Index(line, " in ")
if index >= 0 {
parts := strings.Split(line, " in ")

label := parts[0][len("\tat "):]
path := parts[1]
lineNumber := 0

colonIdx := strings.LastIndexByte(parts[1], ':')
if colonIdx >= 0 {
lineStr := path[colonIdx+1:]

if strings.HasPrefix(lineStr, "line") {
lineStr = lineStr[5:]
}
path = path[0:colonIdx]
lineNumber, _ = strconv.Atoi(lineStr)
}

stack := awsxray.StackFrame{
Path: aws.String(path),
Label: aws.String(label),
Line: aws.Int(lineNumber),
}

exception.Stack = append(exception.Stack, stack)
} else {
idx := strings.LastIndexByte(line, ')')
if idx >= 0 {
label := line[len("\tat ") : idx+1]
path := ""
lineNumber := 0

stack := awsxray.StackFrame{
Path: aws.String(path),
Label: aws.String(label),
Line: aws.Int(lineNumber),
}

exception.Stack = append(exception.Stack, stack)
}
}
}

line, err = r.ReadLine()
if err != nil {
break
}
}
return exceptions
}

// indexOf returns position of the first occurrence of a Byte in str starting at pos index.
func indexOf(str string, c byte, pos int) int {
if pos < 0 {
Expand Down
92 changes: 92 additions & 0 deletions exporter/awsxrayexporter/translator/cause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,95 @@ func TestParseExceptionWithJavaScriptStactracekMalformedLines(t *testing.T) {
assert.Equal(t, "/home/gbusey/file.js", *exceptions[0].Stack[0].Path)
assert.Equal(t, 0, *exceptions[0].Stack[0].Line)
}

func TestParseExceptionWithSimpleStacktrace(t *testing.T) {
exceptionType := "System.FormatException"
message := "Input string was not in a correct format"

// We ignore the exception type / message from the stacktrace
stacktrace := `System.FormatException: Input string was not in a correct format.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.Number.ParseInt32(ReadOnlySpan1 value, NumberStyles styles, NumberFormatInfo info)
at System.Int32.Parse(String s)
at MyNamespace.IntParser.Parse(String s) in C:\apps\MyNamespace\IntParser.cs:line 11
at MyNamespace.Program.Main(String[] args) in C:\apps\MyNamespace\Program.cs:line 12`

exceptions := parseException(exceptionType, message, stacktrace, "dotnet")
assert.Len(t, exceptions, 1)
assert.Equal(t, "System.FormatException", *exceptions[0].Type)
assert.Equal(t, "Input string was not in a correct format", *exceptions[0].Message)
assert.Len(t, exceptions[0].Stack, 5)
assert.Equal(t, "System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)", *exceptions[0].Stack[0].Label)
assert.Equal(t, "", *exceptions[0].Stack[0].Path)
assert.Equal(t, 0, *exceptions[0].Stack[0].Line)
assert.Equal(t, "System.Number.ParseInt32(ReadOnlySpan1 value, NumberStyles styles, NumberFormatInfo info)", *exceptions[0].Stack[1].Label)
assert.Equal(t, "", *exceptions[0].Stack[1].Path)
assert.Equal(t, 0, *exceptions[0].Stack[1].Line)
assert.Equal(t, "System.Int32.Parse(String s)", *exceptions[0].Stack[2].Label)
assert.Equal(t, "", *exceptions[0].Stack[2].Path)
assert.Equal(t, 0, *exceptions[0].Stack[2].Line)
assert.Equal(t, "MyNamespace.IntParser.Parse(String s)", *exceptions[0].Stack[3].Label)
assert.Equal(t, "C:\\apps\\MyNamespace\\IntParser.cs", *exceptions[0].Stack[3].Path)
assert.Equal(t, 11, *exceptions[0].Stack[3].Line)
assert.Equal(t, "MyNamespace.Program.Main(String[] args)", *exceptions[0].Stack[4].Label)
assert.Equal(t, "C:\\apps\\MyNamespace\\Program.cs", *exceptions[0].Stack[4].Path)
assert.Equal(t, 12, *exceptions[0].Stack[4].Line)
}

func TestParseExceptionWithInnerExceptionStacktrace(t *testing.T) {
exceptionType := "System.Exception"
message := "test"

// We ignore the exception type / message from the stacktrace
stacktrace := `System.Exception: test
at integration_test_app.Controllers.AppController.OutgoingHttp() in /Users/bhautip/Documents/otel-dotnet/aws-otel-dotnet/integration-test-app/integration-test-app/Controllers/AppController.cs:line 21
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)`

exceptions := parseException(exceptionType, message, stacktrace, "dotnet")
assert.Len(t, exceptions, 1)
assert.Equal(t, "System.Exception", *exceptions[0].Type)
assert.Equal(t, "test", *exceptions[0].Message)
assert.Len(t, exceptions[0].Stack, 14)
assert.Equal(t, "integration_test_app.Controllers.AppController.OutgoingHttp()", *exceptions[0].Stack[0].Label)
assert.Equal(t, "/Users/bhautip/Documents/otel-dotnet/aws-otel-dotnet/integration-test-app/integration-test-app/Controllers/AppController.cs", *exceptions[0].Stack[0].Path)
assert.Equal(t, 21, *exceptions[0].Stack[0].Line)
assert.Equal(t, "Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)", *exceptions[0].Stack[9].Label)
assert.Equal(t, "", *exceptions[0].Stack[9].Path)
assert.Equal(t, 0, *exceptions[0].Stack[9].Line)
}

func TestParseExceptionWithMalformedStacktrace(t *testing.T) {
exceptionType := "System.Exception"
message := "test"

// We ignore the exception type / message from the stacktrace
stacktrace := `System.Exception: test
at integration_test_app.Controllers.AppController.OutgoingHttp() in /Users/bhautip/Documents/otel-dotnet/aws-otel-dotnet/integration-test-app/integration-test-app/Controllers/AppController.cs:line 21
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context malformed
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken) non-malformed`

exceptions := parseException(exceptionType, message, stacktrace, "dotnet")
assert.Len(t, exceptions, 1)
assert.Equal(t, "System.Exception", *exceptions[0].Type)
assert.Equal(t, "test", *exceptions[0].Message)
assert.Len(t, exceptions[0].Stack, 2)
assert.Equal(t, "integration_test_app.Controllers.AppController.OutgoingHttp()", *exceptions[0].Stack[0].Label)
assert.Equal(t, "/Users/bhautip/Documents/otel-dotnet/aws-otel-dotnet/integration-test-app/integration-test-app/Controllers/AppController.cs", *exceptions[0].Stack[0].Path)
assert.Equal(t, 21, *exceptions[0].Stack[0].Line)
assert.Equal(t, "System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)", *exceptions[0].Stack[1].Label)
assert.Equal(t, "", *exceptions[0].Stack[1].Path)
assert.Equal(t, 0, *exceptions[0].Stack[1].Line)
}