|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * THIS CODE IS WRITTEN IN GOLANG |
| 7 | + * |
| 8 | + * This is alternate code for the php stacktrace translator |
| 9 | + * located in the go-collector-contrib/exporter/awsxrayexporter/internal/translator/cause.go |
| 10 | + * |
| 11 | + * Currently the php repo contains a stacktrace translator that improves |
| 12 | + * upon original php stacktraces by allowing for exception chaining a |
| 13 | + * and giving exact lines of where exceptions occurred. |
| 14 | + * |
| 15 | + * If for any reason this no longer wants to be used, the exception |
| 16 | + * translator in cause.go can be replaced with the following code. It |
| 17 | + * translates original php stacktraces into awsxray format. |
| 18 | + */ |
| 19 | + |
| 20 | +// PUT IN CAUSE.GO FILE |
| 21 | +// func fillPhpStacktrace(stacktrace string, exceptions []awsxray.Exception) []awsxray.Exception { |
| 22 | +// r := textproto.NewReader(bufio.NewReader(strings.NewReader(stacktrace))) |
| 23 | + |
| 24 | +// exception := &exceptions[0] |
| 25 | +// var line string |
| 26 | +// line, err := r.ReadLine() |
| 27 | +// if err != nil { |
| 28 | +// return exceptions |
| 29 | +// } |
| 30 | + |
| 31 | +// exception.Stack = make([]awsxray.StackFrame, 0) |
| 32 | +// for { |
| 33 | +// if strings.HasPrefix(line, "#") { |
| 34 | +// parenFirstIdx := strings.IndexByte(line, '(') |
| 35 | +// parenLastIdx := strings.IndexByte(line, ')') |
| 36 | +// slashIdx := strings.IndexByte(line, '/') |
| 37 | +// colonIdx := strings.IndexByte(line, ':') |
| 38 | +// label := "" |
| 39 | +// path := "" |
| 40 | +// lineNumber := 0 |
| 41 | + |
| 42 | +// if slashIdx >= 0 && colonIdx >= 0 && colonIdx != len(line)-1 { |
| 43 | +// label = line[colonIdx+2:] |
| 44 | +// path = line[slashIdx:parenFirstIdx] |
| 45 | +// lineNumber, _ = strconv.Atoi(line[parenFirstIdx+1 : parenLastIdx]) |
| 46 | +// } |
| 47 | + |
| 48 | +// // only append the exception if all the values of the exception are not default |
| 49 | +// if path != "" || label != "" || lineNumber != 0 { |
| 50 | +// stack := awsxray.StackFrame{ |
| 51 | +// Path: aws.String(path), |
| 52 | +// Label: aws.String(label), |
| 53 | +// Line: aws.Int(lineNumber), |
| 54 | +// } |
| 55 | +// exception.Stack = append(exception.Stack, stack) |
| 56 | +// } |
| 57 | +// } |
| 58 | +// line, err = r.ReadLine() |
| 59 | +// if err != nil { |
| 60 | +// break |
| 61 | +// } |
| 62 | +// } |
| 63 | +// return exceptions |
| 64 | +// } |
| 65 | + |
| 66 | +//PUT IN CAUSE_TEST.GO FILE |
| 67 | +// func TestParseExceptionPhpStacktrace(t *testing.T) { |
| 68 | +// exceptionType := "Exception" |
| 69 | +// message := "Thrown from Class C" |
| 70 | + |
| 71 | +// stacktrace := `#0 /Users/olihamuy/Desktop/TestBay/test.php(66): C->exc() |
| 72 | +// #1 /Users/olihamuy/Desktop/TestBay/test.php(81): C->doexc() |
| 73 | +// #2 /Users/olihamuy/Desktop/TestBay/test.php(85): fail() |
| 74 | +// #3 {main}` |
| 75 | + |
| 76 | +// exceptions := parseException(exceptionType, message, stacktrace, "php") |
| 77 | + |
| 78 | +// assert.Len(t, exceptions, 1) |
| 79 | +// assert.NotEmpty(t, exceptions[0].ID) |
| 80 | +// assert.Equal(t, "Exception", *exceptions[0].Type) |
| 81 | +// assert.Equal(t, "Thrown from Class C", *exceptions[0].Message) |
| 82 | +// assert.Len(t, exceptions[0].Stack, 3) |
| 83 | +// assert.Equal(t, "C->exc()", *exceptions[0].Stack[0].Label) |
| 84 | +// assert.Equal(t, "/Users/olihamuy/Desktop/TestBay/test.php", *exceptions[0].Stack[0].Path) |
| 85 | +// assert.Equal(t, 66, *exceptions[0].Stack[0].Line) |
| 86 | +// assert.Equal(t, "C->doexc()", *exceptions[0].Stack[1].Label) |
| 87 | +// assert.Equal(t, "/Users/olihamuy/Desktop/TestBay/test.php", *exceptions[0].Stack[1].Path) |
| 88 | +// assert.Equal(t, 81, *exceptions[0].Stack[1].Line) |
| 89 | +// assert.Equal(t, "fail()", *exceptions[0].Stack[2].Label) |
| 90 | +// assert.Equal(t, "/Users/olihamuy/Desktop/TestBay/test.php", *exceptions[0].Stack[2].Path) |
| 91 | +// assert.Equal(t, 85, *exceptions[0].Stack[2].Line) |
| 92 | +// } |
| 93 | + |
| 94 | +// func TestParseExceptionPhpStacktraceMalformed(t *testing.T) { |
| 95 | +// exceptionType := "Exception" |
| 96 | +// message := "Thrown from Class C" |
| 97 | + |
| 98 | +// stacktrace := `#0 /Users/olihamuy/Desktop/TestBay/test.php(66): C->exc() |
| 99 | +// #1 /Users/olihamuy/Desktop/TestBay/test.php(81): C->doexc() |
| 100 | +// #2 /Users/olihamuy/Desktop/TestBay/test.php(85) fail() |
| 101 | +// #3 /Users/olihamuy/Desktop/TestBay/test.php(85): |
| 102 | +// #4 /Users/olihamuy/Desktop/TestBay/test.php(): fail() |
| 103 | +// #5 {main}` |
| 104 | + |
| 105 | +// exceptions := parseException(exceptionType, message, stacktrace, "php") |
| 106 | + |
| 107 | +// assert.Len(t, exceptions, 1) |
| 108 | +// assert.NotEmpty(t, exceptions[0].ID) |
| 109 | +// assert.Equal(t, "Exception", *exceptions[0].Type) |
| 110 | +// assert.Equal(t, "Thrown from Class C", *exceptions[0].Message) |
| 111 | +// assert.Len(t, exceptions[0].Stack, 3) |
| 112 | +// assert.Equal(t, "C->exc()", *exceptions[0].Stack[0].Label) |
| 113 | +// assert.Equal(t, "/Users/olihamuy/Desktop/TestBay/test.php", *exceptions[0].Stack[0].Path) |
| 114 | +// assert.Equal(t, 66, *exceptions[0].Stack[0].Line) |
| 115 | +// assert.Equal(t, "fail()", *exceptions[0].Stack[2].Label) |
| 116 | +// assert.Equal(t, "/Users/olihamuy/Desktop/TestBay/test.php", *exceptions[0].Stack[2].Path) |
| 117 | +// assert.Equal(t, 0, *exceptions[0].Stack[2].Line) |
| 118 | +// } |
| 119 | + |
| 120 | +// func TestParseExceptionPhpEmptyStacktrace(t *testing.T) { |
| 121 | +// exceptionType := "Exception" |
| 122 | +// message := "Thrown from Class C" |
| 123 | + |
| 124 | +// stacktrace := "" |
| 125 | + |
| 126 | +// exceptions := parseException(exceptionType, message, stacktrace, "php") |
| 127 | + |
| 128 | +// assert.Len(t, exceptions, 1) |
| 129 | +// assert.NotEmpty(t, exceptions[0].ID) |
| 130 | +// assert.Equal(t, "Exception", *exceptions[0].Type) |
| 131 | +// assert.Equal(t, "Thrown from Class C", *exceptions[0].Message) |
| 132 | +// assert.Len(t, exceptions[0].Stack, 0) |
| 133 | +// } |
0 commit comments