-
Notifications
You must be signed in to change notification settings - Fork 334
Add Telemetry for Extension Finder SPIs #11298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 4 commits into
master
from
mhlidd/add_extension_finder_telemetry
May 8, 2026
+217
−0
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
...-agent/agent-installer/src/test/java/datadog/trace/agent/tooling/ExtensionFinderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
internal-api/src/main/java/datadog/trace/api/telemetry/OtelSpiCollector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.: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.