Skip to content

Commit

Permalink
Add source line tags to test suites (#7964)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mohedano authored Nov 18, 2024
1 parent 8990f3f commit c4bc3cc
Show file tree
Hide file tree
Showing 33 changed files with 445 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import datadog.trace.civisibility.git.GitClientGitInfoBuilder;
import datadog.trace.civisibility.git.tree.GitClient;
import datadog.trace.civisibility.ipc.SignalClient;
import datadog.trace.civisibility.source.BestEffortMethodLinesResolver;
import datadog.trace.civisibility.source.ByteCodeMethodLinesResolver;
import datadog.trace.civisibility.source.CompilerAidedMethodLinesResolver;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.BestEffortLinesResolver;
import datadog.trace.civisibility.source.ByteCodeLinesResolver;
import datadog.trace.civisibility.source.CompilerAidedLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.index.*;
import java.lang.reflect.Type;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class CiVisibilityServices {
final CIProviderInfoFactory ciProviderInfoFactory;
final GitClient.Factory gitClientFactory;
final GitInfoProvider gitInfoProvider;
final MethodLinesResolver methodLinesResolver;
final LinesResolver linesResolver;
final RepoIndexProvider.Factory repoIndexProviderFactory;
@Nullable final SignalClient.Factory signalClientFactory;

Expand All @@ -82,9 +82,8 @@ public class CiVisibilityServices {

CiEnvironment environment = buildCiEnvironment(config, sco);
this.ciProviderInfoFactory = new CIProviderInfoFactory(config, environment);
this.methodLinesResolver =
new BestEffortMethodLinesResolver(
new CompilerAidedMethodLinesResolver(), new ByteCodeMethodLinesResolver());
this.linesResolver =
new BestEffortLinesResolver(new CompilerAidedLinesResolver(), new ByteCodeLinesResolver());

this.gitInfoProvider = gitInfoProvider;
gitInfoProvider.registerGitInfoBuilder(new CIProviderGitInfoBuilder(config, environment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private static BuildSystemSession.Factory buildSystemSessionFactory(
testDecorator,
repoServices.sourcePathResolver,
repoServices.codeowners,
services.methodLinesResolver,
services.linesResolver,
repoServices.executionSettingsFactory,
signalServer,
repoServices.repoIndexProvider,
Expand Down Expand Up @@ -244,7 +244,7 @@ private static TestFrameworkSession.Factory childTestFrameworkSessionFactory(
testDecorator,
repoServices.sourcePathResolver,
repoServices.codeowners,
services.methodLinesResolver,
services.linesResolver,
coverageServices.coverageStoreFactory,
coverageServices.coverageReporter,
services.signalClientFactory,
Expand Down Expand Up @@ -275,7 +275,7 @@ private static TestFrameworkSession.Factory headlessTestFrameworkEssionFactory(
testDecorator,
repoServices.sourcePathResolver,
repoServices.codeowners,
services.methodLinesResolver,
services.linesResolver,
coverageServices.coverageStoreFactory,
executionStrategy);
};
Expand Down Expand Up @@ -303,7 +303,7 @@ private static CIVisibility.SessionFactory manualApiSessionFactory(
testDecorator,
repoServices.sourcePathResolver,
repoServices.codeowners,
services.methodLinesResolver,
services.linesResolver,
coverageServices.coverageStoreFactory);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.civisibility.codeowners.Codeowners;
import datadog.trace.civisibility.decorator.TestDecorator;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import java.util.function.Consumer;
import javax.annotation.Nullable;
Expand All @@ -26,7 +26,7 @@ public abstract class AbstractTestModule {
protected final TestDecorator testDecorator;
protected final SourcePathResolver sourcePathResolver;
protected final Codeowners codeowners;
protected final MethodLinesResolver methodLinesResolver;
protected final LinesResolver linesResolver;
private final Consumer<AgentSpan> onSpanFinish;

public AbstractTestModule(
Expand All @@ -39,15 +39,15 @@ public AbstractTestModule(
TestDecorator testDecorator,
SourcePathResolver sourcePathResolver,
Codeowners codeowners,
MethodLinesResolver methodLinesResolver,
LinesResolver linesResolver,
Consumer<AgentSpan> onSpanFinish) {
this.moduleName = moduleName;
this.config = config;
this.metricCollector = metricCollector;
this.testDecorator = testDecorator;
this.sourcePathResolver = sourcePathResolver;
this.codeowners = codeowners;
this.methodLinesResolver = methodLinesResolver;
this.linesResolver = linesResolver;
this.onSpanFinish = onSpanFinish;

AgentTracer.SpanBuilder spanBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.civisibility.codeowners.Codeowners;
import datadog.trace.civisibility.decorator.TestDecorator;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -37,7 +37,7 @@ public abstract class AbstractTestSession {
protected final TestDecorator testDecorator;
protected final SourcePathResolver sourcePathResolver;
protected final Codeowners codeowners;
protected final MethodLinesResolver methodLinesResolver;
protected final LinesResolver linesResolver;

public AbstractTestSession(
String projectName,
Expand All @@ -49,15 +49,15 @@ public AbstractTestSession(
TestDecorator testDecorator,
SourcePathResolver sourcePathResolver,
Codeowners codeowners,
MethodLinesResolver methodLinesResolver) {
LinesResolver linesResolver) {
this.ciProvider = ciProvider;
this.instrumentationType = instrumentationType;
this.config = config;
this.metricCollector = metricCollector;
this.testDecorator = testDecorator;
this.sourcePathResolver = sourcePathResolver;
this.codeowners = codeowners;
this.methodLinesResolver = methodLinesResolver;
this.linesResolver = linesResolver;

// CI Test Cycle protocol requires session's trace ID and span ID to be the same
IdGenerationStrategy idGenerationStrategy = config.getIdGenerationStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.civisibility.codeowners.Codeowners;
import datadog.trace.civisibility.decorator.TestDecorator;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import datadog.trace.civisibility.source.SourceResolutionException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -69,7 +69,7 @@ public TestImpl(
CiVisibilityMetricCollector metricCollector,
TestDecorator testDecorator,
SourcePathResolver sourcePathResolver,
MethodLinesResolver methodLinesResolver,
LinesResolver linesResolver,
Codeowners codeowners,
CoverageStore.Factory coverageStoreFactory,
Consumer<AgentSpan> onSpanFinish) {
Expand Down Expand Up @@ -121,7 +121,7 @@ public TestImpl(

if (config.isCiVisibilitySourceDataEnabled()) {
populateSourceDataTags(
span, testClass, testMethod, sourcePathResolver, methodLinesResolver, codeowners);
span, testClass, testMethod, sourcePathResolver, linesResolver, codeowners);
}

if (itrCorrelationId != null) {
Expand All @@ -142,7 +142,7 @@ private void populateSourceDataTags(
Class<?> testClass,
Method testMethod,
SourcePathResolver sourcePathResolver,
MethodLinesResolver methodLinesResolver,
LinesResolver linesResolver,
Codeowners codeowners) {
if (testClass == null) {
return;
Expand All @@ -162,10 +162,10 @@ private void populateSourceDataTags(
span.setTag(Tags.TEST_SOURCE_FILE, sourcePath);

if (testMethod != null) {
MethodLinesResolver.MethodLines testMethodLines = methodLinesResolver.getLines(testMethod);
LinesResolver.Lines testMethodLines = linesResolver.getMethodLines(testMethod);
if (testMethodLines.isValid()) {
span.setTag(Tags.TEST_SOURCE_START, testMethodLines.getStartLineNumber());
span.setTag(Tags.TEST_SOURCE_END, testMethodLines.getFinishLineNumber());
span.setTag(Tags.TEST_SOURCE_END, testMethodLines.getEndLineNumber());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.civisibility.codeowners.Codeowners;
import datadog.trace.civisibility.decorator.TestDecorator;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import datadog.trace.civisibility.source.SourceResolutionException;
import datadog.trace.civisibility.utils.SpanUtils;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class TestSuiteImpl implements DDTestSuite {
private final TestDecorator testDecorator;
private final SourcePathResolver sourcePathResolver;
private final Codeowners codeowners;
private final MethodLinesResolver methodLinesResolver;
private final LinesResolver linesResolver;
private final CoverageStore.Factory coverageStoreFactory;
private final boolean parallelized;
private final Consumer<AgentSpan> onSpanFinish;
Expand All @@ -66,7 +66,7 @@ public TestSuiteImpl(
TestDecorator testDecorator,
SourcePathResolver sourcePathResolver,
Codeowners codeowners,
MethodLinesResolver methodLinesResolver,
LinesResolver linesResolver,
CoverageStore.Factory coverageStoreFactory,
Consumer<AgentSpan> onSpanFinish) {
this.moduleSpanContext = moduleSpanContext;
Expand All @@ -81,7 +81,7 @@ public TestSuiteImpl(
this.testDecorator = testDecorator;
this.sourcePathResolver = sourcePathResolver;
this.codeowners = codeowners;
this.methodLinesResolver = methodLinesResolver;
this.linesResolver = linesResolver;
this.coverageStoreFactory = coverageStoreFactory;
this.onSpanFinish = onSpanFinish;

Expand Down Expand Up @@ -154,6 +154,12 @@ private void populateSourceDataTags(

span.setTag(Tags.TEST_SOURCE_FILE, sourcePath);

LinesResolver.Lines testClassLines = linesResolver.getClassLines(testClass);
if (testClassLines.isValid()) {
span.setTag(Tags.TEST_SOURCE_START, testClassLines.getStartLineNumber());
span.setTag(Tags.TEST_SOURCE_END, testClassLines.getEndLineNumber());
}

Collection<String> testCodeOwners = codeowners.getOwners(sourcePath);
if (testCodeOwners != null) {
span.setTag(Tags.TEST_CODEOWNERS, toJson(testCodeOwners));
Expand Down Expand Up @@ -245,7 +251,7 @@ public TestImpl testStart(
metricCollector,
testDecorator,
sourcePathResolver,
methodLinesResolver,
linesResolver,
codeowners,
coverageStoreFactory,
SpanUtils.propagateCiVisibilityTagsTo(span));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import datadog.trace.civisibility.ipc.ModuleExecutionResult;
import datadog.trace.civisibility.ipc.SignalResponse;
import datadog.trace.civisibility.ipc.SignalType;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import datadog.trace.civisibility.utils.SpanUtils;
import datadog.trace.util.Strings;
Expand Down Expand Up @@ -62,7 +62,7 @@ public <T extends CoverageCalculator> BuildSystemModuleImpl(
TestDecorator testDecorator,
SourcePathResolver sourcePathResolver,
Codeowners codeowners,
MethodLinesResolver methodLinesResolver,
LinesResolver linesResolver,
ModuleSignalRouter moduleSignalRouter,
CoverageCalculator.Factory<T> coverageCalculatorFactory,
T sessionCoverageCalculator,
Expand All @@ -79,7 +79,7 @@ public <T extends CoverageCalculator> BuildSystemModuleImpl(
testDecorator,
sourcePathResolver,
codeowners,
methodLinesResolver,
linesResolver,
onSpanFinish);
this.coverageCalculator =
coverageCalculatorFactory.moduleCoverage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import datadog.trace.civisibility.ipc.SignalResponse;
import datadog.trace.civisibility.ipc.SignalServer;
import datadog.trace.civisibility.ipc.SignalType;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import datadog.trace.civisibility.source.index.RepoIndex;
import datadog.trace.civisibility.source.index.RepoIndexProvider;
Expand Down Expand Up @@ -67,7 +67,7 @@ public BuildSystemSessionImpl(
TestDecorator testDecorator,
SourcePathResolver sourcePathResolver,
Codeowners codeowners,
MethodLinesResolver methodLinesResolver,
LinesResolver linesResolver,
ExecutionSettingsFactory executionSettingsFactory,
SignalServer signalServer,
RepoIndexProvider repoIndexProvider,
Expand All @@ -82,7 +82,7 @@ public BuildSystemSessionImpl(
testDecorator,
sourcePathResolver,
codeowners,
methodLinesResolver);
linesResolver);
this.startCommand = startCommand;
this.moduleSignalRouter = moduleSignalRouter;
this.executionSettingsFactory = executionSettingsFactory;
Expand Down Expand Up @@ -167,7 +167,7 @@ public BuildSystemModuleImpl testModuleStart(
testDecorator,
sourcePathResolver,
codeowners,
methodLinesResolver,
linesResolver,
moduleSignalRouter,
coverageCalculatorFactory,
coverageCalculator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import datadog.trace.civisibility.ipc.ModuleSignal;
import datadog.trace.civisibility.ipc.SignalClient;
import datadog.trace.civisibility.ipc.TestFramework;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.LinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import datadog.trace.civisibility.test.ExecutionStrategy;
import java.util.Collection;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class ProxyTestModule implements TestFrameworkModule {
private final TestDecorator testDecorator;
private final SourcePathResolver sourcePathResolver;
private final Codeowners codeowners;
private final MethodLinesResolver methodLinesResolver;
private final LinesResolver linesResolver;
private final CoverageStore.Factory coverageStoreFactory;
private final Collection<TestFramework> testFrameworks = ConcurrentHashMap.newKeySet();

Expand All @@ -64,7 +64,7 @@ public ProxyTestModule(
TestDecorator testDecorator,
SourcePathResolver sourcePathResolver,
Codeowners codeowners,
MethodLinesResolver methodLinesResolver,
LinesResolver linesResolver,
CoverageStore.Factory coverageStoreFactory,
ChildProcessCoverageReporter childProcessCoverageReporter,
SignalClient.Factory signalClientFactory) {
Expand All @@ -78,7 +78,7 @@ public ProxyTestModule(
this.testDecorator = testDecorator;
this.sourcePathResolver = sourcePathResolver;
this.codeowners = codeowners;
this.methodLinesResolver = methodLinesResolver;
this.linesResolver = linesResolver;
this.coverageStoreFactory = coverageStoreFactory;
}

Expand Down Expand Up @@ -171,7 +171,7 @@ public TestSuiteImpl testSuiteStart(
testDecorator,
sourcePathResolver,
codeowners,
methodLinesResolver,
linesResolver,
coverageStoreFactory,
this::propagateTestFrameworkData);
}
Expand Down
Loading

0 comments on commit c4bc3cc

Please sign in to comment.