GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][test] Fix flaky OpenTelemetryServiceTest.testJvmRuntimeMetrics by merlimat · Pull Request #27565 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Motivation

OpenTelemetryServiceTest.testJvmRuntimeMetrics passed only on its retry in 5 CI runs between 2026-11-06 and 2026-11-10 (CI - Unit - Other), two of them scheduled master runs. The first attempt fails within 0.1–0.4 s:

java.lang.AssertionError:
Expecting any element of:
  [ImmutableMetricData{resource=Resource{...

The metric that is missing is jvm.gc.duration. It is the only JVM runtime metric that isn't read from an MXBean when the metrics are collected. RuntimeTelemetry's GarbageCollector adds a NotificationListener to every GC MXBean and records the histogram in handleNotification. The JVM queues the GC notification when a collection ends, and its notification thread delivers it later. The test collected the metrics as soon as Runtime.getRuntime().gc() returned:

for (int i = 0; i < 100; i++) {
    byte[] waste = new byte[1024 * 1024]; // 1MB
}
Runtime.getRuntime().gc();

var metrics = reader.collectAllMetrics();
...
assertThat(metrics).anySatisfy(metric -> assertThat(metric).hasName("jvm.gc.duration"));

On a busy runner the notification thread may not have run by then. The histogram has no points yet, so the reader doesn't export it.

Reproduced locally with a scratch test, not included in this PR. It adds a GC listener before the service's that holds the notification thread for 2 s, then runs testJvmRuntimeMetrics. The unchanged test fails every time, in 55 ms, with the CI signature. The collected metrics contain every other JVM metric the test checks (jvm.cpu.time, jvm.memory.*, jvm.buffer.*, jvm.thread.count, jvm.class.*), and jvm.gc.duration is missing:

error: java.lang.AssertionError: Expected MetricData to have name <jvm.gc.duration> but found <jvm.class.count>

Modifications

Before triggering the GC, the test adds its own listener for GC notifications to every GC MXBean, with the same notification type filter that RuntimeTelemetry uses. It waits for that listener to be called, then collects the metrics:

var gcNotified = new CountDownLatch(1);
NotificationListener gcListener = (notification, handback) -> gcNotified.countDown();
NotificationFilter gcFilter = notification ->
        notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION);
var gcEmitters = ManagementFactory.getGarbageCollectorMXBeans().stream()
        .filter(NotificationEmitter.class::isInstance)
        .map(NotificationEmitter.class::cast)
        .toList();
gcEmitters.forEach(emitter -> emitter.addNotificationListener(gcListener, gcFilter, null));
try {
    // (the existing GC trigger, unchanged)
    Runtime.getRuntime().gc();
    assertThat(gcNotified.await(30, TimeUnit.SECONDS)).as("GC notification delivered").isTrue();
} finally {
    for (NotificationEmitter emitter : gcEmitters) {
        emitter.removeNotificationListener(gcListener);
    }
}

var metrics = reader.collectAllMetrics();

The wait is on the event the metric depends on, not on a timeout. The service adds its listeners when it is built in setup(), so they come before the test's. A GC MXBean (sun.management.NotificationEmitterSupport.sendNotification) calls its listeners one after another, on the notification thread, in the order they were added. Once the test's listener has seen a notification, the service's listener has already recorded that GC. The latch also makes that recording visible to the test thread. The other assertions are unchanged.

Verifying this change

  • With the scratch repro holding the notification thread for 2 s, the fixed test waits for the notification and passes (2061 ms). The unchanged test fails in 55 ms.
  • ./gradlew :pulsar-opentelemetry:test --tests org.apache.pulsar.opentelemetry.OpenTelemetryServiceTest -PtestRetryCount=0 --rerun passed 5 times in a row (all 9 methods; testJvmRuntimeMetrics takes 25–60 ms).
  • checkstyleTest and spotlessCheck pass for pulsar-opentelemetry.
  • A clean local run is weak evidence for a timing fix. The repro above holds the thread on purpose to show the race.

Does this pull request potentially affect one of the following parts:

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
merlimat added 2 commits November 10, 2026 11:53
@merlimat
[fix][test] Fix flaky OpenTelemetryServiceTest.testJvmRuntimeMetrics
dc20c54
jvm.gc.duration is recorded by RuntimeTelemetry's listener for the JVM's
GC notifications, which the JVM delivers on its notification thread after
the collection ends. The test collected the metrics as soon as
Runtime.gc() returned, so when that thread had not run yet the histogram
had no points and jvm.gc.duration was missing.

The test now adds its own GC notification listener after the service's
and waits for it before collecting. A GC MXBean calls its listeners in
the order they were added, on one thread, so once the test's listener
has run the service has recorded that collection.
@merlimat
Merge remote-tracking branch 'apache/master' into wt/otel-jvm-runtime…
79f28c3
…-metrics-flake-30093c
@merlimat merlimat added the area/test label Nov 10, 2026
codelipenghui
codelipenghui approved these changes Nov 10, 2026
View reviewed changes
@merlimat
merlimat merged commit 13e1685 into apache:master Nov 10, 2026
43 checks passed

Uh oh!

There was an error while loading. Please reload this page.

lhotari pushed a commit that referenced this pull request Nov 11, 2026
@merlimat @lhotari
[fix][test] Fix flaky OpenTelemetryServiceTest.testJvmRuntimeMetrics (#…
06ad6bb
…27565)

(cherry picked from commit 13e1685)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 11, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][test] Fix flaky OpenTelemetryServiceTest.testJvmRuntimeMetrics (a…
0df3b29
…pache#27565)

(cherry picked from commit 13e1685)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][test] Fix flaky OpenTelemetryServiceTest.testJvmRuntimeMetrics (a…
705fcae
…pache#27565)

(cherry picked from commit 13e1685)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

area/test cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@merlimat @codelipenghui @lhotari
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.