From 31b0951e83797b6e4f6ed59e8373442977f84fd0 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Wed, 14 Aug 2024 15:57:07 -0700 Subject: [PATCH] Ignore NullObservation in ObservationValidator 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 gh-5388 --- .../observation/tck/ObservationValidator.java | 3 ++- .../observation/tck/ObservationValidatorTests.java | 6 ++++++ .../io/micrometer/observation/NullObservation.java | 14 +++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java index 1ab546e92e..300ee47a05 100644 --- a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java +++ b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java @@ -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; @@ -47,7 +48,7 @@ class ObservationValidator implements ObservationHandler { } ObservationValidator(Consumer consumer) { - this(consumer, context -> true); + this(consumer, context -> !(context instanceof NullContext)); } ObservationValidator(Consumer consumer, Predicate supportsContextPredicate) { diff --git a/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java b/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java index f9081d401e..98602e8e40 100644 --- a/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java +++ b/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java @@ -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; @@ -253,4 +254,9 @@ void startErrorErrorStopShouldBeValid() { Observation.start("test", registry).error(new RuntimeException()).error(new RuntimeException()).stop(); } + @Test + void nullObservationShouldBeIgnored() { + new NullObservation(registry).openScope(); + } + } diff --git a/micrometer-observation/src/main/java/io/micrometer/observation/NullObservation.java b/micrometer-observation/src/main/java/io/micrometer/observation/NullObservation.java index 40ee453fcd..47303e0bd4 100644 --- a/micrometer-observation/src/main/java/io/micrometer/observation/NullObservation.java +++ b/micrometer-observation/src/main/java/io/micrometer/observation/NullObservation.java @@ -28,7 +28,7 @@ public class NullObservation extends SimpleObservation { public NullObservation(ObservationRegistry registry) { - super("null", registry, new Context()); + super("null", registry, new NullContext()); } @Override @@ -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 { + + } + }