@@ -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" );
0 commit comments