Skip to content

Commit dfbdeb4

Browse files
authored
Merge branch 'master' into INPLAT-614
2 parents 8a1b47d + 33422a3 commit dfbdeb4

File tree

114 files changed

+3402
-415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+3402
-415
lines changed

.gitlab-ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ variables:
4141
description: "Enable flaky tests"
4242
value: "false"
4343

44-
default:
45-
interruptible: true
46-
4744
# trigger new commit cancel
4845
workflow:
4946
auto_cancel:
@@ -100,6 +97,7 @@ workflow:
10097

10198
default:
10299
tags: [ "arch:amd64" ]
100+
interruptible: true
103101

104102
.set_datadog_api_keys: &set_datadog_api_keys
105103
- export DATADOG_API_KEY_PROD=$(aws ssm get-parameter --region us-east-1 --name ci.dd-trace-java.DATADOG_API_KEY_PROD --with-decryption --query "Parameter.Value" --out text)
@@ -521,7 +519,7 @@ muzzle-dep-report:
521519
CI_USE_TEST_AGENT: "true"
522520
CI_AGENT_HOST: local-agent
523521
services:
524-
- name: ghcr.io/datadog/dd-apm-test-agent/ddapm-test-agent:v1.24.1
522+
- name: ghcr.io/datadog/dd-apm-test-agent/ddapm-test-agent:v1.27.1
525523
alias: local-agent
526524
variables:
527525
LOG_LEVEL: "DEBUG"

.gitlab/check_test_agent_results.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ SUMMARY_RESPONSE_CODE=$(echo "$SUMMARY_RESPONSE" | awk 'END {print $NF}')
77

88
if [[ SUMMARY_RESPONSE_CODE -eq 200 ]]; then
99
echo "APM Test Agent is running. (HTTP 200)"
10+
elif [[ -n "$CI_USE_TEST_AGENT" ]]; then
11+
echo "APM Test Agent failed to start, had an error, or exited early."
12+
cat summary_response.txt
13+
exit 1
1014
else
1115
echo "APM Test Agent is not running and was not used for testing. No checks failed."
1216
exit 0

communication/src/main/java/datadog/communication/ddagent/ExternalAgentLauncher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import static datadog.trace.util.ProcessSupervisor.Health.NEVER_CHECKED;
66
import static datadog.trace.util.ProcessSupervisor.Health.READY_TO_START;
77

