-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate module context from build system process to child JVM proce…
…sses (#7710)
- Loading branch information
1 parent
919bf01
commit 5a939c0
Showing
44 changed files
with
305 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ProcessHierarchy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package datadog.trace.civisibility; | ||
|
||
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.propagate; | ||
|
||
import datadog.trace.api.config.CiVisibilityConfig; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import datadog.trace.util.Strings; | ||
import java.net.InetSocketAddress; | ||
import java.util.Properties; | ||
import javax.annotation.Nullable; | ||
|
||
public class ProcessHierarchy { | ||
|
||
private static final class SystemPropertiesPropagationGetter | ||
implements AgentPropagation.ContextVisitor<Properties> { | ||
static final AgentPropagation.ContextVisitor<Properties> INSTANCE = | ||
new SystemPropertiesPropagationGetter(); | ||
|
||
private SystemPropertiesPropagationGetter() {} | ||
|
||
@Override | ||
public void forEachKey(Properties carrier, AgentPropagation.KeyClassifier classifier) { | ||
for (String propertyName : carrier.stringPropertyNames()) { | ||
if (!classifier.accept(propertyName, carrier.getProperty(propertyName))) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Nullable public final AgentSpan.Context.Extracted parentProcessModuleContext; | ||
|
||
ProcessHierarchy() { | ||
parentProcessModuleContext = | ||
propagate().extract(System.getProperties(), SystemPropertiesPropagationGetter.INSTANCE); | ||
} | ||
|
||
/** | ||
* Module span context is propagated from the parent process if it runs with the tracer attached. | ||
* If module span context is note there, either we are in the build system, or we are in the tests | ||
* JVM and the build system is not instrumented. | ||
*/ | ||
public boolean isChild() { | ||
return parentProcessModuleContext != null; | ||
} | ||
|
||
/** | ||
* Determines if current process runs in "headless mode", i.e. has no instrumented parent and is | ||
* not one of the supported build system processes. | ||
*/ | ||
public boolean isHeadless() { | ||
return !isChild() && !isParent(); | ||
} | ||
|
||
private boolean isParent() { | ||
return isMavenParent() || isGradleDaemon(); | ||
} | ||
|
||
private boolean isMavenParent() { | ||
return System.getProperty("maven.home") != null | ||
&& System.getProperty("classworlds.conf") != null; | ||
} | ||
|
||
private boolean isGradleDaemon() { | ||
return ClassLoader.getSystemClassLoader() | ||
.getResource("org/gradle/launcher/daemon/bootstrap/GradleDaemon.class") | ||
!= null | ||
// double-check this is not a Gradle Worker | ||
&& System.getProperties().getProperty("org.gradle.internal.worker.tmpdir") == null; | ||
} | ||
|
||
@Nullable | ||
public InetSocketAddress getSignalServerAddress() { | ||
// System.getProperty is used rather than Config, | ||
// because system variables can be set after config was initialized | ||
String host = | ||
System.getProperty( | ||
Strings.propertyNameToSystemPropertyName( | ||
CiVisibilityConfig.CIVISIBILITY_SIGNAL_SERVER_HOST)); | ||
String port = | ||
System.getProperty( | ||
Strings.propertyNameToSystemPropertyName( | ||
CiVisibilityConfig.CIVISIBILITY_SIGNAL_SERVER_PORT)); | ||
if (host != null && port != null) { | ||
return new InetSocketAddress(host, Integer.parseInt(port)); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.