Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import datadog.instrument.utils.ClassLoaderValue;
import datadog.metrics.api.statsd.StatsDClientManager;
import datadog.trace.api.Config;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.Platform;
import datadog.trace.api.WithGlobalTracer;
import datadog.trace.api.appsec.AppSecEventTracker;
Expand Down Expand Up @@ -846,6 +847,17 @@ private static synchronized void installDatadogTracer(
initTelemetry.onFatalError(ex);
}

// OTLP runtime metrics start unconditionally when both DD_METRICS_OTEL_ENABLED
// and DD_RUNTIME_METRICS_ENABLED are set, regardless of whether the application
// imports the OTel API. The OTLP exporter (OtlpMetricsService) was just started
// by CoreTracer above; this registers the JVM metric callbacks with
// OtelMeterProvider so the periodic export has data to collect. Done here
// (not in startJmx, which is delayed 15s) so callbacks are registered before
// the exporter's first scheduled flush — otherwise short-lived tests miss data.
if (Config.get().isRuntimeMetricsEnabled() && InstrumenterConfig.get().isMetricsOtelEnabled()) {
startOtlpRuntimeMetrics();
}

StaticEventLogger.end("GlobalTracer");
}

Expand Down Expand Up @@ -989,6 +1001,27 @@ private static synchronized void initializeJmxSystemAccessProvider(
}
}

/**
* Registers OTLP runtime metric callbacks (JVM heap, CPU, threads, classes, etc.) with the
* agent's OtelMeterProvider. The periodic OTLP exporter started by CoreTracer then collects and
* exports them — this is the same pattern Node and .NET use to start their runtime metrics
* unconditionally during tracer init, independent of any app-side OTel API usage.
*/
private static synchronized void startOtlpRuntimeMetrics() {
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(AGENT_CLASSLOADER);
final Class<?> jvmOtlpClass =
AGENT_CLASSLOADER.loadClass("datadog.opentelemetry.shim.metrics.JvmOtlpRuntimeMetrics");
final Method startMethod = jvmOtlpClass.getMethod("start");
startMethod.invoke(null);
} catch (final Throwable ex) {
log.error("Throwable thrown while starting OTLP runtime metrics", ex);
} finally {
safelySetContextClassLoader(contextLoader);
}
}

private static synchronized void startJmxFetch() {
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,11 @@
"methods": [
{"name": "<init>", "parameterTypes": []}
]
},
{
"name": "datadog.trace.bootstrap.otel.shim.metrics.JvmOtlpRuntimeMetrics",
"methods": [
{"name": "start", "parameterTypes": []}
]
}
]
Loading