8+
import datadog.environment.OperatingSystem;
89
import datadog.trace.api.Config;
9-
import datadog.trace.api.Platform;
1010
import datadog.trace.util.ProcessSupervisor;
1111
import java.io.Closeable;
1212
import java.io.File;
@@ -17,7 +17,7 @@ public class ExternalAgentLauncher implements Closeable {
1717
private static final Logger log = LoggerFactory.getLogger(ExternalAgentLauncher.class);
1818

1919
private static final ProcessBuilder.Redirect DISCARD =
20-
ProcessBuilder.Redirect.to(new File((Platform.isWindows() ? "NUL" : "/dev/null")));
20+
ProcessBuilder.Redirect.to(new File((OperatingSystem.isWindows() ? "NUL" : "/dev/null")));
2121

2222
private ProcessSupervisor traceProcessSupervisor;
2323
private ProcessSupervisor dogStatsDProcessSupervisor;

communication/src/main/java/datadog/communication/monitor/DDAgentStatsDConnection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import com.timgroup.statsd.NoOpDirectStatsDClient;
99
import com.timgroup.statsd.NonBlockingStatsDClientBuilder;
1010
import com.timgroup.statsd.StatsDClientErrorHandler;
11+
import datadog.environment.OperatingSystem;
1112
import datadog.trace.api.Config;
12-
import datadog.trace.api.Platform;
1313
import datadog.trace.relocate.api.IOLogger;
1414
import datadog.trace.util.AgentTaskScheduler;
1515
import datadog.trace.util.AgentThreadFactory;
@@ -136,7 +136,7 @@ private void doConnect() {
136136
if (bufferSize != null) {
137137
clientBuilder.socketBufferSize(bufferSize);
138138
}
139-
int packetSize = Platform.isMac() ? 2048 : 8192;
139+
int packetSize = OperatingSystem.isMacOs() ? 2048 : 8192;
140140
if (bufferSize != null && bufferSize < packetSize) {
141141
packetSize = bufferSize;
142142
}
@@ -185,7 +185,7 @@ private void discoverConnectionSettings() {
185185
}
186186

187187
if (null == host) {
188-
if (!Platform.isWindows() && new File(DEFAULT_DOGSTATSD_SOCKET_PATH).exists()) {
188+
if (!OperatingSystem.isWindows() && new File(DEFAULT_DOGSTATSD_SOCKET_PATH).exists()) {
189189
log.info("Detected {}. Using it to send StatsD data.", DEFAULT_DOGSTATSD_SOCKET_PATH);
190190
host = DEFAULT_DOGSTATSD_SOCKET_PATH;
191191
port = 0; // tells dogstatsd client to treat host as a socket path
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
`java-library`
3+
id("com.gradleup.shadow")
4+
}
5+
6+
apply(from = "$rootDir/gradle/java.gradle")
7+
8+
/*
9+
* Add an addition gradle configuration to be consumed by bootstrap only.
10+
* "datadog.trace." prefix is required to be excluded from Jacoco instrumentation.
11+
* See ConfigDefaults.DEFAULT_CIVISIBILITY_JACOCO_PLUGIN_EXCLUDES for more details.
12+
*/
13+
tasks.shadowJar {
14+
relocate("datadog.environment", "datadog.trace.bootstrap.environment")
15+
}
16+
17+
/*
18+
* Configure test coverage.
19+
*/
20+
extra.set("minimumInstructionCoverage", 0.7)
21+
val excludedClassesCoverage by extra {
22+
listOf(
23+
"datadog.environment.JavaVirtualMachine", // depends on OS and JVM vendor
24+
"datadog.environment.JavaVirtualMachine.JvmOptionsHolder", // depends on OS and JVM vendor
25+
"datadog.environment.JvmOptions", // depends on OS and JVM vendor
26+
"datadog.environment.OperatingSystem", // depends on OS
27+
)
28+
}
29+
val excludedClassesBranchCoverage by extra {
30+
listOf("datadog.environment.CommandLine") // tested using forked process
31+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package datadog.environment;
2+
3+
import static java.util.Collections.emptyList;
4+
5+
import de.thetaphi.forbiddenapis.SuppressForbidden;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/**
10+
* Fetches and captures the command line, both command and its arguments. It relies on a
11+
* non-standard {@code sun.java.command} system property and was tested on:
12+
*
13+
* <ul>
14+
* <li>OracleJDK,
15+
* <li>OpenJDK,
16+
* <li>Temurin based JDK,
17+
* <li>IMB JDK,
18+
* <li>Azul Zulu,
19+
* <li>Amazon Coretto,
20+
* </ul>
21+
*
22+
* This should be replaced by {@code ProcessHandle} and {@code ProcessHandle.Info} once Java 9+
23+
* become available.
24+
*/
25+
class CommandLine {
26+
private static final String SUN_JAVA_COMMAND_PROPERTY = "sun.java.command";
27+
final List<String> fullCommand = findFullCommand();
28+
final String name = getCommandName();
29+
final List<String> arguments = getCommandArguments();
30+
31+
@SuppressForbidden // split on single-character uses fast path
32+
private List<String> findFullCommand() {
33+
String command = SystemProperties.getOrDefault(SUN_JAVA_COMMAND_PROPERTY, "").trim();
34+
return command.isEmpty() ? emptyList() : Arrays.asList(command.split(" "));
35+
}
36+
37+
private String getCommandName() {
38+
return fullCommand.isEmpty() ? null : fullCommand.get(0);
39+
}
40+
41+
private List<String> getCommandArguments() {
42+
if (fullCommand.isEmpty()) {
43+
return fullCommand;
44+
} else {
45+
return fullCommand.subList(1, fullCommand.size());
46+
}
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package datadog.environment;
2+
3+
import javax.annotation.Nonnull;
4+
import javax.annotation.Nullable;
5+
6+
/**
7+
* Safely queries environment variables against security manager.
8+
*
9+
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html">Security
10+
* Manager</a>
11+
*/
12+
public final class EnvironmentVariables {
13+
private EnvironmentVariables() {}
14+
15+
/**
16+
* Gets an environment variable value.
17+
*
18+
* @param name The environment variable name.
19+
* @return The environment variable value, {@code null} if missing or can't be retrieved.
20+
*/
21+
public static @Nullable String get(String name) {
22+
return getOrDefault(name, null);
23+
}
24+
25+
/**
26+
* Gets an environment variable value, or default value if missing or can't be retrieved.
27+
*
28+
* @param name The environment variable name.
29+
* @param defaultValue The default value to return if the environment variable is missing or can't
30+
* be retrieved.
31+
* @return The environment variable value, {@code defaultValue} if missing or can't be retrieved.
32+
*/
33+
public static String getOrDefault(@Nonnull String name, String defaultValue) {
34+
try {
35+
String value = System.getenv(name);
36+
return value == null ? defaultValue : value;
37+
} catch (SecurityException e) {
38+
return defaultValue;
39+
}
40+
}
41+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package datadog.environment;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* This class represents a Java version according the String Naming Convention.
8+
*
9+
* @see <a href="https://www.oracle.com/java/technologies/javase/versioning-naming.html">String
10+
* Naming Convention</a>
11+
*/
12+
final class JavaVersion {
13+
final int major;
14+
final int minor;
15+
final int update;
16+
17+
JavaVersion(int major, int minor, int update) {
18+
this.major = major;
19+
this.minor = minor;
20+
this.update = update;
21+
}
22+
23+
static JavaVersion getRuntimeVersion() {
24+
return parseJavaVersion(SystemProperties.getOrDefault("java.version", ""));
25+
}
26+
27+
static JavaVersion parseJavaVersion(String javaVersion) {
28+
// Remove pre-release part, usually -ea
29+
final int indexOfDash = javaVersion.indexOf('-');
30+
if (indexOfDash >= 0) {
31+
javaVersion = javaVersion.substring(0, indexOfDash);
32+
}
33+
34+
int major = 0;
35+
int minor = 0;
36+
int update = 0;
37+
38+
try {
39+
List<Integer> nums = splitDigits(javaVersion);
40+
major = nums.get(0);
41+
42+
// for java 1.6/1.7/1.8
43+
if (major == 1) {
44+
major = nums.get(1);
45+
minor = nums.get(2);
46+
update = nums.get(3);
47+
} else {
48+
minor = nums.get(1);
49+
update = nums.get(2);
50+
}
51+
} catch (NumberFormatException | IndexOutOfBoundsException e) {
52+
// unable to parse version string - do nothing
53+
}
54+
return new JavaVersion(major, minor, update);
55+
}
56+
57+
/* The method splits java version string by digits. Delimiters are: dot, underscore and plus */
58+
private static List<Integer> splitDigits(String str) {
59+
List<Integer> results = new ArrayList<>();
60+
61+
int len = str.length();
62+
int value = 0;
63+
for (int i = 0; i < len; i++) {
64+
char ch = str.charAt(i);
65+
if (ch >= '0' && ch <= '9') {
66+
value = value * 10 + (ch - '0');
67+
} else if (ch == '.' || ch == '_' || ch == '+') {
68+
results.add(value);
69+
value = 0;
70+
} else {
71+
throw new NumberFormatException();
72+
}
73+
}
74+
results.add(value);
75+
return results;
76+
}
77+
78+
public boolean is(int major) {
79+
return this.major == major;
80+
}
81+
82+
public boolean is(int major, int minor) {
83+
return this.major == major && this.minor == minor;
84+
}
85+
86+
public boolean is(int major, int minor, int update) {
87+
return this.major == major && this.minor == minor && this.update == update;
88+
}
89+
90+
public boolean isAtLeast(int major, int minor, int update) {
91+
return isAtLeast(this.major, this.minor, this.update, major, minor, update);
92+
}
93+
94+
public boolean isBetween(
95+
int fromMajor, int fromMinor, int fromUpdate, int toMajor, int toMinor, int toUpdate) {
96+
return isAtLeast(toMajor, toMinor, toUpdate, fromMajor, fromMinor, fromUpdate)
97+
&& isAtLeast(fromMajor, fromMinor, fromUpdate)
98+
&& !isAtLeast(toMajor, toMinor, toUpdate);
99+
}
100+
101+
private static boolean isAtLeast(
102+
int major, int minor, int update, int atLeastMajor, int atLeastMinor, int atLeastUpdate) {
103+
return (major > atLeastMajor)
104+
|| (major == atLeastMajor && minor > atLeastMinor)
105+
|| (major == atLeastMajor && minor == atLeastMinor && update >= atLeastUpdate);
106+
}
107+
}

0 commit comments

Comments
 (0)