-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Building out a sourcemap system for sentry (#1736)
* feat(api): Building out a sourcemap system for sentry I want to be able to bundle the golang source code in the sentry SDK at build time. This is progress towards that. * feat(api): Getting contextify and source reader in.
- Loading branch information
1 parent
fccc016
commit 807e455
Showing
12 changed files
with
298 additions
and
114 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,36 @@ | ||
|
||
|
||
set(GO_SRC_DIR "${CMAKE_SOURCE_DIR}/server") | ||
file(GLOB_RECURSE APP_GO_FILES | ||
"${GO_SRC_DIR}/*.go" | ||
) | ||
list(FILTER APP_GO_FILES EXCLUDE REGEX ".+_test\\.go") | ||
list(FILTER APP_GO_FILES EXCLUDE REGEX "source/embed") | ||
|
||
set(SOURCEMAP_MARKER ${CMAKE_BINARY_DIR}/golang-sourcemap.txt) | ||
add_custom_command( | ||
OUTPUT ${SOURCEMAP_MARKER} | ||
COMMENT "Finished generating source maps for sentry from golang" | ||
COMMAND ${CMAKE_COMMAND} -E touch ${SOURCEMAP_MARKER} | ||
) | ||
|
||
add_custom_target( | ||
sourcemaps.golang | ||
DEPENDS ${SOURCEMAP_MARKER} | ||
) | ||
|
||
foreach(FILE ${APP_GO_FILES}) | ||
string(REPLACE "${CMAKE_SOURCE_DIR}/" "${CMAKE_CURRENT_SOURCE_DIR}/embed/" OUTPUT_GO_FILE "${FILE}") | ||
string(REPLACE "${CMAKE_SOURCE_DIR}/" "" FILE_FRIENDLY "${FILE}") | ||
get_filename_component(OUTPUT_GO_DIRECTORY ${OUTPUT_GO_FILE} DIRECTORY) | ||
add_custom_command( | ||
OUTPUT ${OUTPUT_GO_FILE}.txt | ||
BYPRODUCTS ${OUTPUT_GO_FILE}.txt | ||
COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTPUT_GO_DIRECTORY} | ||
COMMAND ${CMAKE_COMMAND} -E copy ${FILE} ${OUTPUT_GO_FILE}.txt | ||
COMMENT "Generating source map for ${FILE_FRIENDLY}" | ||
DEPENDS ${FILE} | ||
) | ||
add_custom_command(OUTPUT ${SOURCEMAP_MARKER} APPEND DEPENDS ${OUTPUT_GO_FILE}.txt) | ||
endforeach() | ||
|
This file contains 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,118 @@ | ||
/* | ||
FROM: https://github.com/getsentry/sentry-go/blob/df20ce63bbede6de539d2ad2f15f7daf2b703c65/integrations.go#L215-L328 | ||
MIT License | ||
Copyright (c) 2019 Functional Software, Inc. dba Sentry | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package source | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/getsentry/sentry-go" | ||
) | ||
|
||
// ================================ | ||
// Contextify Frames Integration | ||
// ================================ | ||
|
||
type ContextifyFramesIntegration struct { | ||
sr sourceReader | ||
contextLines int | ||
cachedLocations sync.Map | ||
} | ||
|
||
func (cfi *ContextifyFramesIntegration) Name() string { | ||
return "ContextifyFrames" | ||
} | ||
|
||
func (cfi *ContextifyFramesIntegration) SetupOnce(client *sentry.Client) { | ||
cfi.sr = newSourceReader() | ||
cfi.contextLines = 5 | ||
|
||
client.AddEventProcessor(cfi.processor) | ||
} | ||
|
||
func (cfi *ContextifyFramesIntegration) processor(event *sentry.Event, _ *sentry.EventHint) *sentry.Event { | ||
// Range over all exceptions | ||
for _, ex := range event.Exception { | ||
// If it has no stacktrace, just bail out | ||
if ex.Stacktrace == nil { | ||
continue | ||
} | ||
|
||
// If it does, it should have frames, so try to contextify them | ||
ex.Stacktrace.Frames = cfi.contextify(ex.Stacktrace.Frames) | ||
} | ||
|
||
// Range over all threads | ||
for _, th := range event.Threads { | ||
// If it has no stacktrace, just bail out | ||
if th.Stacktrace == nil { | ||
continue | ||
} | ||
|
||
// If it does, it should have frames, so try to contextify them | ||
th.Stacktrace.Frames = cfi.contextify(th.Stacktrace.Frames) | ||
} | ||
|
||
return event | ||
} | ||
|
||
func (cfi *ContextifyFramesIntegration) contextify(frames []sentry.Frame) []sentry.Frame { | ||
contextifiedFrames := make([]sentry.Frame, 0, len(frames)) | ||
|
||
for _, frame := range frames { | ||
if !frame.InApp { | ||
contextifiedFrames = append(contextifiedFrames, frame) | ||
continue | ||
} | ||
|
||
path := frame.AbsPath | ||
if path == "" { | ||
contextifiedFrames = append(contextifiedFrames, frame) | ||
continue | ||
} | ||
|
||
lines, contextLine := cfi.sr.readContextLines(path, frame.Lineno, cfi.contextLines) | ||
contextifiedFrames = append(contextifiedFrames, cfi.addContextLinesToFrame(frame, lines, contextLine)) | ||
} | ||
|
||
return contextifiedFrames | ||
} | ||
|
||
func (cfi *ContextifyFramesIntegration) addContextLinesToFrame(frame sentry.Frame, lines [][]byte, contextLine int) sentry.Frame { | ||
for i, line := range lines { | ||
switch { | ||
case i < contextLine: | ||
frame.PreContext = append(frame.PreContext, string(line)) | ||
case i == contextLine: | ||
frame.ContextLine = string(line) | ||
default: | ||
frame.PostContext = append(frame.PostContext, string(line)) | ||
} | ||
} | ||
return frame | ||
} |
This file contains 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,3 @@ | ||
# Embedded Sources | ||
|
||
This directory contains embedded source code from the build. |
This file contains 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,27 @@ | ||
package source | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
//go:embed embed/** | ||
var sourceCode embed.FS | ||
|
||
var basePath string | ||
|
||
func init() { | ||
pc, _, _, ok := runtime.Caller(0) | ||
details := runtime.FuncForPC(pc) | ||
if ok && details != nil { | ||
path, _ := details.FileLine(pc) | ||
basePath = strings.TrimSuffix(path, "/server/internal/source/source.go") | ||
} | ||
} | ||
|
||
func transformFileName(path string) string { | ||
newPath := strings.TrimPrefix(path, basePath) | ||
return fmt.Sprintf("/embed/%s.txt", newPath) | ||
} |
This file contains 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,97 @@ | ||
/* | ||
FROM: https://github.com/getsentry/sentry-go/blob/df20ce63bbede6de539d2ad2f15f7daf2b703c65/sourcereader.go | ||
MIT License | ||
Copyright (c) 2019 Functional Software, Inc. dba Sentry | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package source | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
) | ||
|
||
type sourceReader struct { | ||
mu sync.Mutex | ||
cache map[string][][]byte | ||
} | ||
|
||
func newSourceReader() sourceReader { | ||
return sourceReader{ | ||
cache: make(map[string][][]byte), | ||
} | ||
} | ||
|
||
func (sr *sourceReader) readContextLines(filename string, line, context int) ([][]byte, int) { | ||
sr.mu.Lock() | ||
defer sr.mu.Unlock() | ||
|
||
lines, ok := sr.cache[filename] | ||
|
||
if !ok { | ||
data, err := sourceCode.ReadFile(transformFileName(filename)) | ||
if err != nil { | ||
sr.cache[filename] = nil | ||
return nil, 0 | ||
} | ||
lines = bytes.Split(data, []byte{'\n'}) | ||
sr.cache[filename] = lines | ||
} | ||
|
||
return sr.calculateContextLines(lines, line, context) | ||
} | ||
|
||
func (sr *sourceReader) calculateContextLines(lines [][]byte, line, context int) ([][]byte, int) { | ||
// Stacktrace lines are 1-indexed, slices are 0-indexed | ||
line-- | ||
|
||
// contextLine points to a line that caused an issue itself, in relation to | ||
// returned slice. | ||
contextLine := context | ||
|
||
if lines == nil || line >= len(lines) || line < 0 { | ||
return nil, 0 | ||
} | ||
|
||
if context < 0 { | ||
context = 0 | ||
contextLine = 0 | ||
} | ||
|
||
start := line - context | ||
|
||
if start < 0 { | ||
contextLine += start | ||
start = 0 | ||
} | ||
|
||
end := line + context + 1 | ||
|
||
if end > len(lines) { | ||
end = len(lines) | ||
} | ||
|
||
return lines[start:end], contextLine | ||
} |