Expand custom logman tests to handle three different logman cases
This commit is contained in:
parent
6ffaa9e04f
commit
4c4b12e1d6
|
@ -2,31 +2,48 @@ package datadog.trace.agent
|
||||||
|
|
||||||
import datadog.trace.agent.test.IntegrationTestUtils
|
import datadog.trace.agent.test.IntegrationTestUtils
|
||||||
import jvmbootstraptest.LogManagerSetter
|
import jvmbootstraptest.LogManagerSetter
|
||||||
|
import spock.lang.Shared
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory
|
import java.lang.management.ManagementFactory
|
||||||
import java.lang.management.RuntimeMXBean
|
import java.lang.management.RuntimeMXBean
|
||||||
|
|
||||||
class CustomLogManagerTest extends Specification {
|
class CustomLogManagerTest extends Specification {
|
||||||
/**
|
// Tests using forked jvm because groovy has already set the global log manager
|
||||||
* Forked jvm test must run in a pure java runtime because groovy sets the global log manager.
|
@Shared
|
||||||
*/
|
private def agentArg
|
||||||
def "javaagent setup does not set the global log manager"() {
|
|
||||||
setup:
|
def setupSpec() {
|
||||||
final RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean()
|
final RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean()
|
||||||
String agentArg = null
|
|
||||||
for (String arg : runtimeMxBean.getInputArguments()) {
|
for (String arg : runtimeMxBean.getInputArguments()) {
|
||||||
if (arg.startsWith("-javaagent")) {
|
if (arg.startsWith("-javaagent")) {
|
||||||
agentArg = arg
|
agentArg = arg
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final URL customAgent = IntegrationTestUtils.createJarWithClasses(LogManagerSetter.getName(), LogManagerSetter)
|
assert agentArg != null
|
||||||
|
}
|
||||||
|
|
||||||
|
def "jmxfetch starts up in premain when no custom log manager is set"() {
|
||||||
expect:
|
expect:
|
||||||
agentArg != null
|
|
||||||
IntegrationTestUtils.runOnSeparateJvm(LogManagerSetter.getName()
|
IntegrationTestUtils.runOnSeparateJvm(LogManagerSetter.getName()
|
||||||
, ["-javaagent:" + customAgent.getPath(), agentArg, "-Ddd.jmxfetch.enabled=true", "-Ddd.jmxfetch.refresh-beans-period=1", "-Ddatadog.slf4j.simpleLogger.defaultLogLevel=off"] as String[]
|
, [agentArg, "-Ddd.jmxfetch.enabled=true", "-Ddd.jmxfetch.refresh-beans-period=1", "-Ddatadog.slf4j.simpleLogger.defaultLogLevel=off"] as String[]
|
||||||
|
, "" as String[]
|
||||||
|
, true) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
def "jmxfetch startup is delayed when java.util.logging.manager sysprop is present"() {
|
||||||
|
expect:
|
||||||
|
IntegrationTestUtils.runOnSeparateJvm(LogManagerSetter.getName()
|
||||||
|
, [agentArg, "-Ddd.jmxfetch.enabled=true", "-Ddd.jmxfetch.refresh-beans-period=1", "-Ddatadog.slf4j.simpleLogger.defaultLogLevel=off", "-Djava.util.logging.manager=jvmbootstraptest.CustomLogManager"] as String[]
|
||||||
|
, "" as String[]
|
||||||
|
, true) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
def "jmxfetch startup is delayed when tracer custom log manager setting is present"() {
|
||||||
|
expect:
|
||||||
|
IntegrationTestUtils.runOnSeparateJvm(LogManagerSetter.getName()
|
||||||
|
, ["-javaagent:" + customAgent.getPath(), agentArg, "-Ddd.jmxfetch.enabled=true", "-Ddd.jmxfetch.refresh-beans-period=1", "-Ddatadog.slf4j.simpleLogger.defaultLogLevel=off", "-Ddd.app.customlogmanager=true"] as String[]
|
||||||
, "" as String[]
|
, "" as String[]
|
||||||
, true) == 0
|
, true) == 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +1,36 @@
|
||||||
package jvmbootstraptest;
|
package jvmbootstraptest;
|
||||||
|
|
||||||
import java.lang.instrument.Instrumentation;
|
|
||||||
import java.net.DatagramPacket;
|
|
||||||
import java.net.DatagramSocket;
|
|
||||||
import java.net.SocketException;
|
|
||||||
import java.util.logging.LogManager;
|
import java.util.logging.LogManager;
|
||||||
|
|
||||||
public class LogManagerSetter {
|
public class LogManagerSetter {
|
||||||
private static final DatagramSocket socket;
|
|
||||||
private static final int localPort;
|
|
||||||
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
socket = new DatagramSocket(0);
|
|
||||||
localPort = socket.getLocalPort();
|
|
||||||
} catch (SocketException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void premain(final String agentArgs, final Instrumentation inst) throws Exception {
|
|
||||||
// set jmxfetch port in premain before tracer's premain runs
|
|
||||||
System.setProperty("dd.jmxfetch.statsd.port", Integer.toString(localPort));
|
|
||||||
System.setProperty("dd.jmxfetch.statsd.host", "localhost");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String... args) throws Exception {
|
public static void main(String... args) throws Exception {
|
||||||
try {
|
if (System.getProperty("java.util.logging.manager") != null) {
|
||||||
// block until jmxfetch sends data
|
customAssert(
|
||||||
final byte[] buf = new byte[1500];
|
isJmxfetchStarted(),
|
||||||
final DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
false,
|
||||||
socket.receive(packet);
|
"jmxfetch startup must be delayed when log manager system property is present.");
|
||||||
} finally {
|
customAssert(
|
||||||
socket.close();
|
LogManager.getLogManager().getClass(),
|
||||||
}
|
LogManager.class
|
||||||
|
.getClassLoader()
|
||||||
|
.loadClass(System.getProperty("java.util.logging.manager")),
|
||||||
|
"Javaagent should not prevent setting a custom log manager");
|
||||||
|
customAssert(isJmxfetchStarted(), true, "jmxfetch should start after loading LogManager.");
|
||||||
|
} else if (System.getProperty("dd.app.customlogmanager") != null) {
|
||||||
System.setProperty("java.util.logging.manager", CustomLogManager.class.getName());
|
System.setProperty("java.util.logging.manager", CustomLogManager.class.getName());
|
||||||
customAssert(
|
customAssert(
|
||||||
LogManager.getLogManager().getClass(),
|
LogManager.getLogManager().getClass(),
|
||||||
CustomLogManager.class,
|
LogManager.class
|
||||||
|
.getClassLoader()
|
||||||
|
.loadClass(System.getProperty("java.util.logging.manager")),
|
||||||
"Javaagent should not prevent setting a custom log manager");
|
"Javaagent should not prevent setting a custom log manager");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
customAssert(
|
||||||
|
isJmxfetchStarted(),
|
||||||
|
true,
|
||||||
|
"jmxfetch should start in premain when no custom log manager is set.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void customAssert(Object got, Object expected, String assertionMessage) {
|
private static void customAssert(Object got, Object expected, String assertionMessage) {
|
||||||
|
@ -47,4 +39,13 @@ public class LogManagerSetter {
|
||||||
"Assertion failed. Expected <" + expected + "> got <" + got + "> " + assertionMessage);
|
"Assertion failed. Expected <" + expected + "> got <" + got + "> " + assertionMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isJmxfetchStarted() {
|
||||||
|
for (Thread thread : Thread.getAllStackTraces().keySet()) {
|
||||||
|
if ("dd-jmx-collector".equals(thread.getName())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue