Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -3,6 +3,7 @@
import static datadog.opentelemetry.tooling.OtelExtensionHandler.OPENTELEMETRY;
import static datadog.trace.agent.tooling.ExtensionHandler.DATADOG;

import datadog.trace.api.telemetry.OtelSpiCollector;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -13,6 +14,7 @@
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand All @@ -26,6 +28,11 @@ public final class ExtensionFinder {

private static final ExtensionHandler[] handlers = {OPENTELEMETRY, DATADOG};

private static final String EXTENSIONS_PATH_SOURCE = "extensions_path";

private static final String SERVICES_PREFIX = "META-INF/services/";
private static final String OTEL_NAMESPACE = "io.opentelemetry.";

/**
* Discovers extensions on the configured path and creates a classloader for each extension.
* Registers the combined classloader with {@link Utils#setExtendedClassLoader(ClassLoader)}.
Expand All @@ -40,6 +47,7 @@ public static boolean findExtensions(String extensionsPath, Class<?>... extensio
String[] descriptors = descriptors(extensionTypes);

for (JarFile jar : findExtensionJars(extensionsPath)) {
recordOtelSpiTelemetry(jar);
URL extensionURL = findExtensionURL(jar, descriptors);
if (null != extensionURL) {
log.debug("Found extension jar {}", jar.getName());
Expand All @@ -60,6 +68,24 @@ public static boolean findExtensions(String extensionsPath, Class<?>... extensio
return !classLoaders.isEmpty();
}

/**
* Reports telemetry for any OpenTelemetry SPI service descriptors present in the jar — any entry
* under {@code META-INF/services/} whose name lives in the {@code io.opentelemetry.*} namespace.
* The jar's existing handle is reused; no new file resources are opened or held.
*/
static void recordOtelSpiTelemetry(JarFile jar) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.startsWith(SERVICES_PREFIX)) {
String fqn = name.substring(SERVICES_PREFIX.length());
if (fqn.startsWith(OTEL_NAMESPACE)) {
OtelSpiCollector.getInstance().recordSpiDetected(fqn, EXTENSIONS_PATH_SOURCE);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we exit here do avoid looping on entries if we already found?

Copy link
Copy Markdown
Contributor

@mcculls mcculls May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal here is to report all service files under the OTel namespace then the best we could do is exit on the first entry that doesn't start with the services prefix. That's assuming that the service entries appear in a group, which they usually do. If assume that the service entries are ordered then we could exit on the next entry that doesn't start with the OTel namespace.

However I'd like to know about what we intend to gain from this telemetry since this will incur a non-trivial startup cost, albeit only when extensions are added to the tracer (which is uncommon.)

i.e. if the goal is to survey what SPIs are being added as extensions, even though we don't currently support them then I guess this is the only way to discover that (although it would be a good idea to short-circuit the searching when we get to jar entries that we know should appear after service entries - you can look at some example OTel extension jars to see if there's a pattern.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the goal is to survey what SPIs are being added as extensions, even though we don't currently support them

This is exactly the goal here. For the purpose of having information of what SPIs are commonly being used even though we don't support them yet.

Copy link
Copy Markdown
Contributor Author

@mhlidd mhlidd May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: See comment below. TLDR scanning from a static list instead of iterating through all jars.
@mcculls I had Claude go thru some sample OTel extension jar files from Maven Central and it found that Services entries are contiguous, but custom services entries can be shoved in-between OTel service entries.

e.g.:

[1357] META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider
[1358] META-INF/services/co.elastic.otel.common.ChainingSpanProcessorAutoConfiguration
[1359] META-INF/services/io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule

Generally, it appears that JAR files will write all service entries contiguously, but that's technically not guarnateed. IMO, some data is better than no data, so I'm fine w/ reading until the end of service entries, and quitting early. I'll implement this change here.

}
}
}
}

/** Closes jar resources from the extension path which did not contain any extensions. */
private static void close(List<JarFile> unusedJars) {
for (JarFile jar : unusedJars) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package datadog.trace.agent.tooling;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import datadog.trace.api.telemetry.OtelSpiCollector;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class ExtensionFinderTest {

private static final String AUTOCONFIGURE_PROPAGATOR =
"io.opentelemetry.sdk.autoconfigure.spi.ConfigurablePropagatorProvider";
private static final String AUTOCONFIGURE_RESOURCE =
"io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider";
private static final String AUTOCONFIGURE_SAMPLER =
"io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSamplerProvider";
private static final String AUTOCONFIGURE_EXPORTER =
"io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider";
private static final String JAVAAGENT_INSTRUMENTATION_MODULE =
"io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule";
private static final String JAVAAGENT_AGENT_LISTENER =
"io.opentelemetry.javaagent.extension.AgentListener";
private static final String SHADED_AUTOCONFIGURE_SAMPLER =
"io.opentelemetry.javaagent.shaded.io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSamplerProvider";

private final OtelSpiCollector collector = OtelSpiCollector.getInstance();

@BeforeEach
public void clearCollector() {
collector.drain();
}

@Test
public void singleOtelSpiIsReported(@TempDir Path tempDir) throws IOException {
Path jarPath = buildJar(tempDir, "ext.jar", AUTOCONFIGURE_PROPAGATOR);

try (JarFile jar = new JarFile(jarPath.toFile(), false)) {
ExtensionFinder.recordOtelSpiTelemetry(jar);
}

Collection<OtelSpiCollector.OtelSpiMetric> drained = collector.drain();
assertEquals(1, drained.size());
OtelSpiCollector.OtelSpiMetric metric = drained.iterator().next();
assertEquals("otel.spi.detected", metric.metricName);
assertTrue(metric.tags.contains("spi_class:" + AUTOCONFIGURE_PROPAGATOR));
assertTrue(metric.tags.contains("source:extensions_path"));
}

@Test
public void allFourAutoconfigureSpisAreReported(@TempDir Path tempDir) throws IOException {
Path jarPath =
buildJar(
tempDir,
"ext.jar",
AUTOCONFIGURE_PROPAGATOR,
AUTOCONFIGURE_RESOURCE,
AUTOCONFIGURE_SAMPLER,
AUTOCONFIGURE_EXPORTER);

try (JarFile jar = new JarFile(jarPath.toFile(), false)) {
ExtensionFinder.recordOtelSpiTelemetry(jar);
}

assertEquals(
new HashSet<>(
java.util.Arrays.asList(
AUTOCONFIGURE_PROPAGATOR,
AUTOCONFIGURE_RESOURCE,
AUTOCONFIGURE_SAMPLER,
AUTOCONFIGURE_EXPORTER)),
reportedFqns(collector.drain()));
}

@Test
public void javaagentExtensionSpisAreReported(@TempDir Path tempDir) throws IOException {
Path jarPath =
buildJar(tempDir, "ext.jar", JAVAAGENT_INSTRUMENTATION_MODULE, JAVAAGENT_AGENT_LISTENER);

try (JarFile jar = new JarFile(jarPath.toFile(), false)) {
ExtensionFinder.recordOtelSpiTelemetry(jar);
}

assertEquals(
new HashSet<>(
java.util.Arrays.asList(JAVAAGENT_INSTRUMENTATION_MODULE, JAVAAGENT_AGENT_LISTENER)),
reportedFqns(collector.drain()));
}

@Test
public void shadedJavaagentSpiIsReported(@TempDir Path tempDir) throws IOException {
Path jarPath = buildJar(tempDir, "ext.jar", SHADED_AUTOCONFIGURE_SAMPLER);

try (JarFile jar = new JarFile(jarPath.toFile(), false)) {
ExtensionFinder.recordOtelSpiTelemetry(jar);
}

Collection<OtelSpiCollector.OtelSpiMetric> drained = collector.drain();
assertEquals(1, drained.size());
assertTrue(
drained.iterator().next().tags.contains("spi_class:" + SHADED_AUTOCONFIGURE_SAMPLER));
}

@Test
public void nonOtelSpiIsIgnored(@TempDir Path tempDir) throws IOException {
Path jarPath =
buildJar(
tempDir,
"ext.jar",
"com.example.MyService",
"org.springframework.context.ApplicationContextInitializer",
"java.sql.Driver");

try (JarFile jar = new JarFile(jarPath.toFile(), false)) {
ExtensionFinder.recordOtelSpiTelemetry(jar);
}

assertEquals(0, collector.drain().size());
}

@Test
public void jarWithoutAnyServiceDescriptorsEmitsNothing(@TempDir Path tempDir)
throws IOException {
Path jarPath = tempDir.resolve("empty.jar");
try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jarPath))) {
jos.putNextEntry(new JarEntry("README.txt"));
jos.write("not an extension".getBytes());
jos.closeEntry();
}

try (JarFile jar = new JarFile(jarPath.toFile(), false)) {
ExtensionFinder.recordOtelSpiTelemetry(jar);
}

