Skip to content

Use per-thread scope stack in ObservationValidator LIFO validation - #7295

Merged
jonatan-ivanov merged 1 commit into
micrometer-metrics:mainfrom
seonghyeoklee:gh-7288
Mar 24, 2026
Merged

jonatan-ivanov merged 1 commit into
micrometer-metrics:mainfrom
seonghyeoklee:gh-7288

Conversation

@seonghyeoklee

Copy link
Copy Markdown
Contributor

Summary

The LIFO scope ordering validation in ObservationValidator used a single global Deque<Context>, which produced false positives when observations ran in parallel on different threads (sibling scopes).

This replaces the global stack with a ThreadLocal<Deque<Context>> so that each thread tracks its own scope ordering independently.

Changes

  • ObservationValidator: Deque<Context> β†’ ThreadLocal<Deque<Context>> with ThreadLocal.withInitial(ArrayDeque::new)
  • ObservationValidatorTests: Add siblingScopesOnDifferentThreadsShouldBeValid test that verifies parallel observations on separate threads no longer trigger false positives

Closes gh-7288

Closes micrometer-metricsgh-7288

Signed-off-by: seonghyeoklee <dltjdgur327@gmail.com>
@jonatan-ivanov
jonatan-ivanov merged commit a76e8ca into micrometer-metrics:main Mar 24, 2026
11 checks passed
@jonatan-ivanov

Copy link
Copy Markdown
Member

Thank you!

jonatan-ivanov added a commit to jonatan-ivanov/micrometer that referenced this pull request Mar 25, 2026
In micrometer-metricsgh-7295 a ThreadLocal was introduced and elements are only added to
it, never removed.

Also, it was reported in micrometer-metricsgh-6713 that in some cases reusing
TestObservationRegistry can trigger validation issues since the
validator remembers all the low cardinality key-values that were
recorded previously.

Both of the issues can be worked around by creating a new
TestObservationRegistry instance, however in some cases clearing its
memory might be more convenient.

See micrometer-metricsgh-7295
See micrometer-metricsgh-6713
@jonatan-ivanov

Copy link
Copy Markdown
Member

Fyi: I created #7295 to be able to clear the state of the validator.

jonatan-ivanov added a commit that referenced this pull request Mar 25, 2026
In gh-7295 a ThreadLocal was introduced and elements are only added to
it, never removed.

Also, it was reported in gh-6713 that in some cases reusing
TestObservationRegistry can trigger validation issues since the
validator remembers all the low cardinality key-values that were
recorded previously.

Both of the issues can be worked around by creating a new
TestObservationRegistry instance, however in some cases clearing its
memory might be more convenient.

See gh-7295
See gh-6713
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ObservationValidator scope LIFO validation produces false positives for parallel observations

2 participants