Skip to content

Commit

Permalink
Merge branch 'master' into bbujon/sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
ygree committed Nov 30, 2023
2 parents ff7d0c3 + f6f3414 commit 5b0eeae
Show file tree
Hide file tree
Showing 193 changed files with 6,622 additions and 1,860 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -853,15 +853,29 @@ public void withTracer(TracerAPI tracer) {
* on JFR.
*/
private static ProfilingContextIntegration createProfilingContextIntegration() {
if (Config.get().isProfilingEnabled() && !Platform.isWindows()) {
try {
return (ProfilingContextIntegration)
AGENT_CLASSLOADER
.loadClass("com.datadog.profiling.ddprof.DatadogProfilingIntegration")
.getDeclaredConstructor()
.newInstance();
} catch (Throwable t) {
log.debug("Profiling context labeling not available. {}", t.getMessage());
if (Config.get().isProfilingEnabled()) {
if (Config.get().isDatadogProfilerEnabled() && !Platform.isWindows()) {
try {
return (ProfilingContextIntegration)
AGENT_CLASSLOADER
.loadClass("com.datadog.profiling.ddprof.DatadogProfilingIntegration")
.getDeclaredConstructor()
.newInstance();
} catch (Throwable t) {
log.debug("ddprof-based profiling context labeling not available. {}", t.getMessage());
}
}
if (Config.get().isProfilingTimelineEventsEnabled()) {
// important: note that this will not initialise JFR until onStart is called
try {
return (ProfilingContextIntegration)
AGENT_CLASSLOADER
.loadClass("com.datadog.profiling.controller.openjdk.JFREventContextIntegration")
.getDeclaredConstructor()
.newInstance();
} catch (Throwable t) {
log.debug("JFR event-based profiling context labeling not available. {}", t.getMessage());
}
}
}
return ProfilingContextIntegration.NoOp.INSTANCE;
Expand Down Expand Up @@ -922,6 +936,7 @@ public void withTracer(TracerAPI tracer) {
.getDeclaredConstructor()
.newInstance());
}
tracer.getProfilingContext().onStart();
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
e = e.getCause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@
import datadog.trace.api.DDTags;
import datadog.trace.api.UserEventTrackingMode;
import datadog.trace.api.internal.TraceSegment;
import datadog.trace.bootstrap.ActiveSubsystems;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import java.util.Map;
import javax.annotation.Nonnull;

public class AppSecUserEventDecorator {

public boolean isEnabled() {
if (!ActiveSubsystems.APPSEC_ACTIVE) {
return false;
}
UserEventTrackingMode mode = Config.get().getAppSecUserEventsTrackingMode();
if (mode == DISABLED) {
return false;
}
return true;
}

public void onUserNotFound() {
if (!isEnabled()) {
return;
}
TraceSegment segment = getSegment();
if (segment == null) {
return;
}
segment.setTagTop("appsec.events.users.login.failure.usr.exists", false);
}

public void onLoginSuccess(String userId, Map<String, String> metadata) {
TraceSegment segment = getSegment();
if (segment == null) {
Expand All @@ -24,7 +47,7 @@ public void onLoginSuccess(String userId, Map<String, String> metadata) {
onEvent(segment, "users.login.success", metadata);
}

public void onLoginFailure(String userId, Map<String, String> metadata, boolean userExists) {
public void onLoginFailure(String userId, Map<String, String> metadata) {
TraceSegment segment = getSegment();
if (segment == null) {
return;
Expand All @@ -33,7 +56,7 @@ public void onLoginFailure(String userId, Map<String, String> metadata, boolean
if (userId != null) {
segment.setTagTop("appsec.events.users.login.failure.usr.id", userId);
}
segment.setTagTop("appsec.events.users.login.failure.usr.exists", userExists);

onEvent(segment, "users.login.failure", metadata);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.trace.bootstrap.instrumentation.decorator

import datadog.trace.api.internal.TraceSegment
import datadog.trace.bootstrap.ActiveSubsystems
import datadog.trace.test.util.DDSpecification

import static datadog.trace.api.config.AppSecConfig.APPSEC_AUTOMATED_USER_EVENTS_TRACKING
Expand All @@ -9,6 +10,17 @@ class AppSecUserEventDecoratorTest extends DDSpecification {

def traceSegment = Mock(TraceSegment)

Boolean appsecActiveOriginal = null

def setup() {
appsecActiveOriginal = ActiveSubsystems.APPSEC_ACTIVE
ActiveSubsystems.APPSEC_ACTIVE = true
}

def cleanup() {
ActiveSubsystems.APPSEC_ACTIVE = appsecActiveOriginal
}

def "test onSignup [#mode]"() {
setup:
injectSysConfig(APPSEC_AUTOMATED_USER_EVENTS_TRACKING, mode)
Expand Down Expand Up @@ -59,23 +71,62 @@ class AppSecUserEventDecoratorTest extends DDSpecification {
def decorator = newDecorator()

when:
decorator.onLoginFailure('user', ['key1': 'value1', 'key2': 'value2'], (boolean)userExists)
decorator.onLoginFailure('user', ['key1': 'value1', 'key2': 'value2'])

then:
1 * traceSegment.setTagTop('_dd.appsec.events.users.login.failure.auto.mode', modeTag)
1 * traceSegment.setTagTop('appsec.events.users.login.failure.track', true, true)
1 * traceSegment.setTagTop('manual.keep', true)
1 * traceSegment.setTagTop('appsec.events.users.login.failure.usr.id', 'user')
1 * traceSegment.setTagTop("appsec.events.users.login.failure.usr.exists", userExists)
1 * traceSegment.setTagTop('appsec.events.users.login.failure', ['key1':'value1', 'key2':'value2'])
0 * _

where:
mode | modeTag | userExists | description
'safe' | 'SAFE' | true | 'with existing user'
'safe' | 'SAFE' | false | 'user doesn\'t exist'
'extended' | 'EXTENDED' | true | 'with existing user'
'extended' | 'EXTENDED' | false | 'user doesn\'t exist'
mode | modeTag | description
'safe' | 'SAFE' | 'with existing user'
'safe' | 'SAFE' | 'user doesn\'t exist'
'extended' | 'EXTENDED' | 'with existing user'
'extended' | 'EXTENDED' | 'user doesn\'t exist'
}

def "test onUserNotFound [#mode]"() {
setup:
injectSysConfig(APPSEC_AUTOMATED_USER_EVENTS_TRACKING, mode)
def decorator = newDecorator()

when:
decorator.onUserNotFound()

then:
1 * traceSegment.setTagTop("appsec.events.users.login.failure.usr.exists", false)
0 * _

where:
mode | modeTag
'safe' | 'SAFE'
'extended' | 'EXTENDED'
}

def "test isEnabled (appsec = #appsec, mode = #mode)"() {
setup:
ActiveSubsystems.APPSEC_ACTIVE = appsec
injectSysConfig(APPSEC_AUTOMATED_USER_EVENTS_TRACKING, mode)
def decorator = newDecorator()
when:
def enabled = decorator.isEnabled()
then:
enabled == result
where:
appsec | mode | result
false | "disabled" | false
false | "safe" | false
false | "extended" | false
true | "disabled" | false
true | "safe" | true
true | "extended" | true
}
def newDecorator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ private static DDBuildSystemSession.Factory buildSystemSessionFactory(
new ConventionBasedResourceResolver(
fileSystem, config.getCiVisibilityResourceFolderNames());
RepoIndexBuilder indexBuilder =
new RepoIndexBuilder(
projectRoot.toString(), packageResolver, resourceResolver, fileSystem);
new RepoIndexBuilder(repoRoot, packageResolver, resourceResolver, fileSystem);

SourcePathResolver sourcePathResolver = getSourcePathResolver(repoRoot, indexBuilder);
Codeowners codeowners = getCodeowners(repoRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import datadog.trace.civisibility.coverage.CoverageUtils;
import datadog.trace.civisibility.decorator.TestDecorator;
import datadog.trace.civisibility.ipc.ModuleExecutionResult;
import datadog.trace.civisibility.ipc.TestFramework;
import datadog.trace.civisibility.source.MethodLinesResolver;
import datadog.trace.civisibility.source.SourcePathResolver;
import datadog.trace.civisibility.source.index.RepoIndexProvider;
Expand Down Expand Up @@ -141,10 +140,7 @@ public void onModuleExecutionResultReceived(ModuleExecutionResult result) {
}
}

for (TestFramework testFramework : result.getTestFrameworks()) {
SpanUtils.mergeTag(span, Tags.TEST_FRAMEWORK, testFramework.getName());
SpanUtils.mergeTag(span, Tags.TEST_FRAMEWORK_VERSION, testFramework.getVersion());
}
SpanUtils.mergeTestFrameworks(span, result.getTestFrameworks());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -132,7 +133,7 @@ private void sendModuleExecutionResult() {
coverageEnabled,
itrEnabled,
testsSkippedTotal,
testFrameworks,
new TreeSet<>(testFrameworks),
coverageData);

try (SignalClient signalClient = new SignalClient(signalServerAddress)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ private boolean isItrEnabled(CiVisibilitySettings ciVisibilitySettings) {
}

private boolean isCodeCoverageEnabled(CiVisibilitySettings ciVisibilitySettings) {
return ciVisibilitySettings.isCodeCoverageEnabled()
&& config.isCiVisibilityCodeCoverageEnabled();
return config.isCiVisibilityCodeCoverageEnabled()
&& (ciVisibilitySettings.isCodeCoverageEnabled() // coverage enabled via backend settings
|| config
.isCiVisibilityJacocoPluginVersionProvided() // coverage enabled via tracer settings
);
}

private Map<String, String> getPropertiesPropagatedToChildProcess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

public final class TestFramework {
public final class TestFramework implements Comparable<TestFramework> {
private final String name;
private final String version;

Expand Down Expand Up @@ -35,4 +35,10 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(name, version);
}

@Override
public int compareTo(TestFramework o) {
int nameComparison = name.compareTo(o.name);
return nameComparison != 0 ? nameComparison : version.compareTo(o.version);
}
}
Loading

0 comments on commit 5b0eeae

Please sign in to comment.