TUN-6724: Migrate to sentry-go from raven-go

This commit is contained in:
Devin Carr
2022-12-24 21:44:15 -07:00
parent 87bd36c924
commit 794e8e622f
183 changed files with 33669 additions and 4852 deletions

View File

@@ -1,5 +1,5 @@
*.test
*.out
example/example
docs/_build
docs/doctrees
/xunit.xml
/coverage.xml

View File

@@ -1,3 +0,0 @@
[submodule "docs/_sentryext"]
path = docs/_sentryext
url = https://github.com/getsentry/sentry-doc-support

View File

@@ -1,26 +1,41 @@
sudo: false
language: go
go:
- "1.2"
- "1.3"
- 1.4.x
- 1.5.x
- 1.6.x
- 1.7.x
- 1.8.x
- 1.9.x
- 1.10.x
- 1.11.x
- tip
before_install:
- go install -race std
- go get golang.org/x/tools/cmd/cover
- go get github.com/tebeka/go2xunit
- go get github.com/t-yuki/gocover-cobertura
- go get -v ./...
script:
- go test -v -race ./...
- go test -v -cover ./...
- go test -v -race ./... | tee gotest.out
- $GOPATH/bin/go2xunit -fail -input gotest.out -output xunit.xml
- go test -v -coverprofile=coverage.txt -covermode count .
- $GOPATH/bin/gocover-cobertura < coverage.txt > coverage.xml
after_script:
- npm install -g @zeus-ci/cli
- zeus upload -t "application/x-cobertura+xml" coverage.xml
- zeus upload -t "application/x-xunit+xml" xunit.xml
matrix:
allow_failures:
- go: tip
notifications:
webhooks:
urls:
- https://zeus.ci/hooks/cd949996-d30a-11e8-ba53-0a580a28042d/public/provider/travis/webhook
on_success: always
on_failure: always
on_start: always
on_cancel: always
on_error: always

View File

@@ -1,6 +1,10 @@
# raven [![Build Status](https://travis-ci.org/getsentry/raven-go.png?branch=master)](https://travis-ci.org/getsentry/raven-go)
# raven
raven is a Go client for the [Sentry](https://github.com/getsentry/sentry)
[![Build Status](https://api.travis-ci.org/getsentry/raven-go.svg?branch=master)](https://travis-ci.org/getsentry/raven-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/getsentry/raven-go)](https://goreportcard.com/report/github.com/getsentry/raven-go)
[![GoDoc](https://godoc.org/github.com/getsentry/raven-go?status.svg)](https://godoc.org/github.com/getsentry/raven-go)
raven is the official Go SDK for the [Sentry](https://github.com/getsentry/sentry)
event/error logging system.
- [**API Documentation**](https://godoc.org/github.com/getsentry/raven-go)
@@ -11,3 +15,5 @@ event/error logging system.
```text
go get github.com/getsentry/raven-go
```
Note: Go 1.7 and newer are supported.

View File

@@ -945,7 +945,7 @@ func (t *HTTPTransport) Send(url, authHeader string, packet *Packet) error {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("raven: got http status %d", res.StatusCode)
return fmt.Errorf("raven: got http status %d - x-sentry-error: %s", res.StatusCode, res.Header.Get("X-Sentry-Error"))
}
return nil
}

View File

@@ -69,17 +69,31 @@ func (h *Http) Class() string { return "request" }
// ...
// }))
func RecoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
return Recoverer(http.HandlerFunc(handler)).ServeHTTP
}
// Recovery handler to wrap the stdlib net/http Mux.
// Example:
// mux := http.NewServeMux
// ...
// http.Handle("/", raven.Recoverer(mux))
func Recoverer(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
rvalStr := fmt.Sprint(rval)
packet := NewPacket(rvalStr, NewException(errors.New(rvalStr), GetOrNewStacktrace(rval.(error), 2, 3, nil)), NewHttp(r))
var packet *Packet
if err, ok := rval.(error); ok {
packet = NewPacket(rvalStr, NewException(errors.New(rvalStr), GetOrNewStacktrace(err, 2, 3, nil)), NewHttp(r))
} else {
packet = NewPacket(rvalStr, NewException(errors.New(rvalStr), NewStacktrace(2, 3, nil)), NewHttp(r))
}
Capture(packet, nil)
w.WriteHeader(http.StatusInternalServerError)
}
}()
handler(w, r)
}
handler.ServeHTTP(w, r)
})
}

View File

@@ -61,14 +61,17 @@ func GetOrNewStacktrace(err error, skip int, context int, appPackagePrefixes []s
for _, f := range stacktracer.StackTrace() {
pc := uintptr(f) - 1
fn := runtime.FuncForPC(pc)
var fName string
var file string
var line int
if fn != nil {
file, line = fn.FileLine(pc)
fName = fn.Name()
} else {
file = "unknown"
fName = "unknown"
}
frame := NewStacktraceFrame(pc, file, line, context, appPackagePrefixes)
frame := NewStacktraceFrame(pc, fName, file, line, context, appPackagePrefixes)
if frame != nil {
frames = append([]*StacktraceFrame{frame}, frames...)
}
@@ -89,14 +92,27 @@ func GetOrNewStacktrace(err error, skip int, context int, appPackagePrefixes []s
// be considered "in app".
func NewStacktrace(skip int, context int, appPackagePrefixes []string) *Stacktrace {
var frames []*StacktraceFrame
for i := 1 + skip; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
callerPcs := make([]uintptr, 100)
numCallers := runtime.Callers(skip+2, callerPcs)
// If there are no callers, the entire stacktrace is nil
if numCallers == 0 {
return nil
}
callersFrames := runtime.CallersFrames(callerPcs)
for {
fr, more := callersFrames.Next()
if fr.Func != nil {
frame := NewStacktraceFrame(fr.PC, fr.Function, fr.File, fr.Line, context, appPackagePrefixes)
if frame != nil {
frames = append(frames, frame)
}
}
frame := NewStacktraceFrame(pc, file, line, context, appPackagePrefixes)
if frame != nil {
frames = append(frames, frame)
if !more {
break
}
}
// If there are no frames, the entire stacktrace is nil
@@ -122,9 +138,9 @@ func NewStacktrace(skip int, context int, appPackagePrefixes []string) *Stacktra
//
// appPackagePrefixes is a list of prefixes used to check whether a package should
// be considered "in app".
func NewStacktraceFrame(pc uintptr, file string, line, context int, appPackagePrefixes []string) *StacktraceFrame {
func NewStacktraceFrame(pc uintptr, fName, file string, line, context int, appPackagePrefixes []string) *StacktraceFrame {
frame := &StacktraceFrame{AbsolutePath: file, Filename: trimPath(file), Lineno: line, InApp: false}
frame.Module, frame.Function = functionName(pc)
frame.Module, frame.Function = functionName(fName)
// `runtime.goexit` is effectively a placeholder that comes from
// runtime/asm_amd64.s and is meaningless.
@@ -143,7 +159,7 @@ func NewStacktraceFrame(pc uintptr, file string, line, context int, appPackagePr
}
if context > 0 {
contextLines, lineIdx := fileContext(file, line, context)
contextLines, lineIdx := sourceCodeLoader.Load(file, line, context)
if len(contextLines) > 0 {
for i, line := range contextLines {
switch {
@@ -157,7 +173,7 @@ func NewStacktraceFrame(pc uintptr, file string, line, context int, appPackagePr
}
}
} else if context == -1 {
contextLine, _ := fileContext(file, line, 0)
contextLine, _ := sourceCodeLoader.Load(file, line, 0)
if len(contextLine) > 0 {
frame.ContextLine = string(contextLine[0])
}
@@ -166,12 +182,8 @@ func NewStacktraceFrame(pc uintptr, file string, line, context int, appPackagePr
}
// Retrieve the name of the package and function containing the PC.
func functionName(pc uintptr) (pack string, name string) {
fn := runtime.FuncForPC(pc)
if fn == nil {
return
}
name = fn.Name()
func functionName(fName string) (pack string, name string) {
name = fName
// We get this:
// runtime/debug.*T·ptrmethod
// and want this:
@@ -185,24 +197,36 @@ func functionName(pc uintptr) (pack string, name string) {
return
}
var fileCacheLock sync.Mutex
var fileCache = make(map[string][][]byte)
type SourceCodeLoader interface {
Load(filename string, line, context int) ([][]byte, int)
}
func fileContext(filename string, line, context int) ([][]byte, int) {
fileCacheLock.Lock()
defer fileCacheLock.Unlock()
lines, ok := fileCache[filename]
var sourceCodeLoader SourceCodeLoader = &fsLoader{cache: make(map[string][][]byte)}
func SetSourceCodeLoader(loader SourceCodeLoader) {
sourceCodeLoader = loader
}
type fsLoader struct {
mu sync.Mutex
cache map[string][][]byte
}
func (fs *fsLoader) Load(filename string, line, context int) ([][]byte, int) {
fs.mu.Lock()
defer fs.mu.Unlock()
lines, ok := fs.cache[filename]
if !ok {
data, err := ioutil.ReadFile(filename)
if err != nil {
// cache errors as nil slice: code below handles it correctly
// otherwise when missing the source or running as a different user, we try
// reading the file on each error which is unnecessary
fileCache[filename] = nil
fs.cache[filename] = nil
return nil, 0
}
lines = bytes.Split(data, []byte{'\n'})
fileCache[filename] = lines
fs.cache[filename] = lines
}
if lines == nil {