Skip to content

Commit 7352724

Browse files
committed
Defer metrics aggregator classloading to save startup time
1 parent cb4b55d commit 7352724

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/benchmark/StaticEventLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static String getAgentVersion() {
9797
for (int c = reader.read(); c != -1; c = reader.read()) {
9898
sb.append((char) c);
9999
}
100-
} catch (IOException e) {
100+
} catch (Throwable ignored) {
101101
// swallow exception
102102
return null;
103103
}

dd-trace-core/src/main/java/datadog/trace/common/metrics/NoOpMetricsAggregator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public final class NoOpMetricsAggregator implements MetricsAggregator {
1111

12-
static final NoOpMetricsAggregator INSTANCE = new NoOpMetricsAggregator();
12+
public static final NoOpMetricsAggregator INSTANCE = new NoOpMetricsAggregator();
1313

1414
@Override
1515
public void start() {}

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import datadog.trace.civisibility.interceptor.CiVisibilityTraceInterceptor;
7373
import datadog.trace.common.GitMetadataTraceInterceptor;
7474
import datadog.trace.common.metrics.MetricsAggregator;
75+
import datadog.trace.common.metrics.NoOpMetricsAggregator;
7576
import datadog.trace.common.sampling.Sampler;
7677
import datadog.trace.common.sampling.SingleSpanSampler;
7778
import datadog.trace.common.sampling.SpanSamplingRules;
@@ -182,7 +183,7 @@ public static CoreTracerBuilder builder() {
182183
/** Scope manager is in charge of managing the scopes from which spans are created */
183184
final ContinuableScopeManager scopeManager;
184185

185-
final MetricsAggregator metricsAggregator;
186+
volatile MetricsAggregator metricsAggregator;
186187

187188
/** Initial static configuration associated with the tracer. */
188189
final Config initialConfig;
@@ -783,14 +784,26 @@ private CoreTracer(
783784
pendingTraceBuffer.start();
784785

785786
sharedCommunicationObjects.whenReady(this.writer::start);
786-
787-
metricsAggregator =
788-
createMetricsAggregator(config, sharedCommunicationObjects, this.healthMetrics);
789-
// Schedule the metrics aggregator to begin reporting after a random delay of 1 to 10 seconds
790-
// (using milliseconds granularity.) This avoids a fleet of traced applications starting at the
791-
// same time from sending metrics in sync.
792-
AgentTaskScheduler.get()
793-
.scheduleWithJitter(MetricsAggregator::start, metricsAggregator, 1, SECONDS);
787+
// temporary assign a no-op instance. The final one will be resolved when the discovery will be
788+
// allowed
789+
metricsAggregator = NoOpMetricsAggregator.INSTANCE;
790+
final SharedCommunicationObjects sco = sharedCommunicationObjects;
791+
// asynchronously create the aggregator to avoid triggering expensive classloading during the
792+
// tracer initialisation.
793+
sharedCommunicationObjects.whenReady(
794+
() ->
795+
AgentTaskScheduler.get()
796+
.execute(
797+
() -> {
798+
metricsAggregator = createMetricsAggregator(config, sco, this.healthMetrics);
799+
// Schedule the metrics aggregator to begin reporting after a random delay of
800+
// 1 to 10 seconds (using milliseconds granularity.)
801+
// This avoids a fleet of traced applications starting at the same time from
802+
// sending metrics in sync.
803+
AgentTaskScheduler.get()
804+
.scheduleWithJitter(
805+
MetricsAggregator::start, metricsAggregator, 1, SECONDS);
806+
}));
794807

795808
if (dataStreamsMonitoring == null) {
796809
this.dataStreamsMonitoring =

0 commit comments

Comments
 (0)