Skip to content

ObservationValidator introduced in TestObservationRegistry with version 1.14 sometimes throws InvalidObservationException #5802

@mathiaskinzler

Description

@mathiaskinzler

Describe the bug
We are using micrometer in a spring boot application and have tests in place for our micrometer trace instrumentation. After (implicitly) updating micrometer from 1.13.8 to 1.14.2, a few of our tests fail due to InvalidObservationException being thrown. Tracing this down, the reason is the newly introduced ObservationValidator.
Note that the issue is in the micrometer test jar, not in the micrometer productive code.

The error message is "Invalid stop: Observation 'xxx' has already been stopped"

The underlying reason why "stop" is tried to be executed "twice" is probably that MicrometerObservationListener (which is not in the test jar) does an observation.stop in it's doOnCancel method (the same as in it's doOnComplete). It seems that the spring boot reactive framework does not guarantee that doOnStop and doOnCancel are always called mutually exclusive. Note that the situation only occurs in a few test cases where the request is terminated by an Exception or using skipWhile on a Flux which seems to indicate that the onCancel is issued by the spring framework when disposing the request.

Environment

Spring boot 3.4.1 with managed micrometer version 1.14.2

  • Micrometer version 1.14.2
  • Micrometer registry dynatrace
  • OS: windows/linux
  • Java version: 17

To Reproduce
How to reproduce the bug:

Hard to provide a test case short of our application.

Expected behavior
Adding micrometer test must not break existing productive code. A quick solution might be to allow to create a TestObservationRegistry that does not include the ObservationValidator by default. Alternatively, allowing to pass an alternative Consumer&ltValidationResult&gt instead of throwInvalidObservationException might be an option so that validation results could be checked by the consuming tests.

Additional context
Add any other context about the problem here, e.g. related issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions