go.undefinedlabs.com/scopeagent@v0.4.2/autoinstrument/init.go (about) 1 package autoinstrument 2 3 import ( 4 "reflect" 5 "sync" 6 "testing" 7 8 "github.com/undefinedlabs/go-mpatch" 9 10 "go.undefinedlabs.com/scopeagent" 11 "go.undefinedlabs.com/scopeagent/agent" 12 "go.undefinedlabs.com/scopeagent/env" 13 "go.undefinedlabs.com/scopeagent/instrumentation" 14 scopegocheck "go.undefinedlabs.com/scopeagent/instrumentation/gocheck" 15 scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing" 16 ) 17 18 var ( 19 once sync.Once 20 ) 21 22 func init() { 23 once.Do(func() { 24 var m *testing.M 25 var mRunMethod reflect.Method 26 var ok bool 27 mType := reflect.TypeOf(m) 28 if mRunMethod, ok = mType.MethodByName("Run"); !ok { 29 return 30 } 31 32 var runPatch *mpatch.Patch 33 var err error 34 runPatch, err = mpatch.PatchMethodByReflect(mRunMethod, func(m *testing.M) int { 35 logOnError(runPatch.Unpatch()) 36 defer func() { 37 logOnError(runPatch.Patch()) 38 }() 39 if env.ScopeInstrumentationTestingLogger.Value { 40 scopetesting.PatchTestingLogger() 41 defer scopetesting.UnpatchTestingLogger() 42 } 43 return scopeagent.Run(m, agent.WithGlobalPanicHandler()) 44 }) 45 logOnError(err) 46 47 scopegocheck.Init() 48 }) 49 } 50 51 func logOnError(err error) { 52 if err != nil { 53 instrumentation.Logger().Println(err) 54 } 55 }