assertEquals(0, collector.drain().size());
}

@Test
public void mixedOtelAndNonOtelReportsOnlyOtel(@TempDir Path tempDir) throws IOException {
Path jarPath =
buildJar(
tempDir,
"ext.jar",
AUTOCONFIGURE_PROPAGATOR,
"com.example.MyService",
JAVAAGENT_AGENT_LISTENER,
"java.sql.Driver");

try (JarFile jar = new JarFile(jarPath.toFile(), false)) {
ExtensionFinder.recordOtelSpiTelemetry(jar);
}

assertEquals(
new HashSet<>(java.util.Arrays.asList(AUTOCONFIGURE_PROPAGATOR, JAVAAGENT_AGENT_LISTENER)),
reportedFqns(collector.drain()));
}

private static Set<String> reportedFqns(Collection<OtelSpiCollector.OtelSpiMetric> drained) {
Set<String> fqns = new HashSet<>();
for (OtelSpiCollector.OtelSpiMetric metric : drained) {
for (String tag : metric.tags) {
if (tag.startsWith("spi_class:")) {
fqns.add(tag.substring("spi_class:".length()));
}
}
}
return fqns;
}

/** Builds a jar with empty {@code META-INF/services/<fqn>} entries for each given FQN. */
private static Path buildJar(Path dir, String name, String... serviceFqns) throws IOException {
Path jarPath = dir.resolve(name);
try (OutputStream out = Files.newOutputStream(jarPath);
JarOutputStream jos = new JarOutputStream(out)) {
for (String fqn : serviceFqns) {
jos.putNextEntry(new JarEntry("META-INF/services/" + fqn));
jos.closeEntry();
}
}
return jarPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static void onEnter(@Advice.Argument(value = 0, readOnly = false) String[
+ "datadog.trace.api.telemetry.ConfigInversionMetricCollectorImpl$ConfigInversionMetric:build_time,"
+ "datadog.trace.api.telemetry.NoOpConfigInversionMetricCollector:build_time,"
+ "datadog.trace.api.telemetry.OtelEnvMetricCollectorImpl:build_time,"
+ "datadog.trace.api.telemetry.OtelSpiCollector:build_time,"
+ "datadog.trace.api.profiling.ProfilingEnablement:build_time,"
+ "datadog.trace.bootstrap.config.provider.ConfigConverter:build_time,"
+ "datadog.trace.bootstrap.config.provider.ConfigConverter$ValueOfLookup:build_time,"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package datadog.trace.api.telemetry;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Collects telemetry about OpenTelemetry SPIs detected in the customer environment. */
public class OtelSpiCollector implements MetricCollector<OtelSpiCollector.OtelSpiMetric> {
private static final Logger log = LoggerFactory.getLogger(OtelSpiCollector.class);
private static final String OTEL_SPI_DETECTED_METRIC_NAME = "otel.spi.detected";
private static final String SPI_CLASS_TAG = "spi_class:";
private static final String SOURCE_TAG = "source:";
private static final String NAMESPACE = "tracers";
private static final OtelSpiCollector INSTANCE = new OtelSpiCollector();

private final BlockingQueue<OtelSpiMetric> metricsQueue;

private OtelSpiCollector() {
this.metricsQueue = new ArrayBlockingQueue<>(RAW_QUEUE_SIZE);
}

public static OtelSpiCollector getInstance() {
return INSTANCE;
}

public void recordSpiDetected(String spiFqn, String source) {
if (!metricsQueue.offer(
new OtelSpiMetric(
NAMESPACE,
true,
OTEL_SPI_DETECTED_METRIC_NAME,
"count",
1,
SPI_CLASS_TAG + spiFqn,
SOURCE_TAG + source))) {
log.debug(
"Unable to add telemetry metric {} for spi_class={} source={}",
OTEL_SPI_DETECTED_METRIC_NAME,
spiFqn,
source);
}
}

@Override
public void prepareMetrics() {
// Nothing to do here
}

@Override
public Collection<OtelSpiMetric> drain() {
if (this.metricsQueue.isEmpty()) {
return Collections.emptyList();
}
List<OtelSpiMetric> drained = new ArrayList<>(this.metricsQueue.size());
this.metricsQueue.drainTo(drained);
return drained;
}

public static class OtelSpiMetric extends MetricCollector.Metric {
public OtelSpiMetric(
String namespace,
boolean common,
String metricName,
String type,
Number value,
final String... tags) {
super(namespace, common, metricName, type, value, tags);
}
}
}
Loading
Loading