Skip to content

detect inferred spans on tracer name#1097

Merged
SylvainJuge merged 5 commits into
elastic:mainfrom
SylvainJuge:inferred-spans_remove-attribute
May 7, 2026
Merged

detect inferred spans on tracer name#1097
SylvainJuge merged 5 commits into
elastic:mainfrom
SylvainJuge:inferred-spans_remove-attribute

Conversation

@SylvainJuge
Copy link
Copy Markdown
Member

@SylvainJuge SylvainJuge commented May 6, 2026

In open-telemetry/opentelemetry-java-contrib#2803 in contrib, we use the tracer name to filter the inferred spans.

In EDOT, we need to implement the same heuristic for consistency, also it allows to avoid relying on the internal, out of semconv and undocumented is_inferred span attribute.

Instead, we rely on the tracer name which is expected to be stable and is equal to inferred-spans for inferred spans.

This is mostly a refactor that anticipates that the is_inferred span attribute might change (or be removed) in the future, there is no visible change in behavior for end-users.

@SylvainJuge SylvainJuge marked this pull request as ready for review May 6, 2026 18:36
@SylvainJuge SylvainJuge requested a review from a team as a code owner May 6, 2026 18:36
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 6, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR refactors inferred span detection from checking a boolean span attribute to using the instrumentation scope name. A new SpanUtils.isInferredSpan() utility checks whether a span's scope name equals "inferred-spans". The IS_INFERRED constant is removed from ElasticAttributes. Production code (LocalRootSpan, SpanStackTraceFilter) and tests are updated to call the new utility and create inferred spans using tracers with InferredSpansProcessor.TRACER_NAME instead of setting attributes. Build dependencies are added for inferred spans testing, and integration test assertions now validate instrumentation scope information.

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • 🛠️ Update Documentation: Commit on current branch
  • 🛠️ Update Documentation: Create PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread common/build.gradle.kts
testImplementation("io.opentelemetry:opentelemetry-api-incubator")

// only to get reference to upstream inferred tracer name
testCompileOnly(libs.contribInferredSpans);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

for reviewer: only this line is added, other changes are just reformatting.


import io.opentelemetry.sdk.trace.ReadableSpan;

public class InferredSpanDetector {
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.

do we really need a separated class just for this static method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I haven't found any other utility class with static methods where to put this. Do you think it would be better to rename the class to SpanUtil to suggest that it may be extended with other span-related code in the future ?

jackshirazi
jackshirazi previously approved these changes May 7, 2026
Copy link
Copy Markdown
Contributor

@jackshirazi jackshirazi left a comment

Choose a reason for hiding this comment

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

approved, but note comment

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@testing/integration-tests/inferred-spans-test/src/test/java/InferredSpansTest.java`:
- Around line 47-50: The test currently builds inferredSpansScope with
InstrumentationScopeInfo.builder("inferred-spans").setVersion(System.getProperty("test.contrib-inferred-spans.version"))
which can pass a null version when the system property is missing; change this
to read the property into a local variable, validate it is non-null/non-empty
(fail fast with a clear AssertionError or IllegalStateException mentioning
"test.contrib-inferred-spans.version" and the test name InferredSpansTest), and
only call setVersion(...) when the value is present so the test fails
immediately with a clear message instead of producing a scope with a null
version that causes confusing assertion mismatches.
- Around line 47-50: The InstrumentationScopeInfo builder is being given a
potentially null version via
System.getProperty("test.contrib-inferred-spans.version"), causing assertion
mismatches; change the inferredSpansScope construction to supply a non-null
default (e.g., use
java.util.Objects.requireNonNullElse(System.getProperty("test.contrib-inferred-spans.version"),
"") or similar) when calling setVersion so the builder never receives null, and
add the import for java.util.Objects; keep the rest of the
InstrumentationScopeInfo.builder("inferred-spans") call unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 8bb406e8-7a77-478d-a15a-a9f6909304d0

📥 Commits

Reviewing files that changed from the base of the PR and between 3ca94a0 and 3d64867.

📒 Files selected for processing (11)
  • common/build.gradle.kts
  • common/src/main/java/co/elastic/otel/common/ElasticAttributes.java
  • common/src/main/java/co/elastic/otel/common/LocalRootSpan.java
  • common/src/main/java/co/elastic/otel/common/SpanUtils.java
  • common/src/test/java/co/elastic/otel/common/LocalRootSpanTest.java
  • common/src/test/java/co/elastic/otel/common/SpanUtilsTest.java
  • custom/src/main/java/co/elastic/otel/SpanStackTraceFilter.java
  • custom/src/test/java/co/elastic/otel/SpanStackTraceFilterTest.java
  • custom/src/test/java/co/elastic/otel/SpanStackTraceTest.java
  • testing/integration-tests/inferred-spans-test/build.gradle.kts
  • testing/integration-tests/inferred-spans-test/src/test/java/InferredSpansTest.java
💤 Files with no reviewable changes (1)
  • common/src/main/java/co/elastic/otel/common/ElasticAttributes.java

Comment on lines +47 to +50
private static final InstrumentationScopeInfo inferredSpansScope =
InstrumentationScopeInfo.builder("inferred-spans")
.setVersion(System.getProperty("test.contrib-inferred-spans.version"))
.build();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

setVersion(null) silently degrades the scope assertion when run outside Gradle.

If test.contrib-inferred-spans.version is absent (e.g., IDE run), System.getProperty(...) returns null, producing a scope with a null version. The hasInstrumentationScopeInfo assertion on line 95 will then mismatch against the real span's non-null version — the test fails with no clear error about the missing property.

🛡️ Proposed fix: fail fast with a clear message
+import java.util.Objects;
 ...
-  private static final InstrumentationScopeInfo inferredSpansScope =
-      InstrumentationScopeInfo.builder("inferred-spans")
-          .setVersion(System.getProperty("test.contrib-inferred-spans.version"))
-          .build();
+  private static final InstrumentationScopeInfo inferredSpansScope =
+      InstrumentationScopeInfo.builder("inferred-spans")
+          .setVersion(
+              Objects.requireNonNull(
+                  System.getProperty("test.contrib-inferred-spans.version"),
+                  "System property 'test.contrib-inferred-spans.version' must be set"))
+          .build();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private static final InstrumentationScopeInfo inferredSpansScope =
InstrumentationScopeInfo.builder("inferred-spans")
.setVersion(System.getProperty("test.contrib-inferred-spans.version"))
.build();
private static final InstrumentationScopeInfo inferredSpansScope =
InstrumentationScopeInfo.builder("inferred-spans")
.setVersion(
Objects.requireNonNull(
System.getProperty("test.contrib-inferred-spans.version"),
"System property 'test.contrib-inferred-spans.version' must be set"))
.build();
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@testing/integration-tests/inferred-spans-test/src/test/java/InferredSpansTest.java`
around lines 47 - 50, The test currently builds inferredSpansScope with
InstrumentationScopeInfo.builder("inferred-spans").setVersion(System.getProperty("test.contrib-inferred-spans.version"))
which can pass a null version when the system property is missing; change this
to read the property into a local variable, validate it is non-null/non-empty
(fail fast with a clear AssertionError or IllegalStateException mentioning
"test.contrib-inferred-spans.version" and the test name InferredSpansTest), and
only call setVersion(...) when the value is present so the test fails
immediately with a clear message instead of producing a scope with a null
version that causes confusing assertion mismatches.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

setVersion(null) will cause assertion mismatches when the system property is absent.

If test.contrib-inferred-spans.version is not set (e.g., running the test directly from an IDE or a misconfigured CI job), System.getProperty(...) returns null, so inferredSpansScope is built with a null version. The hasInstrumentationScopeInfo assertion on line 95 then compares against a scope whose version is null, which won't match the real span's non-null scope version — silently failing the test in every non-Gradle environment.

A null-safe guard keeps the intent clear:

🛡️ Proposed fix
-  private static final InstrumentationScopeInfo inferredSpansScope =
-      InstrumentationScopeInfo.builder("inferred-spans")
-          .setVersion(System.getProperty("test.contrib-inferred-spans.version"))
-          .build();
+  private static final String INFERRED_SPANS_VERSION =
+      Objects.requireNonNull(
+          System.getProperty("test.contrib-inferred-spans.version"),
+          "System property 'test.contrib-inferred-spans.version' must be set");
+
+  private static final InstrumentationScopeInfo inferredSpansScope =
+      InstrumentationScopeInfo.builder("inferred-spans")
+          .setVersion(INFERRED_SPANS_VERSION)
+          .build();

Also add import java.util.Objects; to the imports.

Does io.opentelemetry.sdk.common.InstrumentationScopeInfo.Builder.setVersion(null) throw NullPointerException or silently store null?
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@testing/integration-tests/inferred-spans-test/src/test/java/InferredSpansTest.java`
around lines 47 - 50, The InstrumentationScopeInfo builder is being given a
potentially null version via
System.getProperty("test.contrib-inferred-spans.version"), causing assertion
mismatches; change the inferredSpansScope construction to supply a non-null
default (e.g., use
java.util.Objects.requireNonNullElse(System.getProperty("test.contrib-inferred-spans.version"),
"") or similar) when calling setVersion so the builder never receives null, and
add the import for java.util.Objects; keep the rest of the
InstrumentationScopeInfo.builder("inferred-spans") call unchanged.

@SylvainJuge SylvainJuge merged commit be5229e into elastic:main May 7, 2026
23 checks passed
@SylvainJuge SylvainJuge deleted the inferred-spans_remove-attribute branch May 7, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants