Skip to content

JUnit4 suite-level TestExecutionListener callbacks see no active span under Bazel #11886

Description

@mboos

Tracer Version(s)

1.63.1, 1.62.0

Java Version(s)

21.0.6

JVM Vendor

Azul Zing / Zulu

Bug Report

We attach custom test metadata to CI Visibility spans via a org.junit.platform.launcher.TestExecutionListener registered on the JUnit Platform Launcher (tried both META-INF/services auto-registration and explicit registerTestExecutionListeners()). This listener calls GlobalTracer.get().activeSpan() inside executionStarted/executionFinished to tag the currently-active span.

For JUnit4 tests bridged through junit-vintage-engine:

Method-level spans: activeSpan() resolves correctly and our tags apply.
Suite-level (container) spans, under Bazel: activeSpan() is reliably null at both executionStarted and executionFinished, even though the suite span itself is still created and reported to Datadog correctly (correct duration/status/etc — just without our tags).
We have a separate, non-Bazel test runner that invokes the exact same JUnit Platform Launcher API directly (LauncherFactory.openSession(), explicit listener registration, selectClass(...)) and registers an equivalent listener doing the same activeSpan() lookup — and it successfully tags suite-level spans in production. So this isn't a general limitation of the plain Launcher API path; something about the Bazel environment specifically prevents the suite span from being visible as "active" to an external TestExecutionListener.

What we've ruled out
We suspected a version regression between 1.62.0 and 1.63.1 (a bytecode diff showed 1.63.1 removed a lazy-suite-autostart fallback from JUnit4TracingListener that existed in 1.62.0, landing in the same PR that added Bazel-wrapper-specific suite-event handling). We tested this directly: swapped Bazel's agent from 1.63.1 down to 1.62.0 for the same test class, same Bazel launch setup. Result: activeSpan() is still null for the suite container on 1.62.0 too.

Expected Behavior

A suite-level TestExecutionListener callback should be able to observe the active suite span via GlobalTracer.get().activeSpan() under Bazel, consistent with how it behaves for the same JUnit Platform Launcher API used outside Bazel.

Reproduction Code

  1. A JUnit4 test class run via junit-vintage-engine under the JUnit Platform Launcher, under Bazel, with a custom main_class = "org.junit.platform.console.ConsoleLauncher" (i.e., not Bazel's native BazelTestRunner/RunNotifierWrapper).
  2. Register a TestExecutionListener that calls GlobalTracer.get().activeSpan() inside executionStarted/executionFinished for container-type TestIdentifiers.
  3. Observed on both dd-java-agent 1.62.0 and 1.63.1: activeSpan() is null for the suite container, both at start and finish.
  4. The equivalent pattern (plain Launcher API, explicit listener registration) works outside Bazel in our environment, on 1.62.0.

Activity

  1. daniel-mohedano commented on Jul 9, 2026

    @daniel-mohedano
    Contributor

    Hey @mboos, thanks for the report!

    After taking a look and reproducing locally, I believe the issue is not Bazel-specific, but rather related to running the test class through junit-vintage-engine. For the VintageTestEngine the suite span is created and closed entirely inside ParentRunner.run(), while the vintage engine's RunListenerAdapter fires the container's platform-level executionStarted/executionFinished around that run() call. This explains why the activeSpan() returns null inside your listener for the suite/container callbacks while the method-level ones still see the active span. For native JUnit 5 (Jupiter), we have a separate instrumentation that activates the suite span for the platform executionStarted event, so it's visible there.

    Could you confirm if your working non-Bazel runner is Jupiter rather than JUnit4-vintage? That would confirm the engine is the differentiator here.

  2. mboos commented on Jul 9, 2026

    @mboos
    Author

    The non-bazel runner is using the org.junit.platform.launcher.Launcher API, which I believe uses both the Jupiter and vintage engines, depending on the test type (we have both). When the same junit4 tests is run with our custom runner rather than Bazel, the class-level listener hooks can see the suite span.

    When we try to write our own console launcher with that same API under Bazel (in place of org.junit.platform.console.ConsoleLauncher, we still can't tag the suite spans.

  3. daniel-mohedano commented on Jul 13, 2026

    @daniel-mohedano
    Contributor

    I see. Would you be able to share a minimal reproducible project for the issue? That'd be very helpful on my side to debug it. In particular I'm interested in how the launcher and listeners are implemented and built/registered, as well as how you run them in both environments (the non-Bazel invocation with the tagging working and the Bazel setup that interferes with it). If they're not obvious from the project, any JVM/DD_CIVISIBILITY_ tags used in each case and bazel configuration would help too. Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions