Skip to content

Commit 18ede72

Browse files
authored
Merge branch 'master' into alejandro.gonzalez/APPSEC-57055
2 parents ed077e0 + 368851d commit 18ede72

13 files changed

Lines changed: 492 additions & 320 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package datadog.communication.monitor;
22

3-
import static datadog.trace.api.ConfigDefaults.DEFAULT_DOGSTATSD_PORT;
43
import static datadog.trace.bootstrap.instrumentation.api.WriterConstants.LOGGING_WRITER_TYPE;
54

65
import datadog.trace.api.Config;
@@ -22,7 +21,8 @@ public static StatsDClientManager statsDClientManager() {
2221
return INSTANCE;
2322
}
2423

25-
private static final AtomicInteger defaultStatsDPort = new AtomicInteger(DEFAULT_DOGSTATSD_PORT);
24+
private static final AtomicInteger defaultStatsDPort =
25+
new AtomicInteger(Config.get().getDogsStatsDPort());
2626

2727
public static void setDefaultStatsDPort(final int newPort) {
2828
if (newPort > 0 && defaultStatsDPort.getAndSet(newPort) != newPort) {

dd-java-agent/instrumentation/spark/src/main/java/datadog/trace/instrumentation/spark/AbstractDatadogSparkListener.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ public synchronized void onJobEnd(SparkListenerJobEnd jobEnd) {
498498
return;
499499
}
500500

501-
lastJobFailed = false;
502501
if (jobEnd.jobResult() instanceof JobFailed) {
503502
JobFailed jobFailed = (JobFailed) jobEnd.jobResult();
504503
Exception exception = jobFailed.exception();
@@ -510,9 +509,15 @@ public synchronized void onJobEnd(SparkListenerJobEnd jobEnd) {
510509
jobSpan.setErrorMessage(errorMessage);
511510
jobSpan.setTag(DDTags.ERROR_STACK, errorStackTrace);
512511
jobSpan.setTag(DDTags.ERROR_TYPE, "Spark Job Failed");
513-
lastJobFailed = true;
514-
lastJobFailedMessage = errorMessage;
515-
lastJobFailedStackTrace = errorStackTrace;
512+
513+
// Only propagate the error to the application if it is not a cancellation
514+
if (errorMessage != null && !errorMessage.toLowerCase().contains("cancelled")) {
515+
lastJobFailed = true;
516+
lastJobFailedMessage = errorMessage;
517+
lastJobFailedStackTrace = errorStackTrace;
518+
}
519+
} else {
520+
lastJobFailed = false;
516521
}
517522

518523
SparkAggregatedTaskMetrics metrics = jobMetrics.remove(jobEnd.jobId());

dd-java-agent/instrumentation/spark/src/testFixtures/groovy/datadog/trace/instrumentation/spark/AbstractSparkListenerTest.groovy

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ abstract class AbstractSparkListenerTest extends AgentTestRunner {
121121
return new SparkListenerJobEnd(jobId, time, JobSucceeded$.MODULE$)
122122
}
123123

124+
protected jobFailedEvent(Integer jobId, Long time, String errorMessage) {
125+
def exception = new RuntimeException(errorMessage)
126+
def jobFailed = new org.apache.spark.scheduler.JobFailed(exception)
127+
return new SparkListenerJobEnd(jobId, time, jobFailed)
128+
}
129+
124130
protected stageSubmittedEvent(Integer stageId, Long time) {
125131
def stageInfo = createStageInfo(stageId)
126132
stageInfo.submissionTime = Option.apply(time)
@@ -457,6 +463,34 @@ abstract class AbstractSparkListenerTest extends AgentTestRunner {
457463
}
458464
}
459465

466+
def "test lastJobFailed is not set when job is cancelled"() {
467+
setup:
468+
def listener = getTestDatadogSparkListener()
469+
listener.onApplicationStart(applicationStartEvent(1000L))
470+
listener.onJobStart(jobStartEvent(1, 1900L, [1]))
471+
listener.onJobEnd(jobFailedEvent(1, 2200L, "Job was cancelled by user"))
472+
listener.onApplicationEnd(new SparkListenerApplicationEnd(2300L))
473+
474+
expect:
475+
assertTraces(1) {
476+
trace(2) {
477+
span {
478+
operationName "spark.application"
479+
resourceName "spark.application"
480+
spanType "spark"
481+
errored false
482+
parent()
483+
}
484+
span {
485+
operationName "spark.job"
486+
spanType "spark"
487+
errored true
488+
childOf(span(0))
489+
}
490+
}
491+
}
492+
}
493+
460494
protected validateRelativeError(double value, double expected, double relativeAccuracy) {
461495
double relativeError = Math.abs(value - expected) / expected
462496
assert relativeError < relativeAccuracy

dd-java-agent/src/main/java/datadog/trace/bootstrap/AgentBootstrap.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.bootstrap;
22

3+
import static datadog.trace.bootstrap.SystemUtils.getPropertyOrEnvVar;
34
import static java.nio.charset.StandardCharsets.UTF_8;
45

56
import datadog.cli.CLIHelper;
@@ -44,8 +45,8 @@
4445
* </ul>
4546
*/
4647
public final class AgentBootstrap {
47-
static final String LIB_INJECTION_ENABLED_FLAG = "DD_INJECTION_ENABLED";
48-
static final String LIB_INJECTION_FORCE_FLAG = "DD_INJECT_FORCE";
48+
static final String LIB_INJECTION_ENABLED_ENV_VAR = "DD_INJECTION_ENABLED";
49+
static final String LIB_INJECTION_FORCE_SYS_PROP = "dd.inject.force";
4950

5051
private static final Class<?> thisClass = AgentBootstrap.class;
5152
private static final int MAX_EXCEPTION_CHAIN_LENGTH = 99;
@@ -155,11 +156,13 @@ private static void agentmainImpl(
155156

156157
static boolean getConfig(String configName) {
157158
switch (configName) {
158-
case LIB_INJECTION_ENABLED_FLAG:
159-
return System.getenv(LIB_INJECTION_ENABLED_FLAG) != null;
160-
case LIB_INJECTION_FORCE_FLAG:
161-
String libInjectionForceFlag = System.getenv(LIB_INJECTION_FORCE_FLAG);
162-
return "true".equalsIgnoreCase(libInjectionForceFlag) || "1".equals(libInjectionForceFlag);
159+
case LIB_INJECTION_ENABLED_ENV_VAR:
160+
return System.getenv(LIB_INJECTION_ENABLED_ENV_VAR) != null;
161+
case LIB_INJECTION_FORCE_SYS_PROP:
162+
{
163+
String injectionForceFlag = getPropertyOrEnvVar(LIB_INJECTION_FORCE_SYS_PROP);
164+
return "true".equalsIgnoreCase(injectionForceFlag) || "1".equals(injectionForceFlag);
165+
}
163166
default:
164167
return false;
165168
}
@@ -285,8 +288,9 @@ static int parseJavaMajorVersion(String version) {
285288

286289
static boolean shouldAbortDueToOtherJavaAgents() {
287290
// Simply considering having multiple agents
288-
if (getConfig(LIB_INJECTION_ENABLED_FLAG)
289-
&& !getConfig(LIB_INJECTION_FORCE_FLAG)
291+
292+
if (getConfig(LIB_INJECTION_ENABLED_ENV_VAR)
293+
&& !getConfig(LIB_INJECTION_FORCE_SYS_PROP)
290294
&& getAgentFilesFromVMArguments().size() > 1) {
291295
// Formatting agent file list, Java 7 style
292296
StringBuilder agentFiles = new StringBuilder();
@@ -305,7 +309,7 @@ && getAgentFilesFromVMArguments().size() > 1) {
305309
"Info: multiple JVM agents detected, found "
306310
+ agentFiles
307311
+ ". Loading multiple APM/Tracing agent is not a recommended or supported configuration."
308-
+ "Please set the DD_INJECT_FORCE configuration to TRUE to load Datadog APM/Tracing agent.");
312+
+ "Please set the environment variable DD_INJECT_FORCE or the system property dd.inject.force to TRUE to load Datadog APM/Tracing agent.");
309313
return true;
310314
}
311315
return false;

dd-java-agent/src/main/java/datadog/trace/bootstrap/SystemUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,16 @@ public static String getPropertyOrDefault(String property, String defaultValue)
3030
return defaultValue;
3131
}
3232
}
33+
34+
private static String toEnvVar(String string) {
35+
return string.replace('.', '_').replace('-', '_').toUpperCase();
36+
}
37+
38+
public static String getPropertyOrEnvVar(String property) {
39+
String envVarValue = System.getenv(toEnvVar(property));
40+
if (envVarValue != null) {
41+
return envVarValue;
42+
}
43+
return System.getProperty(property);
44+
}
3345
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package datadog.smoketest;
2+
3+
import java.util.List;
4+
5+
public class CrashTelemetryData extends MinimalTelemetryData {
6+
List<LogMessage> payload;
7+
8+
public static class LogMessage {
9+
public String message;
10+
public String level;
11+
public String tags;
12+
}
13+
}

0 commit comments

Comments
 (0)