Skip to content

Commit

Permalink
Merge branch 'master' into jpbempel/fix-var-hoisting-rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbempel authored Dec 31, 2024
2 parents 48e1f1e + 0dc9c08 commit 8e854bf
Show file tree
Hide file tree
Showing 474 changed files with 2,747 additions and 1,143 deletions.
2 changes: 1 addition & 1 deletion communication/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
}

ext {
minimumBranchCoverage = 0.6
minimumBranchCoverage = 0.5
minimumInstructionCoverage = 0.8
excludedClassesCoverage = [
'datadog.communication.ddagent.ExternalAgentLauncher',
Expand Down
2 changes: 1 addition & 1 deletion communication/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ com.beust:jcommander:1.78=testRuntimeClasspath
com.datadoghq.okhttp3:okhttp:3.12.15=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.datadoghq.okio:okio:1.17.6=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.datadoghq:dd-javac-plugin-client:0.2.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.datadoghq:java-dogstatsd-client:4.4.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.datadoghq:java-dogstatsd-client:4.4.3=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-annotations:2.9.0=testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-core:2.9.9=testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-databind:2.9.9.3=testCompileClasspath,testRuntimeClasspath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import datadog.remoteconfig.DefaultConfigurationPoller;
import datadog.trace.api.Config;
import datadog.trace.util.AgentTaskScheduler;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import okhttp3.HttpUrl;
Expand All @@ -21,12 +23,23 @@
public class SharedCommunicationObjects {
private static final Logger log = LoggerFactory.getLogger(SharedCommunicationObjects.class);

private final List<Runnable> pausedComponents = new ArrayList<>();
private volatile boolean paused;

public OkHttpClient okHttpClient;
public HttpUrl agentUrl;
public Monitoring monitoring;
private DDAgentFeaturesDiscovery featuresDiscovery;
private ConfigurationPoller configurationPoller;

public SharedCommunicationObjects() {
this(false);
}

public SharedCommunicationObjects(boolean paused) {
this.paused = paused;
}

public void createRemaining(Config config) {
if (monitoring == null) {
monitoring = Monitoring.DISABLED;
Expand All @@ -46,6 +59,32 @@ public void createRemaining(Config config) {
}
}

public void whenReady(Runnable callback) {
if (paused) {
synchronized (pausedComponents) {
if (paused) {
pausedComponents.add(callback);
return;
}
}
}
callback.run(); // not paused, run immediately
}

public void resume() {
paused = false;
synchronized (pausedComponents) {
for (Runnable callback : pausedComponents) {
try {
callback.run();
} catch (Throwable e) {
log.warn("Problem resuming remote component {}", callback, e);
}
}
pausedComponents.clear();
}
}

private static HttpUrl parseAgentUrl(Config config) {
String agentUrl = config.getAgentUrl();
if (agentUrl.startsWith("unix:")) {
Expand Down Expand Up @@ -100,11 +139,16 @@ public DDAgentFeaturesDiscovery featuresDiscovery(Config config) {
agentUrl,
config.isTraceAgentV05Enabled(),
config.isTracerMetricsEnabled());
if (AGENT_THREAD_GROUP.equals(Thread.currentThread().getThreadGroup())) {
featuresDiscovery.discover(); // safe to run on same thread

if (paused) {
// defer remote discovery until remote I/O is allowed
} else {
// avoid performing blocking I/O operation on application thread
AgentTaskScheduler.INSTANCE.execute(featuresDiscovery::discover);
if (AGENT_THREAD_GROUP.equals(Thread.currentThread().getThreadGroup())) {
featuresDiscovery.discover(); // safe to run on same thread
} else {
// avoid performing blocking I/O operation on application thread
AgentTaskScheduler.INSTANCE.execute(featuresDiscovery::discover);
}
}
}
return featuresDiscovery;
Expand Down
13 changes: 13 additions & 0 deletions components/context/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id("me.champeau.jmh")
}

apply(from = "$rootDir/gradle/java.gradle")

jmh {
version = "1.28"
}

val excludedClassesInstructionCoverage by extra {
listOf("datadog.context.ContextProviders") // covered by forked test
}
118 changes: 118 additions & 0 deletions components/context/gradle.lockfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
ch.qos.logback:logback-classic:1.2.3=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
ch.qos.logback:logback-core:1.2.3=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.beust:jcommander:1.78=jmhRuntimeClasspath,testRuntimeClasspath
com.github.javaparser:javaparser-core:3.25.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.github.spotbugs:spotbugs-annotations:4.2.0=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.github.spotbugs:spotbugs-annotations:4.7.3=spotbugs
com.github.spotbugs:spotbugs:4.7.3=spotbugs
com.google.code.findbugs:jsr305:3.0.2=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath
com.google.code.gson:gson:2.9.1=spotbugs
com.thoughtworks.qdox:qdox:1.12.1=jmhRuntimeClasspath,testRuntimeClasspath
commons-codec:commons-codec:1.15=spotbugs
de.thetaphi:forbiddenapis:3.1=compileClasspath,jmhCompileClasspath
info.picocli:picocli:4.6.3=jmhRuntimeClasspath,testRuntimeClasspath
jaxen:jaxen:1.2.0=spotbugs
jline:jline:2.14.6=jmhRuntimeClasspath,testRuntimeClasspath
junit:junit:4.13.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
net.jcip:jcip-annotations:1.0=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath
net.sf.jopt-simple:jopt-simple:5.0.4=jmh,jmhCompileClasspath,jmhRuntimeClasspath
net.sf.saxon:Saxon-HE:11.4=spotbugs
org.apache.ant:ant-antlr:1.10.12=jmhRuntimeClasspath,testRuntimeClasspath
org.apache.ant:ant-antlr:1.9.15=codenarc
org.apache.ant:ant-junit:1.10.12=jmhRuntimeClasspath,testRuntimeClasspath
org.apache.ant:ant-junit:1.9.15=codenarc
org.apache.ant:ant-launcher:1.10.12=jmhRuntimeClasspath,testRuntimeClasspath
org.apache.ant:ant:1.10.12=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.bcel:bcel:6.5.0=spotbugs
org.apache.commons:commons-lang3:3.12.0=pitest,spotbugs
org.apache.commons:commons-math3:3.2=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.apache.commons:commons-text:1.10.0=pitest,spotbugs
org.apache.httpcomponents.client5:httpclient5:5.1.3=spotbugs
org.apache.httpcomponents.core5:httpcore5-h2:5.1.3=spotbugs
org.apache.httpcomponents.core5:httpcore5:5.1.3=spotbugs
org.apache.logging.log4j:log4j-api:2.19.0=spotbugs
org.apache.logging.log4j:log4j-core:2.19.0=spotbugs
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
org.codehaus.groovy:groovy-all:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-ant:2.5.14=codenarc
org.codehaus.groovy:groovy-ant:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-astbuilder:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-cli-picocli:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-console:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-datetime:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-docgenerator:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-groovydoc:2.5.14=codenarc
org.codehaus.groovy:groovy-groovydoc:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-groovysh:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-jmx:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-json:2.5.14=codenarc
org.codehaus.groovy:groovy-json:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-jsr223:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-macro:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-nio:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-servlet:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-sql:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-swing:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-templates:2.5.14=codenarc
org.codehaus.groovy:groovy-templates:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-test-junit5:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-test:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-testng:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-xml:2.5.14=codenarc
org.codehaus.groovy:groovy-xml:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy:2.5.14=codenarc
org.codehaus.groovy:groovy:3.0.17=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codenarc:CodeNarc:2.2.0=codenarc
org.dom4j:dom4j:2.1.3=spotbugs
org.gmetrics:GMetrics:1.1=codenarc
org.hamcrest:hamcrest-core:1.3=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hamcrest:hamcrest:2.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.jacoco:org.jacoco.agent:0.8.5=jacocoAgent,jacocoAnt
org.jacoco:org.jacoco.ant:0.8.5=jacocoAnt
org.jacoco:org.jacoco.core:0.8.5=jacocoAnt
org.jacoco:org.jacoco.report:0.8.5=jacocoAnt
org.junit.jupiter:junit-jupiter-api:5.9.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-engine:5.9.2=jmhRuntimeClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-params:5.9.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter:5.9.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-commons:1.9.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-engine:1.9.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-launcher:1.9.2=jmhRuntimeClasspath,testRuntimeClasspath
org.junit:junit-bom:5.9.1=spotbugs
org.junit:junit-bom:5.9.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.objenesis:objenesis:3.3=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.openjdk.jmh:jmh-core:1.36=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.openjdk.jmh:jmh-generator-asm:1.36=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.openjdk.jmh:jmh-generator-bytecode:1.36=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.openjdk.jmh:jmh-generator-reflection:1.36=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.opentest4j:opentest4j:1.2.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.ow2.asm:asm-analysis:7.2=jacocoAnt
org.ow2.asm:asm-analysis:9.4=spotbugs
org.ow2.asm:asm-commons:7.2=jacocoAnt
org.ow2.asm:asm-commons:9.4=spotbugs
org.ow2.asm:asm-tree:7.2=jacocoAnt
org.ow2.asm:asm-tree:9.4=spotbugs
org.ow2.asm:asm-util:9.4=spotbugs
org.ow2.asm:asm:7.2=jacocoAnt
org.ow2.asm:asm:9.0=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.ow2.asm:asm:9.4=spotbugs
org.pitest:pitest-command-line:1.9.11=pitest
org.pitest:pitest-entry:1.9.11=pitest
org.pitest:pitest:1.9.11=pitest
org.slf4j:jcl-over-slf4j:1.7.30=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:jul-to-slf4j:1.7.30=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:log4j-over-slf4j:1.7.30=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-api:1.7.30=testCompileClasspath
org.slf4j:slf4j-api:1.7.32=jmhRuntimeClasspath,testRuntimeClasspath
org.slf4j:slf4j-api:2.0.0=spotbugs,spotbugsSlf4j
org.slf4j:slf4j-simple:2.0.0=spotbugsSlf4j
org.spockframework:spock-core:2.2-groovy-3.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.spockframework:spock-junit4:2.2-groovy-3.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.testng:testng:7.5=jmhRuntimeClasspath,testRuntimeClasspath
org.webjars:jquery:3.5.1=jmhRuntimeClasspath,testRuntimeClasspath
org.xmlresolver:xmlresolver:4.4.3=spotbugs
xml-apis:xml-apis:1.4.01=spotbugs
empty=annotationProcessor,jmhAnnotationProcessor,runtimeClasspath,spotbugsPlugins,testAnnotationProcessor
142 changes: 142 additions & 0 deletions components/context/src/main/java/datadog/context/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package datadog.context;

import static datadog.context.ContextProviders.binder;
import static datadog.context.ContextProviders.manager;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

/**
* Immutable context scoped to an execution unit or carrier object.
*
* <p>There are three ways to get a Context instance:
*
* <ul>
* <li>The first one is to retrieve the one from the current execution unit using {@link
* #current()}. A Context instance can be marked as current using {@link #attach()} within the
* execution unit.
* <li>The second one is to retrieve one from a carrier object using {@link #from(Object
* carrier)}. A Context instance would need to be attached to the carrier first using {@link
* #attachTo(Object carrier)} attached.
* <li>Finally, the third option is to get the default root Context instance calling {@link
* #root()}.
* </ul>
*
* <p>When there is no context attached to the current execution unit, {@link #current()} will
* return the root context. Similarly, {@link #from(Object carrier)} will return the root context
* when there is no context attached to the carrier.
*
* <p>From a {@link Context} instance, each value is stored and retrieved by its {@link ContextKey},
* using {@link #with(ContextKey key, Object value)} to store a value (creating a new immutable
* {@link Context} instance), and {@link #get(ContextKey)} to retrieve it. {@link ContextKey}s
* represent product of functional areas, and should be created sparingly.
*
* <p>{@link Context} instances are thread safe as they are immutable (including their {@link
* ContextKey}) but the value they hold may themselves be mutable.
*
* @see ContextKey
*/
@ParametersAreNonnullByDefault
public interface Context {
/**
* Returns the root context.
*
* @return the initial local context that all contexts extend.
*/
static Context root() {
return manager().root();
}

/**
* Returns the context attached to the current execution unit.
*
* @return the attached context; {@link #root()} if there is none.
*/
static Context current() {
return manager().current();
}

/**
* Attaches this context to the current execution unit.
*
* @return a scope to be closed when the context is invalid.
*/
default ContextScope attach() {
return manager().attach(this);
}

/**
* Swaps this context with the one attached to current execution unit.
*
* @return the previously attached context; {@link #root()} if there was none.
*/
default Context swap() {
return manager().swap(this);
}

/**
* Returns the context attached to the given carrier object.
*
* @param carrier the carrier object to get the context from.
* @return the attached context; {@link #root()} if there is none.
*/
static Context from(Object carrier) {
return binder().from(carrier);
}

/**
* Attaches this context to the given carrier object.
*
* @param carrier the object to carry the context.
*/
default void attachTo(Object carrier) {
binder().attachTo(carrier, this);
}

/**
* Detaches the context attached to the given carrier object, leaving it context-less.
*
* @param carrier the carrier object to detach its context from.
* @return the previously attached context; {@link #root()} if there was none.
*/
static Context detachFrom(Object carrier) {
return binder().detachFrom(carrier);
}

/**
* Gets the value stored in this context under the given key.
*
* @param <T> the type of the value.
* @param key the key used to store the value.
* @return the value stored under the key; {@code null} if there is none.
*/
@Nullable
<T> T get(ContextKey<T> key);

/**
* Creates a copy of this context with the given key-value set.
*
* <p>Existing value with the given key will be replaced, and mapping to a {@code null} value will
* remove the key-value from the context copy.
*
* @param <T> the type of the value.
* @param key the key to store the value.
* @param value the value to store.
* @return a new context with the key-value set.
*/
<T> Context with(ContextKey<T> key, @Nullable T value);

/**
* Creates a copy of this context with the implicit key is mapped to the value.
*
* @param value the value to store.
* @return a new context with the implicitly keyed value set.
* @see #with(ContextKey, Object)
*/
default Context with(@Nullable ImplicitContextKeyed value) {
if (value == null) {
return root();
}
return value.storeInto(this);
}
}
Loading

0 comments on commit 8e854bf

Please sign in to comment.