diff --git a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/InvalidObservationException.java b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/InvalidObservationException.java index 6caddbd851..cd22299d16 100644 --- a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/InvalidObservationException.java +++ b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/InvalidObservationException.java @@ -80,9 +80,7 @@ private StackTraceElement[] findRelevantStackTraceElements(StackTraceElement[] s private int findFirstRelevantStackTraceElementIndex(StackTraceElement[] stackTrace) { int index = -1; for (int i = 0; i < stackTrace.length; i++) { - String className = stackTrace[i].getClassName(); - if (className.equals(Observation.class.getName()) - || className.equals("io.micrometer.observation.SimpleObservation")) { + if (isObservationRelated(stackTrace[i])) { // the first relevant StackTraceElement is after the last Observation index = i + 1; } @@ -91,6 +89,13 @@ private int findFirstRelevantStackTraceElementIndex(StackTraceElement[] stackTra return (index >= stackTrace.length) ? -1 : index; } + private boolean isObservationRelated(StackTraceElement stackTraceElement) { + String className = stackTraceElement.getClassName(); + return className.equals(Observation.class.getName()) + || className.equals("io.micrometer.observation.SimpleObservation") + || className.startsWith("io.micrometer.observation.SimpleObservation$"); + } + public EventName getEventName() { return eventName; } 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 0206d7eb82..f9081d401e 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 @@ -21,6 +21,7 @@ import io.micrometer.observation.ObservationRegistry; import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; /** @@ -37,7 +38,11 @@ void doubleStartShouldBeInvalid() { assertThatThrownBy(() -> Observation.start("test", registry).start()) .isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid start: Observation has already been started"); + .hasMessage("Invalid start: Observation has already been started") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid start: Observation has already been started\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -45,7 +50,10 @@ void stopBeforeStartShouldBeInvalid() { assertThatThrownBy(() -> Observation.createNotStarted("test", registry).stop()) .isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid stop: Observation has not been started yet"); + .hasMessage("Invalid stop: Observation has not been started yet") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid stop: Observation has not been started yet\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$stopBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -53,7 +61,10 @@ void errorBeforeStartShouldBeInvalid() { assertThatThrownBy(() -> Observation.createNotStarted("test", registry).error(new RuntimeException())) .isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid error signal: Observation has not been started yet"); + .hasMessage("Invalid error signal: Observation has not been started yet") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid error signal: Observation has not been started yet\n" + + "ERROR: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -61,7 +72,10 @@ void eventBeforeStartShouldBeInvalid() { assertThatThrownBy(() -> Observation.createNotStarted("test", registry).event(Event.of("test"))) .isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid event signal: Observation has not been started yet"); + .hasMessage("Invalid event signal: Observation has not been started yet") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid event signal: Observation has not been started yet\n" + + "EVENT: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -71,7 +85,10 @@ void scopeBeforeStartShouldBeInvalid() { assertThatThrownBy(() -> Observation.createNotStarted("test", registry).openScope()) .isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid scope opening: Observation has not been started yet"); + .hasMessage("Invalid scope opening: Observation has not been started yet") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope opening: Observation has not been started yet\n" + + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -79,7 +96,11 @@ void observeAfterStartShouldBeInvalid() { assertThatThrownBy(() -> Observation.start("test", registry).observe(() -> "")) .isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid start: Observation has already been started"); + .hasMessage("Invalid start: Observation has already been started") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid start: Observation has already been started\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$observeAfterStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$observeAfterStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -90,7 +111,12 @@ void doubleStopShouldBeInvalid() { observation.stop(); }).isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid stop: Observation has already been stopped"); + .hasMessage("Invalid stop: Observation has already been stopped") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid stop: Observation has already been stopped\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -101,7 +127,12 @@ void errorAfterStopShouldBeInvalid() { observation.error(new RuntimeException()); }).isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid error signal: Observation has already been stopped"); + .hasMessage("Invalid error signal: Observation has already been stopped") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid error signal: Observation has already been stopped\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "ERROR: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -112,7 +143,12 @@ void eventAfterStopShouldBeInvalid() { observation.event(Event.of("test")); }).isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid event signal: Observation has already been stopped"); + .hasMessage("Invalid event signal: Observation has already been stopped") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid event signal: Observation has already been stopped\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "EVENT: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -124,7 +160,12 @@ void scopeOpenAfterStopShouldBeInvalid() { observation.openScope(); }).isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid scope opening: Observation has already been stopped"); + .hasMessage("Invalid scope opening: Observation has already been stopped") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope opening: Observation has already been stopped\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeOpenAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeOpenAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeOpenAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -137,7 +178,13 @@ void scopeResetAfterStopShouldBeInvalid() { scope.reset(); }).isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid scope resetting: Observation has already been stopped"); + .hasMessage("Invalid scope resetting: Observation has already been stopped") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope resetting: Observation has already been stopped\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "SCOPE_RESET: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeResetAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test @@ -149,7 +196,13 @@ void scopeCloseAfterStopShouldBeInvalid() { scope.close(); }).isExactlyInstanceOf(InvalidObservationException.class) .hasNoCause() - .hasMessage("Invalid scope closing: Observation has already been stopped"); + .hasMessage("Invalid scope closing: Observation has already been stopped") + .satisfies(exception -> assertThat(exception.toString()).matches( + "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope closing: Observation has already been stopped\n" + + "START: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)\n" + + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "SCOPE_CLOSE: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeCloseAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests.java:\\d+\\)$")); } @Test