Skip to content

Commit

Permalink
Ignore NullObservation in ObservationValidator (micrometer-metrics#5389)
Browse files Browse the repository at this point in the history
NullObservation is a special case for context propagation. It's not
fully an Observation since it is only for scope handling which can
happen outside of "normal" Observations, therefore we not necessarily
need to validate it.

Closes micrometer-metricsgh-5388
  • Loading branch information
jonatan-ivanov authored Aug 27, 2024
1 parent 30f0b88 commit d0e40ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.observation.tck;

import io.micrometer.common.lang.Nullable;
import io.micrometer.observation.NullObservation.NullContext;
import io.micrometer.observation.Observation.Context;
import io.micrometer.observation.Observation.Event;
import io.micrometer.observation.ObservationHandler;
Expand Down Expand Up @@ -47,7 +48,7 @@ class ObservationValidator implements ObservationHandler<Context> {
}

ObservationValidator(Consumer<ValidationResult> consumer) {
this(consumer, context -> true);
this(consumer, context -> !(context instanceof NullContext));
}

ObservationValidator(Consumer<ValidationResult> consumer, Predicate<Context> supportsContextPredicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.observation.tck;

import io.micrometer.observation.NullObservation;
import io.micrometer.observation.Observation;
import io.micrometer.observation.Observation.Event;
import io.micrometer.observation.Observation.Scope;
Expand Down Expand Up @@ -253,4 +254,9 @@ void startErrorErrorStopShouldBeValid() {
Observation.start("test", registry).error(new RuntimeException()).error(new RuntimeException()).stop();
}

@Test
void nullObservationShouldBeIgnored() {
new NullObservation(registry).openScope();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class NullObservation extends SimpleObservation {

public NullObservation(ObservationRegistry registry) {
super("null", registry, new Context());
super("null", registry, new NullContext());
}

@Override
Expand Down Expand Up @@ -66,4 +66,16 @@ public Observation start() {
return this;
}

/**
* A special {@link Observation.Context} that should be used only in
* {@link NullObservation} in special cases where clearing of scopes is important. Its
* only purpose is to make scenarios through {@link NullObservation} distinguishable
* from "normal" {@link Observation Observations}.
*
* @since 1.14.0
*/
public static class NullContext extends Context {

}

}

0 comments on commit d0e40ac

Please sign in to comment.