From 1b5e65d90583510dc42b93911809731d81c032cc Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Wed, 25 Mar 2020 21:51:24 +0100 Subject: [PATCH 1/2] Global panic handler as an agent option --- agent/agent.go | 16 ++++++++++++++++ autoinstrument/init.go | 3 ++- init.go | 11 ----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 35ec3ae3..93e921f0 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -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" @@ -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) diff --git a/autoinstrument/init.go b/autoinstrument/init.go index d76e4b7f..17abcde4 100644 --- a/autoinstrument/init.go +++ b/autoinstrument/init.go @@ -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" ) @@ -35,7 +36,7 @@ func init() { }() scopetesting.PatchTestingLogger() defer scopetesting.UnpatchTestingLogger() - return scopeagent.Run(m) + return scopeagent.Run(m, agent.WithGlobalPanicHandler()) }) logOnError(err) }) diff --git a/init.go b/init.go index afb3626f..fb3ae83d 100644 --- a/init.go +++ b/init.go @@ -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 ( @@ -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) From 1b9243099354e9c09fbf68fd2f8737c8a3f5b6cb Mon Sep 17 00:00:00 2001 From: drodriguezhdez Date: Thu, 26 Mar 2020 10:08:56 +0100 Subject: [PATCH 2/2] Update go-mpatch to 1.0.2 --- go.mod | 5 ++++- go.sum | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index e7306cf3..5bfa1e54 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 798fb5ca..e82076c1 100644 --- a/go.sum +++ b/go.sum @@ -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=