Skip to content

Commit 63f450e

Browse files
committed
Delay starting OkHttp, default to 1 second
1 parent 56d4919 commit 63f450e

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public class Agent {
7878
private static final String AGENT_INSTALLER_CLASS_NAME =
7979
"datadog.trace.agent.tooling.AgentInstaller";
8080

81+
private static final int DEFAULT_OKHTTP_DELAY = 1; // seconds
82+
8183
private static final int DEFAULT_JMX_START_DELAY = 15; // seconds
8284

8385
private static final Logger log;
@@ -329,7 +331,7 @@ public void run() {
329331
if (appUsingCustomJMXBuilder) {
330332
log.debug("Custom JMX builder detected. Delaying JMXFetch initialization.");
331333
registerMBeanServerBuilderCallback(new StartJmxCallback(jmxStartDelay));
332-
// one minute fail-safe in case nothing touches JMX and and callback isn't triggered
334+
// one minute fail-safe in case nothing touches JMX and callback isn't triggered
333335
scheduleJmxStart(60 + jmxStartDelay);
334336
} else if (appUsingCustomLogManager) {
335337
log.debug("Custom logger detected. Delaying JMXFetch initialization.");
@@ -339,20 +341,23 @@ public void run() {
339341
}
340342
}
341343

342-
boolean delayOkHttp = appUsingCustomLogManager && okHttpMayIndirectlyLoadJUL();
344+
boolean waitForJUL = appUsingCustomLogManager && okHttpMayIndirectlyLoadJUL();
343345

344346
/*
345347
* Similar thing happens with DatadogTracer on (at least) zulu-8 because it uses OkHttp which indirectly loads JFR
346348
* events which in turn loads LogManager. This is not a problem on newer JDKs because there JFR uses different
347349
* logging facility. Likewise on IBM JDKs OkHttp may indirectly load 'IBMSASL' which in turn loads LogManager.
348350
*/
351+
int okHttpStartDelay = getOkHttpStartDelay();
349352
InstallDatadogTracerCallback installDatadogTracerCallback =
350-
new InstallDatadogTracerCallback(initTelemetry, inst, delayOkHttp);
351-
if (delayOkHttp) {
353+
new InstallDatadogTracerCallback(initTelemetry, inst, okHttpStartDelay);
354+
if (waitForJUL) {
352355
log.debug("Custom logger detected. Delaying Datadog Tracer initialization.");
353356
registerLogManagerCallback(installDatadogTracerCallback);
357+
} else if (okHttpStartDelay > 0) {
358+
installDatadogTracerCallback.run(); // start OkHttp on separate thread
354359
} else {
355-
installDatadogTracerCallback.execute();
360+
installDatadogTracerCallback.execute(); // start OkHttp on this thread
356361
}
357362

358363
/*
@@ -362,7 +367,7 @@ public void run() {
362367
if (profilingEnabled && !isOracleJDK8()) {
363368
StaticEventLogger.begin("Profiling");
364369

365-
if (delayOkHttp) {
370+
if (waitForJUL) {
366371
log.debug("Custom logger detected. Delaying Profiling initialization.");
367372
registerLogManagerCallback(new StartProfilingAgentCallback(inst));
368373
} else {
@@ -499,18 +504,18 @@ protected static class InstallDatadogTracerCallback extends ClassLoadCallBack {
499504
private final Instrumentation instrumentation;
500505
private final Object sco;
501506
private final Class<?> scoClass;
502-
private final boolean delayOkHttp;
507+
private final int okHttpStartDelay;
503508

504509
public InstallDatadogTracerCallback(
505510
InitializationTelemetry initTelemetry,
506511
Instrumentation instrumentation,
507-
boolean delayOkHttp) {
508-
this.delayOkHttp = delayOkHttp;
512+
int okHttpStartDelay) {
513+
this.okHttpStartDelay = okHttpStartDelay;
509514
this.instrumentation = instrumentation;
510515
try {
511516
scoClass =
512517
AGENT_CLASSLOADER.loadClass("datadog.communication.ddagent.SharedCommunicationObjects");
513-
sco = scoClass.getConstructor(boolean.class).newInstance(delayOkHttp);
518+
sco = scoClass.getConstructor(boolean.class).newInstance(okHttpStartDelay > 0);
514519
} catch (ClassNotFoundException
515520
| NoSuchMethodException
516521
| InstantiationException
@@ -530,7 +535,7 @@ public AgentThread agentThread() {
530535

531536
@Override
532537
public void execute() {
533-
if (delayOkHttp) {
538+
if (okHttpStartDelay > 0) {
534539
resumeRemoteComponents();
535540
}
536541

@@ -550,7 +555,7 @@ private void resumeRemoteComponents() {
550555
try {
551556
// remote components were paused for custom log-manager/jmx-builder
552557
// add small delay before resuming remote I/O to help stabilization
553-
Thread.sleep(1_000);
558+
Thread.sleep(okHttpStartDelay * 1_000L);
554559
scoClass.getMethod("resume").invoke(sco);
555560
} catch (InterruptedException ignore) {
556561
} catch (Throwable e) {
@@ -1244,6 +1249,19 @@ private static String getNullIfEmpty(final String value) {
12441249
return value;
12451250
}
12461251

1252+
/** @return configured OkHttp start delay in seconds */
1253+
private static int getOkHttpStartDelay() {
1254+
String startDelay = ddGetProperty("dd.okhttp.start-delay");
1255+
if (startDelay != null) {
1256+
try {
1257+
return Integer.parseInt(startDelay);
1258+
} catch (NumberFormatException e) {
1259+
// fall back to default delay
1260+
}
1261+
}
1262+
return DEFAULT_OKHTTP_DELAY;
1263+
}
1264+
12471265
/** @return configured JMX start delay in seconds */
12481266
private static int getJmxStartDelay() {
12491267
String startDelay = ddGetProperty("dd.dogstatsd.start-delay");

dd-trace-api/src/main/java/datadog/trace/api/ConfigDefaults.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public final class ConfigDefaults {
9090

9191
static final boolean DEFAULT_TRACE_PROPAGATION_EXTRACT_FIRST = false;
9292

93+
static final int DEFAULT_OKHTTP_START_DELAY = 1; // seconds
94+
9395
static final boolean DEFAULT_JMX_FETCH_MULTIPLE_RUNTIME_SERVICES_ENABLED = false;
9496
static final int DEFAULT_JMX_FETCH_MULTIPLE_RUNTIME_SERVICES_LIMIT = 10;
9597

dd-trace-api/src/main/java/datadog/trace/api/config/GeneralConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public final class GeneralConfig {
3737

3838
public static final String STARTUP_LOGS_ENABLED = "trace.startup.logs";
3939

40+
public static final String OKHTTP_START_DELAY = "okhttp.start-delay";
41+
4042
public static final String DOGSTATSD_START_DELAY = "dogstatsd.start-delay";
4143
public static final String DOGSTATSD_HOST = "dogstatsd.host";
4244
public static final String DOGSTATSD_PORT = "dogstatsd.port";

internal-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public static String getHostName() {
191191
private final boolean tracePropagationExtractFirst;
192192
private final int clockSyncPeriod;
193193
private final boolean logsInjectionEnabled;
194+
private final int okHttpStartDelay;
194195

195196
private final String dogStatsDNamedPipe;
196197
private final int dogStatsDStartDelay;
@@ -1005,6 +1006,8 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins
10051006
configProvider.getBoolean(
10061007
LOGS_INJECTION_ENABLED, DEFAULT_LOGS_INJECTION_ENABLED, LOGS_INJECTION);
10071008

1009+
okHttpStartDelay = configProvider.getInteger(OKHTTP_START_DELAY, DEFAULT_OKHTTP_START_DELAY);
1010+
10081011
dogStatsDNamedPipe = configProvider.getString(DOGSTATSD_NAMED_PIPE);
10091012

10101013
dogStatsDStartDelay =
@@ -2220,6 +2223,10 @@ public int getClockSyncPeriod() {
22202223
return clockSyncPeriod;
22212224
}
22222225

2226+
public int getOkHttpStartDelay() {
2227+
return okHttpStartDelay;
2228+
}
2229+
22232230
public String getDogStatsDNamedPipe() {
22242231
return dogStatsDNamedPipe;
22252232
}
@@ -4345,6 +4352,8 @@ public String toString() {
43454352
+ tracePropagationExtractFirst
43464353
+ ", clockSyncPeriod="
43474354
+ clockSyncPeriod
4355+
+ ", okHttpStartDelay="
4356+
+ okHttpStartDelay
43484357
+ ", jmxFetchEnabled="
43494358
+ jmxFetchEnabled
43504359
+ ", dogStatsDStartDelay="

0 commit comments

Comments
 (0)