-
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
Open
mhlidd
wants to merge
1
commit into
mhlidd/add_extensions_metric
Choose a base branch
from
mhlidd/add_extension_finder_telemetry
base: mhlidd/add_extensions_metric
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−0
Open
Changes from all commits
Commits
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; | ||
| } | ||
| } |
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.)