Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.
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
16 changes: 16 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"go.undefinedlabs.com/scopeagent/env"
scopeError "go.undefinedlabs.com/scopeagent/errors"
"go.undefinedlabs.com/scopeagent/instrumentation"
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
"go.undefinedlabs.com/scopeagent/reflection"
"go.undefinedlabs.com/scopeagent/runner"
"go.undefinedlabs.com/scopeagent/tags"
"go.undefinedlabs.com/scopeagent/tracer"
Expand Down Expand Up @@ -176,6 +178,20 @@ func WithRecorders(recorders ...tracer.SpanRecorder) Option {
}
}

func WithGlobalPanicHandler() Option {
return func(agent *Agent) {
reflection.AddPanicHandler(func(e interface{}) {
instrumentation.Logger().Printf("Panic handler triggered by: %v.\nFlushing agent, sending partial results...", scopeError.GetCurrentError(e).ErrorStack())
agent.Flush()
})
reflection.AddOnPanicExitHandler(func(e interface{}) {
instrumentation.Logger().Printf("Process is going to end by: %v,\nStopping agent...", scopeError.GetCurrentError(e).ErrorStack())
scopetesting.PanicAllRunningTests(e, 3)
agent.Stop()
})
}
}

// Creates a new Scope Agent instance
func NewAgent(options ...Option) (*Agent, error) {
agent := new(Agent)
Expand Down
3 changes: 2 additions & 1 deletion autoinstrument/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/undefinedlabs/go-mpatch"

"go.undefinedlabs.com/scopeagent"
"go.undefinedlabs.com/scopeagent/agent"
"go.undefinedlabs.com/scopeagent/instrumentation"
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
)
Expand Down Expand Up @@ -35,7 +36,7 @@ func init() {
}()
scopetesting.PatchTestingLogger()
defer scopetesting.UnpatchTestingLogger()
return scopeagent.Run(m)
return scopeagent.Run(m, agent.WithGlobalPanicHandler())
})
logOnError(err)
})
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ require (
github.com/opentracing/basictracer-go v1.0.0
github.com/opentracing/opentracing-go v1.1.0
github.com/stretchr/testify v1.5.1
github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98
github.com/undefinedlabs/go-mpatch v0.0.0-20200326085307-1a86426f42a6
github.com/vmihailenco/msgpack v4.0.4+incompatible
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/grpc v1.27.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98 h1:Z/megzdoMbuZ0H/oLmfTw92PAEfyTi1DkvAr8AUzFgw=
github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98/go.mod h1:TyJZDQ/5AgyN7FSLiBJ8RO9u2c6wbtRvK827b6AVqY4=
github.com/undefinedlabs/go-mpatch v0.0.0-20200326085307-1a86426f42a6 h1:VO1oVFjnL0fwOlwLpDqY1xhY/cfr0Ycz4aLwWM76D6k=
github.com/undefinedlabs/go-mpatch v0.0.0-20200326085307-1a86426f42a6/go.mod h1:TyJZDQ/5AgyN7FSLiBJ8RO9u2c6wbtRvK827b6AVqY4=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
11 changes: 0 additions & 11 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import (
"testing"

"go.undefinedlabs.com/scopeagent/agent"
"go.undefinedlabs.com/scopeagent/errors"
"go.undefinedlabs.com/scopeagent/instrumentation"
"go.undefinedlabs.com/scopeagent/instrumentation/logging"
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
"go.undefinedlabs.com/scopeagent/reflection"
)

var (
Expand Down Expand Up @@ -52,15 +50,6 @@ func Run(m *testing.M, opts ...agent.Option) int {
newAgent.Stop()
os.Exit(1)
}()
reflection.AddPanicHandler(func(e interface{}) {
instrumentation.Logger().Printf("Panic handler triggered by: %v.\nFlushing agent, sending partial results...", errors.GetCurrentError(e).ErrorStack())
newAgent.Flush()
})
reflection.AddOnPanicExitHandler(func(e interface{}) {
instrumentation.Logger().Printf("Process is going to end by: %v,\nStopping agent...", errors.GetCurrentError(e).ErrorStack())
scopetesting.PanicAllRunningTests(e, 3)
newAgent.Stop()
})

defaultAgent = newAgent
return newAgent.Run(m)
Expand Down