Skip to content
Merged
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
7 changes: 5 additions & 2 deletions dd-java-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def sharedShadowJar = tasks.register('sharedShadowJar', ShadowJar) {
// Put the jar in a different directory so we don't overwrite the normal shadow jar and
// break caching, and also to not interfere with CI scripts that copy everything in the
// libs directory
it.destinationDirectory.set(file("${project.buildDir}/shared-lib"))
it.destinationDirectory.set(project.layout.buildDirectory.dir("shared-lib"))
// Add a classifier so we don't confuse the jar file with the normal shadow jar
archiveClassifier = 'shared'
it.dependencies {
Expand All @@ -192,7 +192,7 @@ includeShadowJar(sharedShadowJar, 'shared')
// place the tracer in its own shadow jar separate to instrumentation
def traceShadowJar = tasks.register('traceShadowJar', ShadowJar) {
configurations = [project.configurations.traceShadowInclude]
it.destinationDirectory.set(file("${project.buildDir}/trace-lib"))
it.destinationDirectory.set(project.layout.buildDirectory.dir("trace-lib"))
archiveClassifier = 'trace'
it.dependencies deps.excludeShared
}
Expand Down Expand Up @@ -265,6 +265,9 @@ dependencies {
testImplementation project(':dd-trace-core')
testImplementation project(':utils:test-utils')

testImplementation group: 'com.squareup.okhttp3', name: 'mockwebserver', version: libs.versions.okhttp.legacy.get()
testImplementation project(':utils:test-agent-utils:decoder')

testImplementation libs.bundles.test.logging
testImplementation libs.guava
testImplementation libs.okhttp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datadog.json.JsonWriter;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.Closeable;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -211,7 +212,8 @@ public ForwarderJsonSenderThread(String forwarderPath, byte[] payload) {
public void run() {
ProcessBuilder builder = new ProcessBuilder(forwarderPath, "library_entrypoint");

try {
// Run forwarder and mute tracing for subprocesses executed in by dd-java-agent.
try (final Closeable ignored = muteTracing()) {
Process process = builder.start();
try (OutputStream out = process.getOutputStream()) {
out.write(payload);
Expand All @@ -221,5 +223,19 @@ public void run() {
System.err.println("Failed to send telemetry: " + e.getMessage());
}
}

@SuppressForbidden
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice solution

private Closeable muteTracing() {
try {
Class<?> agentTracerClass =
Class.forName("datadog.trace.bootstrap.instrumentation.api.AgentTracer");
Object tracerAPI = agentTracerClass.getMethod("get").invoke(null);
Object scope = tracerAPI.getClass().getMethod("muteTracing").invoke(tracerAPI);
return (Closeable) scope;
} catch (Throwable e) {
// Ignore all exceptions and fallback to No-Op Closable.
return () -> {};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class AgentLoadedIntoBootstrapTest extends Specification {
def "Agent loads in when separate jvm is launched"() {
expect:
IntegrationTestUtils.runOnSeparateJvm(AgentLoadedChecker.getName()
, "" as String[]
, "" as String[]
, []
, []
, [:]
, true) == 0
}
Expand All @@ -28,8 +28,8 @@ class AgentLoadedIntoBootstrapTest extends Specification {

expect:
IntegrationTestUtils.runOnSeparateJvm(mainClassName
, "" as String[]
, "" as String[]
, []
, []
, [:]
, pathToJar as String
, true) == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class CustomLogManagerTest extends Specification {
"-Ddd.jmxfetch.refresh-beans-period=1",
"-Ddd.profiling.enabled=true",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -36,8 +36,8 @@ class CustomLogManagerTest extends Specification {
"-Ddd.profiling.enabled=true",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Djava.util.logging.manager=jvmbootstraptest.CustomLogManager"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -52,8 +52,8 @@ class CustomLogManagerTest extends Specification {
"-Ddd.profiling.enabled=true",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Djava.util.logging.manager=jvmbootstraptest.MissingLogManager"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -68,8 +68,8 @@ class CustomLogManagerTest extends Specification {
"-Ddd.profiling.enabled=true",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Ddd.app.customlogmanager=true"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -84,8 +84,8 @@ class CustomLogManagerTest extends Specification {
"-Ddd.profiling.enabled=true",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Ddd.app.customjmxbuilder=false"
] as String[]
, "" as String[]
]
, []
, ["JBOSS_HOME": "/"]
, true) == 0
}
Expand All @@ -102,8 +102,8 @@ class CustomLogManagerTest extends Specification {
"-Ddd.app.customlogmanager=false",
"-Ddd.app.customjmxbuilder=false",
"-Djava.util.logging.manager=jvmbootstraptest.CustomLogManager"
] as String[]
, "" as String[]
]
, []
, ["JBOSS_HOME": "/"]
, true) == 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CustomMBeanServerBuilderTest extends Specification {
"-Ddd.profiling.enabled=true",
"-Ddd.instrumentation.telemetry.enabled=false",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -38,8 +38,8 @@ class CustomMBeanServerBuilderTest extends Specification {
"-Ddd.instrumentation.telemetry.enabled=false",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Djavax.management.builder.initial=jvmbootstraptest.CustomMBeanServerBuilder"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -55,8 +55,8 @@ class CustomMBeanServerBuilderTest extends Specification {
"-Ddd.instrumentation.telemetry.enabled=false",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Djavax.management.builder.initial=jvmbootstraptest.MissingMBeanServerBuilder"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -72,8 +72,8 @@ class CustomMBeanServerBuilderTest extends Specification {
"-Ddd.instrumentation.telemetry.enabled=false",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Ddd.app.customjmxbuilder=true"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand All @@ -90,8 +90,8 @@ class CustomMBeanServerBuilderTest extends Specification {
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL",
"-Ddd.app.customjmxbuilder=false",
"-Djavax.management.builder.initial=jvmbootstraptest.CustomMBeanServerBuilder"
] as String[]
, "" as String[]
]
, []
, [:]
, true) == 0
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package datadog.trace.agent

import spock.lang.Specification
import spock.lang.Timeout
import spock.lang.IgnoreIf
import datadog.test.SimpleAgentMock
import datadog.trace.api.Platform

import jvmbootstraptest.InitializationTelemetryCheck
import spock.lang.IgnoreIf
import spock.lang.Specification
import spock.lang.Timeout

@Timeout(30)
@IgnoreIf(reason = "SecurityManager is permanently disabled as of JDK 24", value = {
Expand All @@ -26,19 +26,39 @@ class InitializationTelemetryTest extends Specification {

def "normal start-up"() {
when:
def result = InitializationTelemetryCheck.runTestJvm(null, false, "sleep")
def result = InitializationTelemetryCheck.runTestJvm(null)

then:
result.exitCode == 0
result.telemetryJson.contains('library_entrypoint.complete')
}

def "test initial telemetry forwarder trace muted"() {
when:
def agent = new SimpleAgentMock().start()
def result = InitializationTelemetryCheck.runTestJvm(null, agent.port)

then:
result.exitCode == 0
result.telemetryJson.contains('library_entrypoint.complete')

// Check that we have only one span related to sub-process execution,
// and it is not initial telemetry forwarder.
agent.spans.size() == 1
def span = agent.spans.get(0)
span.name == 'command_execution'
span.resource == 'echo'

cleanup:
agent.close()
}

def "incomplete agent start-up"() {
// In this case, the SecurityManager blocks a custom permission that is checked by bytebuddy causing
// agent initialization to fail. However, we should catch the exception allowing the application
// to run normally.
when:
def result = InitializationTelemetryCheck.runTestJvm(InitializationTelemetryCheck.BlockByteBuddy, false, "sleep")
def result = InitializationTelemetryCheck.runTestJvm(InitializationTelemetryCheck.BlockByteBuddy)

then:
result.exitCode == 0
Expand All @@ -50,7 +70,7 @@ class InitializationTelemetryTest extends Specification {
// In this case, the SecurityManager blocks access to the forwarder environment variable,
// so the tracer is unable to report initialization telemetry
when:
def result = InitializationTelemetryCheck.runTestJvm(InitializationTelemetryCheck.BlockForwarderEnvVar, true)
def result = InitializationTelemetryCheck.runTestJvm(InitializationTelemetryCheck.BlockForwarderEnvVar)

then:
result.exitCode == 0
Expand All @@ -62,7 +82,7 @@ class InitializationTelemetryTest extends Specification {
// In this case, the SecurityManager blocks access to process execution, so the tracer is
// unable to invoke the forwarder executable
when:
def result = InitializationTelemetryCheck.runTestJvm(InitializationTelemetryCheck.BlockForwarderExecution, true)
def result = InitializationTelemetryCheck.runTestJvm(InitializationTelemetryCheck.BlockForwarderExecution)

then:
result.exitCode == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class InstrumenterUnloadTest extends Specification {
, [
"-verbose:class",
"-Ddatadog.slf4j.simpleLogger.defaultLogLevel=$DEFAULT_LOG_LEVEL"
] as String[]
, "" as String[]
]
, []
, ["DD_API_KEY": API_KEY]
, new PrintStream(testOutput))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class JMXFetchTest extends Specification {
"-Ddd.jmxfetch.start-delay=0",
"-Ddd.jmxfetch.statsd.port=${jmxStatsSocket.localPort}",
"-Ddd.writer.type=DDAgentWriter"
] as String[]
, ["30000"] as String[]
]
, ["30000"]
, [:]
, System.getProperty("java.class.path"))

Expand Down Expand Up @@ -64,8 +64,8 @@ class JMXFetchTest extends Specification {
"-Ddd.jmxfetch.start-delay=0",
"-Ddd.jmxfetch.statsd.host=example.local",
"-Ddd.writer.type=DDAgentWriter"
] as String[]
, "" as String[]
]
, []
, [:]
, true)

Expand All @@ -89,8 +89,8 @@ class JMXFetchTest extends Specification {
"-Ddd.trace.debug=true",
"-Ddd.writer.type=DDAgentWriter"
]
+ configSettings as String[]
, "" as String[]
+ configSettings as List<CharSequence>
, []
, [:]
, new PrintStream(testOutput))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ListIntegrationsTest extends Specification {
def testOutput = new ByteArrayOutputStream()
expect:
IntegrationTestUtils.runOnSeparateJvm(AgentJar.name
, [] as String[]
, ["-li"] as String[]
, []
, ["-li"]
, [:]
, new PrintStream(testOutput)) == 0
!new String(testOutput.toByteArray()).contains("ERROR")
Expand Down
Loading