Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package datadog.trace.civisibility.domain;

import datadog.context.Context;
import datadog.context.ContextHandle;

/** Keeps nested test and suite contexts restorable when they end out of order. */
final class ContextActivation {
private static final ThreadLocal<ContextActivation> CURRENT = new ThreadLocal<>();

private ContextActivation parent;
private ContextHandle previousContext;
private boolean ended;

private ContextActivation(Context context) {
parent = CURRENT.get();
previousContext = context.swap();
CURRENT.set(this);
}

static ContextActivation enter(Context context) {
return new ContextActivation(context);
}

boolean isCurrent() {
return CURRENT.get() == this;
}

void exit() {
ended = true;
if (!isCurrent()) {
return;
}

ContextHandle restoreContext;
ContextActivation activation = this;
do {
restoreContext = activation.previousContext;
ContextActivation parent = activation.parent;
activation.previousContext = null;
activation.parent = null;
activation = parent;
} while (activation != null && activation.ended);

if (activation == null) {
CURRENT.remove();
} else {
CURRENT.set(activation);
}
if (restoreContext != null) {
restoreContext.swap();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package datadog.trace.civisibility.domain;

import static datadog.json.JsonMapper.toJson;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpanWithoutScope;
import static datadog.trace.civisibility.Constants.CI_VISIBILITY_INSTRUMENTATION_NAME;

import datadog.trace.api.Config;
import datadog.trace.api.DDTraceId;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.api.civisibility.DDTest;
import datadog.trace.api.civisibility.InstrumentationTestBridge;
Expand Down Expand Up @@ -63,6 +63,8 @@ public class TestImpl implements DDTest {
private final ExecutionResults executionResults;
private final TestFrameworkInstrumentation instrumentation;
private final AgentSpan span;
private final ContextActivation contextActivation;
private boolean ended;
private final DDTraceId sessionId;
private final long suiteId;
private final Consumer<AgentSpan> onSpanFinish;
Expand Down Expand Up @@ -125,7 +127,12 @@ public TestImpl(

span = spanBuilder.start();

activateSpanWithoutScope(span);
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
AgentTracer.get().activateSpanWithoutScope(span);
contextActivation = null;
} else {
contextActivation = ContextActivation.enter(span);
}

span.setSpanType(InternalSpanTypes.TEST);
span.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_TEST);
Expand Down Expand Up @@ -256,6 +263,11 @@ public TestContext getContext() {

@Override
public void end(@Nullable Long endTime) {
if (!InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
endWithContext(endTime);
return;
}

closeOutstandingSpans();

final AgentSpan activeSpan = AgentTracer.activeSpan();
Expand All @@ -277,9 +289,42 @@ public void end(@Nullable Long endTime) {
}

InstrumentationTestBridge.fireBeforeTestEnd(context);

CoveragePerTestBridge.removeThreadLocalCoverageProbes();
reportCoverage();
AgentTracer.get().closeActive();
finishTest(endTime);
}

/**
* Finishes the test and restores its saved context without finishing outstanding application
* spans.
*/
private void endWithContext(@Nullable Long endTime) {
if (ended) {
return;
}
ended = true;
boolean cleanupCurrentContext = contextActivation.isCurrent();
try {
if (cleanupCurrentContext) {
span.swap();
// Listeners such as Selenium cleanup require this test to be current.
try {
InstrumentationTestBridge.fireBeforeTestEnd(context);
} finally {
if (contextActivation.isCurrent()) {
CoveragePerTestBridge.removeThreadLocalCoverageProbes();
}
}
}
reportCoverage();
} finally {
contextActivation.exit();
finishTest(endTime);
}
}

private void reportCoverage() {
// do not process coverage reports for skipped tests
if (span.getTag(Tags.TEST_STATUS) != TestStatus.skip) {
CoverageStore coverageStore = context.getCoverageStore();
Expand All @@ -289,14 +334,14 @@ public void end(@Nullable Long endTime) {
metricCollector.add(CiVisibilityCountMetric.CODE_COVERAGE_IS_EMPTY, 1);
}
}
}

private void finishTest(@Nullable Long endTime) {
boolean debugInfoCaptured = span.getTag(Tags.ERROR_DEBUG_INFO_CAPTURED) != null;
if (debugInfoCaptured) {
executionResults.setHasFailedTestReplayTests();
}

AgentTracer.closeActive();

onSpanFinish.accept(span);

if (endTime != null) {
Expand Down Expand Up @@ -340,8 +385,8 @@ public void end(@Nullable Long endTime) {
* finalizing an operation properly usually results in its span remaining open. This is something
* that we have no control over, since this happens in the clients' codebase.
*
* <p>This method attempts to finalize such "dangling" spans: it closes whatever is on top of the
* spans stack until it encounters a CI Visibility span or the stack is empty.
* <p>In legacy mode, this method attempts to finalize such "dangling" spans: it closes whatever
* is on top of the spans stack until it encounters a CI Visibility span or the stack is empty.
*/
private void closeOutstandingSpans() {
AgentSpan activeSpan;
Expand All @@ -353,7 +398,7 @@ private void closeOutstandingSpans() {
}

log.debug("Closing outstanding span: {}", activeSpan);
AgentTracer.closeActive();
AgentTracer.get().closeActive();
activeSpan.finish();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package datadog.trace.civisibility.domain;

import static datadog.json.JsonMapper.toJson;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpanWithoutScope;
import static datadog.trace.civisibility.Constants.CI_VISIBILITY_INSTRUMENTATION_NAME;

import datadog.trace.api.Config;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.civisibility.DDTestSuite;
import datadog.trace.api.civisibility.config.LibraryCapability;
import datadog.trace.api.civisibility.coverage.CoverageStore;
Expand Down Expand Up @@ -38,6 +38,8 @@ public class TestSuiteImpl implements DDTestSuite {

private final AgentSpanContext moduleSpanContext;
private final AgentSpan span;
private final ContextActivation contextActivation;
private boolean ended;
private final String moduleName;
private final String testSuiteName;
private final String itrCorrelationId;
Expand Down Expand Up @@ -144,7 +146,14 @@ public TestSuiteImpl(
configurationErrors.applyTags(span);

if (!parallelized) {
activateSpanWithoutScope(span);
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
AgentTracer.get().activateSpanWithoutScope(span);
contextActivation = null;
} else {
contextActivation = ContextActivation.enter(span);
}
} else {
contextActivation = null;
}

metricCollector.add(CiVisibilityCountMetric.EVENT_CREATED, 1, instrumentation, EventType.SUITE);
Expand Down Expand Up @@ -206,26 +215,36 @@ public void setSkipReason(String skipReason) {

@Override
public void end(@Nullable Long endTime) {
if (!parallelized) {
final AgentSpan activeSpan = AgentTracer.activeSpan();
if (activeSpan == null) {
throw new IllegalStateException(
"No active span present, it is possible that end() was called multiple times");
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
if (!parallelized) {
final AgentSpan activeSpan = AgentTracer.activeSpan();
if (activeSpan == null) {
throw new IllegalStateException(
"No active span present, it is possible that end() was called multiple times");
}

if (activeSpan != this.span) {
throw new IllegalStateException(
"Active span does not correspond to the finished suite, "
+ "it is possible that end() was called multiple times "
+ "or an operation that was started by the suite is still in progress; "
+ "active span is: "
+ activeSpan
+ "; "
+ "expected span is: "
+ this.span);
}

AgentTracer.get().closeActive();
}

if (activeSpan != this.span) {
throw new IllegalStateException(
"Active span does not correspond to the finished suite, "
+ "it is possible that end() was called multiple times "
+ "or an operation that was started by the suite is still in progress; "
+ "active span is: "
+ activeSpan
+ "; "
+ "expected span is: "
+ this.span);
} else {
if (ended) {
return;
}
ended = true;
if (contextActivation != null) {
contextActivation.exit();
}

AgentTracer.closeActive();
}

onSpanFinish.accept(span);
Expand Down
Loading
Loading