diff --git a/instrumentation/gocheck/types.go b/instrumentation/gocheck/types.go index 9eac1567..daba99be 100644 --- a/instrumentation/gocheck/types.go +++ b/instrumentation/gocheck/types.go @@ -337,7 +337,6 @@ func isTestCached(c *chk.C) bool { cachedMap := config.GetCachedTestsMap() if _, ok := cachedMap[fqn]; ok { instrumentation.Logger().Printf("Test '%v' is cached.", fqn) - fmt.Print("[SCOPE CACHED] ") return true } instrumentation.Logger().Printf("Test '%v' is not cached.", fqn) diff --git a/instrumentation/testing/config/testing.go b/instrumentation/testing/config/testing.go index 46e974b7..4700a41e 100644 --- a/instrumentation/testing/config/testing.go +++ b/instrumentation/testing/config/testing.go @@ -6,14 +6,21 @@ import ( "sync" ) +type ( + TestDescription struct { + Suite string + Name string + } +) + var ( - testsToSkip map[string]struct{} + testsToSkip map[string]TestDescription m sync.Mutex ) // Gets the map of cached tests -func GetCachedTestsMap() map[string]struct{} { +func GetCachedTestsMap() map[string]TestDescription { m.Lock() defer m.Unlock() @@ -22,14 +29,19 @@ func GetCachedTestsMap() map[string]struct{} { } config := instrumentation.GetRemoteConfiguration() - testsToSkip = map[string]struct{}{} + testsToSkip = map[string]TestDescription{} if config != nil { if iCached, ok := config["cached"]; ok { cachedTests := iCached.([]interface{}) for _, item := range cachedTests { testItem := item.(map[string]interface{}) - testFqn := fmt.Sprintf("%v.%v", testItem["test_suite"], testItem["test_name"]) - testsToSkip[testFqn] = struct{}{} + suite := fmt.Sprint(testItem["test_suite"]) + name := fmt.Sprint(testItem["test_name"]) + testFqn := fmt.Sprintf("%s.%s", suite, name) + testsToSkip[testFqn] = TestDescription{ + Suite: suite, + Name: name, + } } } } diff --git a/instrumentation/testing/testing.go b/instrumentation/testing/testing.go index 0383c3cd..f320d4ff 100644 --- a/instrumentation/testing/testing.go +++ b/instrumentation/testing/testing.go @@ -63,31 +63,30 @@ func StartTest(t *testing.T, opts ...Option) *Test { func StartTestFromCaller(t *testing.T, pc uintptr, opts ...Option) *Test { // check if the test is cached - if isTestCached(t, pc) { - - test := &Test{t: t, ctx: context.Background()} - for _, opt := range opts { - opt(test) - } - - // Extracting the testing func name (by removing any possible sub-test suffix `{test_func}/{sub_test}`) - // to search the func source code bounds and to calculate the package name. - fullTestName := runner.GetOriginalTestName(t.Name()) - pName, _ := instrumentation.GetPackageAndName(pc) - - testTags := opentracing.Tags{ - "span.kind": "test", - "test.name": fullTestName, - "test.suite": pName, - "test.framework": "testing", - "test.language": "go", + if ok, testsDescription := isTestCached(t, pc); ok { + ctx := context.Background() + tracer := instrumentation.Tracer() + for _, desc := range testsDescription { + startTime := time.Now() + options := []opentracing.StartSpanOption{ + opentracing.Tags{ + "span.kind": "test", + "test.name": desc.Name, + "test.suite": desc.Suite, + "test.framework": "testing", + "test.language": "go", + }, + opentracing.StartTime(startTime), + } + span, _ := opentracing.StartSpanFromContextWithTracer(ctx, tracer, desc.Name, options...) + span.SetBaggageItem("trace.kind", "test") + span.SetTag("test.status", tags.TestStatus_CACHE) + span.FinishWithOptions(opentracing.FinishOptions{ + FinishTime: startTime, + }) } - span, _ := opentracing.StartSpanFromContextWithTracer(test.ctx, instrumentation.Tracer(), fullTestName, testTags) - span.SetBaggageItem("trace.kind", "test") - span.SetTag("test.status", tags.TestStatus_CACHE) - span.Finish() t.SkipNow() - return test + return nil } else { @@ -320,16 +319,22 @@ func addAutoInstrumentedTest(t *testing.T) { } // Get if the test is cached -func isTestCached(t *testing.T, pc uintptr) bool { - pkgName, testName := instrumentation.GetPackageAndName(pc) +func isTestCached(t *testing.T, pc uintptr) (bool, []config.TestDescription) { + pkgName, _ := instrumentation.GetPackageAndName(pc) + testName := runner.GetOriginalTestName(t.Name()) fqn := fmt.Sprintf("%s.%s", pkgName, testName) cachedMap := config.GetCachedTestsMap() if _, ok := cachedMap[fqn]; ok { instrumentation.Logger().Printf("Test '%v' is cached.", fqn) - fmt.Print("[SCOPE CACHED] ") + var tests []config.TestDescription + for _, v := range cachedMap { + if v.Suite == pkgName && strings.HasPrefix(v.Name, testName) { + tests = append(tests, v) + } + } reflection.SkipAndFinishTest(t) - return true + return true, tests } instrumentation.Logger().Printf("Test '%v' is not cached.", fqn) - return false + return false, nil }