Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit db10f26

Browse files
Global panic handler as an agent option (#208)
* Global panic handler as an agent option * Update go-mpatch to 1.0.2 Co-authored-by: drodriguezhdez <[email protected]>
1 parent cd8fc09 commit db10f26

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

agent/agent.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"go.undefinedlabs.com/scopeagent/env"
2323
scopeError "go.undefinedlabs.com/scopeagent/errors"
2424
"go.undefinedlabs.com/scopeagent/instrumentation"
25+
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
26+
"go.undefinedlabs.com/scopeagent/reflection"
2527
"go.undefinedlabs.com/scopeagent/runner"
2628
"go.undefinedlabs.com/scopeagent/tags"
2729
"go.undefinedlabs.com/scopeagent/tracer"
@@ -176,6 +178,20 @@ func WithRecorders(recorders ...tracer.SpanRecorder) Option {
176178
}
177179
}
178180

181+
func WithGlobalPanicHandler() Option {
182+
return func(agent *Agent) {
183+
reflection.AddPanicHandler(func(e interface{}) {
184+
instrumentation.Logger().Printf("Panic handler triggered by: %v.\nFlushing agent, sending partial results...", scopeError.GetCurrentError(e).ErrorStack())
185+
agent.Flush()
186+
})
187+
reflection.AddOnPanicExitHandler(func(e interface{}) {
188+
instrumentation.Logger().Printf("Process is going to end by: %v,\nStopping agent...", scopeError.GetCurrentError(e).ErrorStack())
189+
scopetesting.PanicAllRunningTests(e, 3)
190+
agent.Stop()
191+
})
192+
}
193+
}
194+
179195
// Creates a new Scope Agent instance
180196
func NewAgent(options ...Option) (*Agent, error) {
181197
agent := new(Agent)

autoinstrument/init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/undefinedlabs/go-mpatch"
99

1010
"go.undefinedlabs.com/scopeagent"
11+
"go.undefinedlabs.com/scopeagent/agent"
1112
"go.undefinedlabs.com/scopeagent/instrumentation"
1213
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
1314
)
@@ -35,7 +36,7 @@ func init() {
3536
}()
3637
scopetesting.PatchTestingLogger()
3738
defer scopetesting.UnpatchTestingLogger()
38-
return scopeagent.Run(m)
39+
return scopeagent.Run(m, agent.WithGlobalPanicHandler())
3940
})
4041
logOnError(err)
4142
})

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ require (
1313
github.com/opentracing/basictracer-go v1.0.0
1414
github.com/opentracing/opentracing-go v1.1.0
1515
github.com/stretchr/testify v1.5.1
16-
github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98
16+
github.com/undefinedlabs/go-mpatch v0.0.0-20200326085307-1a86426f42a6
1717
github.com/vmihailenco/msgpack v4.0.4+incompatible
18+
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
1819
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
1920
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
21+
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
2022
google.golang.org/appengine v1.6.5 // indirect
2123
google.golang.org/grpc v1.27.1
2224
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2325
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
26+
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
2427
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
4545
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
4646
github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98 h1:Z/megzdoMbuZ0H/oLmfTw92PAEfyTi1DkvAr8AUzFgw=
4747
github.com/undefinedlabs/go-mpatch v0.0.0-20200122175732-0044123dbb98/go.mod h1:TyJZDQ/5AgyN7FSLiBJ8RO9u2c6wbtRvK827b6AVqY4=
48+
github.com/undefinedlabs/go-mpatch v0.0.0-20200326085307-1a86426f42a6 h1:VO1oVFjnL0fwOlwLpDqY1xhY/cfr0Ycz4aLwWM76D6k=
49+
github.com/undefinedlabs/go-mpatch v0.0.0-20200326085307-1a86426f42a6/go.mod h1:TyJZDQ/5AgyN7FSLiBJ8RO9u2c6wbtRvK827b6AVqY4=
4850
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
4951
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
5052
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

init.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import (
1111
"testing"
1212

1313
"go.undefinedlabs.com/scopeagent/agent"
14-
"go.undefinedlabs.com/scopeagent/errors"
1514
"go.undefinedlabs.com/scopeagent/instrumentation"
1615
"go.undefinedlabs.com/scopeagent/instrumentation/logging"
1716
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
18-
"go.undefinedlabs.com/scopeagent/reflection"
1917
)
2018

2119
var (
@@ -52,15 +50,6 @@ func Run(m *testing.M, opts ...agent.Option) int {
5250
newAgent.Stop()
5351
os.Exit(1)
5452
}()
55-
reflection.AddPanicHandler(func(e interface{}) {
56-
instrumentation.Logger().Printf("Panic handler triggered by: %v.\nFlushing agent, sending partial results...", errors.GetCurrentError(e).ErrorStack())
57-
newAgent.Flush()
58-
})
59-
reflection.AddOnPanicExitHandler(func(e interface{}) {
60-
instrumentation.Logger().Printf("Process is going to end by: %v,\nStopping agent...", errors.GetCurrentError(e).ErrorStack())
61-
scopetesting.PanicAllRunningTests(e, 3)
62-
newAgent.Stop()
63-
})
6453

6554
defaultAgent = newAgent
6655
return newAgent.Run(m)

0 commit comments

Comments
 (0)