|
1 | 1 | package datadog.trace.agent |
2 | 2 |
|
3 | 3 | import datadog.trace.agent.test.IntegrationTestUtils |
4 | | -import datadog.trace.api.Config |
5 | 4 | import jvmbootstraptest.AgentLoadedChecker |
6 | | -import org.junit.Rule |
7 | | -import org.junit.contrib.java.lang.system.RestoreSystemProperties |
| 5 | +import jvmbootstraptest.JmxStartedChecker |
| 6 | +import spock.lang.Shared |
8 | 7 | import spock.lang.Specification |
9 | 8 | import spock.lang.Timeout |
10 | 9 |
|
11 | | -import java.lang.reflect.Method |
12 | | - |
13 | 10 | @Timeout(30) |
14 | 11 | class JMXFetchTest extends Specification { |
| 12 | + @Shared |
| 13 | + DatagramSocket jmxStatsSocket |
| 14 | + |
| 15 | + def setupSpec() { |
| 16 | + jmxStatsSocket = new DatagramSocket(0) |
| 17 | + } |
15 | 18 |
|
16 | | - @Rule |
17 | | - public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties() |
| 19 | + def cleanupSpec() { |
| 20 | + jmxStatsSocket.close() |
| 21 | + } |
18 | 22 |
|
19 | 23 | def "test jmxfetch"() { |
20 | 24 | setup: |
21 | | - def currentContextLoader = Thread.currentThread().getContextClassLoader() |
22 | | - DatagramSocket socket = new DatagramSocket(0) |
23 | | - |
24 | | - System.setProperty("dd.jmxfetch.enabled", "true") |
25 | | - System.setProperty("dd.jmxfetch.start-delay", "0") |
26 | | - System.setProperty("dd.jmxfetch.statsd.port", Integer.toString(socket.localPort)) |
27 | | - // Overwrite writer type to disable console jmxfetch reporter |
28 | | - System.setProperty("dd.writer.type", "DDAgentWriter") |
29 | | - |
30 | | - def classLoader = IntegrationTestUtils.getJmxFetchClassLoader() |
31 | | - // Have to set this so JMXFetch knows where to find resources |
32 | | - Thread.currentThread().setContextClassLoader(classLoader) |
33 | | - final Class<?> jmxFetchAgentClass = |
34 | | - classLoader.loadClass("datadog.trace.agent.jmxfetch.JMXFetch") |
35 | | - final Method jmxFetchInstallerMethod = jmxFetchAgentClass.getDeclaredMethod("run", Config) |
36 | | - jmxFetchInstallerMethod.setAccessible(true) |
37 | | - jmxFetchInstallerMethod.invoke(null, new Config()) |
| 25 | + // verify that JMX starts and reports metrics through the given socket. |
| 26 | + def returnCode = IntegrationTestUtils.runOnSeparateJvm(JmxStartedChecker.getName() |
| 27 | + , ["-Ddd.jmxfetch.enabled=true", |
| 28 | + "-Ddd.jmxfetch.start-delay=0", |
| 29 | + "-Ddd.jmxfetch.statsd.port=${jmxStatsSocket.localPort}", |
| 30 | + "-Ddd.writer.type=DDAgentWriter"] as String[] |
| 31 | + , "" as String[] |
| 32 | + , [:] |
| 33 | + , true) |
38 | 34 |
|
39 | 35 | byte[] buf = new byte[1500] |
40 | 36 | DatagramPacket packet = new DatagramPacket(buf, buf.length) |
41 | | - socket.receive(packet) |
| 37 | + jmxStatsSocket.receive(packet) |
42 | 38 | String received = new String(packet.getData(), 0, packet.getLength()) |
43 | 39 |
|
44 | | - Set<String> threads = Thread.getAllStackTraces().keySet().collect { it.name } |
45 | | - |
46 | 40 | expect: |
47 | | - threads.contains("dd-jmx-collector") |
48 | | - received.contains("jvm.") |
49 | | - |
50 | | - cleanup: |
51 | | - jmxFetchInstallerMethod.setAccessible(false) |
52 | | - socket.close() |
53 | | - Thread.currentThread().setContextClassLoader(currentContextLoader) |
| 41 | + returnCode == 0 |
| 42 | + received.contains("#service:${JmxStartedChecker.getName()}") |
54 | 43 | } |
55 | 44 |
|
56 | 45 | def "Agent loads when JmxFetch is misconfigured"() { |
| 46 | + setup: |
57 | 47 | // verify the agent starts up correctly with a bogus address. |
58 | | - expect: |
59 | | - IntegrationTestUtils.runOnSeparateJvm(AgentLoadedChecker.getName() |
| 48 | + def returnCode = IntegrationTestUtils.runOnSeparateJvm(AgentLoadedChecker.getName() |
60 | 49 | , ["-Ddd.jmxfetch.enabled=true", |
61 | 50 | "-Ddd.jmxfetch.start-delay=0", |
62 | | - "-Ddd.jmxfetch.statsd.host=example.local"] as String[] |
| 51 | + "-Ddd.jmxfetch.statsd.host=example.local", |
| 52 | + "-Ddd.writer.type=DDAgentWriter"] as String[] |
63 | 53 | , "" as String[] |
64 | 54 | , [:] |
65 | | - , true) == 0 |
| 55 | + , true) |
| 56 | + |
| 57 | + expect: |
| 58 | + returnCode == 0 |
66 | 59 | } |
67 | 60 |
|
68 | 61 | def "test jmxfetch config"() { |
69 | 62 | setup: |
70 | | - names.each { |
71 | | - System.setProperty("dd.jmxfetch.${it}.enabled", "$enable") |
| 63 | + def configSettings = names.collect { |
| 64 | + "-Ddd.jmxfetch.${it}.enabled=${enable}" |
| 65 | + } |
| 66 | + def testOutput = new ByteArrayOutputStream() |
| 67 | + def returnCode = IntegrationTestUtils.runOnSeparateJvm(JmxStartedChecker.getName() |
| 68 | + , ["-Ddd.jmxfetch.enabled=true", |
| 69 | + "-Ddd.jmxfetch.start-delay=0", |
| 70 | + "-Ddd.jmxfetch.statsd.port=${jmxStatsSocket.localPort}", |
| 71 | + "-Ddd.trace.debug=true", |
| 72 | + "-Ddd.writer.type=DDAgentWriter"] + configSettings as String[] |
| 73 | + , "" as String[] |
| 74 | + , [:] |
| 75 | + , new PrintStream(testOutput)) |
| 76 | + |
| 77 | + def actualConfig = [] |
| 78 | + new ByteArrayInputStream((testOutput.toByteArray())).eachLine { |
| 79 | + System.out.println(it) |
| 80 | + def match = (it =~ 'Reading metric config resource (.*)') |
| 81 | + if (match) { |
| 82 | + actualConfig += match[0][1] |
| 83 | + } |
72 | 84 | } |
73 | | - def classLoader = IntegrationTestUtils.getJmxFetchClassLoader() |
74 | | - // Have to set this so JMXFetch knows where to find resources |
75 | | - Thread.currentThread().setContextClassLoader(classLoader) |
76 | | - final Class<?> jmxFetchAgentClass = |
77 | | - classLoader.loadClass("datadog.trace.agent.jmxfetch.JMXFetch") |
78 | | - final Method jmxFetchInstallerMethod = jmxFetchAgentClass.getDeclaredMethod("getInternalMetricFiles") |
79 | | - jmxFetchInstallerMethod.setAccessible(true) |
80 | 85 |
|
81 | 86 | expect: |
82 | | - jmxFetchInstallerMethod.invoke(null).sort() == result.sort() |
83 | | - |
84 | | - cleanup: |
85 | | - names.each { |
86 | | - System.clearProperty("dd.jmxfetch.${it}.enabled") |
87 | | - } |
| 87 | + returnCode == 0 |
| 88 | + actualConfig as Set == expectedConfig as Set |
88 | 89 |
|
89 | 90 | where: |
90 | | - names | enable | result |
| 91 | + names | enable | expectedConfig |
91 | 92 | [] | true | [] |
92 | 93 | ["tomcat"] | false | [] |
93 | 94 | ["tomcat"] | true | ["datadog/trace/agent/jmxfetch/metricconfigs/tomcat.yaml"] |
|
0 commit comments