From af4b826c6a442b6b5e69c68b0758e24c779ec310 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Tue, 26 Nov 2024 13:09:49 -0800 Subject: [PATCH] Use Thread.currentThread().getStackTrace() in InvalidObservationException (#5703) It does a null check and probably also cheaper to call. Closes gh-5702 --- .../tck/InvalidObservationException.java | 6 ++-- .../tck/ObservationValidatorTests.java | 34 +++++++++---------- 2 files changed, 21 insertions(+), 19 deletions(-) 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 cd22299d16..d95f3c0dc4 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 @@ -31,6 +31,8 @@ */ public class InvalidObservationException extends RuntimeException { + private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0]; + private final Context context; private final List history; @@ -63,14 +65,14 @@ public static class HistoryElement { HistoryElement(EventName eventName) { this.eventName = eventName; - StackTraceElement[] currentStackTrace = Thread.getAllStackTraces().get(Thread.currentThread()); + StackTraceElement[] currentStackTrace = Thread.currentThread().getStackTrace(); this.stackTrace = findRelevantStackTraceElements(currentStackTrace); } private StackTraceElement[] findRelevantStackTraceElements(StackTraceElement[] stackTrace) { int index = findFirstRelevantStackTraceElementIndex(stackTrace); if (index == -1) { - return new StackTraceElement[0]; + return EMPTY_STACK_TRACE; } else { return Arrays.copyOfRange(stackTrace, index, stackTrace.length); 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 ca3c595824..14a181a1c1 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 @@ -42,8 +42,8 @@ void doubleStartShouldBeInvalid() { .hasMessage("Invalid start: Observation 'test' has already been started") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid start: Observation 'test' 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+\\)$")); + + "START: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "START: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -54,7 +54,7 @@ void stopBeforeStartShouldBeInvalid() { .hasMessage("Invalid stop: Observation 'test' has not been started yet") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid stop: Observation 'test' has not been started yet\n" - + "STOP: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$stopBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); + + "STOP: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$stopBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -65,7 +65,7 @@ void errorBeforeStartShouldBeInvalid() { .hasMessage("Invalid error signal: Observation 'test' has not been started yet") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid error signal: Observation 'test' has not been started yet\n" - + "ERROR: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); + + "ERROR: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -76,7 +76,7 @@ void eventBeforeStartShouldBeInvalid() { .hasMessage("Invalid event signal: Observation 'test' has not been started yet") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid event signal: Observation 'test' has not been started yet\n" - + "EVENT: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); + + "EVENT: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -89,7 +89,7 @@ void scopeBeforeStartShouldBeInvalid() { .hasMessage("Invalid scope opening: Observation 'test' has not been started yet") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope opening: Observation 'test' has not been started yet\n" - + "SCOPE_OPEN: app//io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); + + "SCOPE_OPEN: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$scopeBeforeStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -100,8 +100,8 @@ void observeAfterStartShouldBeInvalid() { .hasMessage("Invalid start: Observation 'test' has already been started") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid start: Observation 'test' 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+\\)$")); + + "START: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$observeAfterStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "START: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$observeAfterStartShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -115,9 +115,9 @@ void doubleStopShouldBeInvalid() { .hasMessage("Invalid stop: Observation 'test' has already been stopped") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid stop: Observation 'test' 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+\\)$")); + + "START: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$doubleStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -131,9 +131,9 @@ void errorAfterStopShouldBeInvalid() { .hasMessage("Invalid error signal: Observation 'test' has already been stopped") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid error signal: Observation 'test' 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+\\)$")); + + "START: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "ERROR: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$errorAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test @@ -147,9 +147,9 @@ void eventAfterStopShouldBeInvalid() { .hasMessage("Invalid event signal: Observation 'test' has already been stopped") .satisfies(exception -> assertThat(exception.toString()).matches( "(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid event signal: Observation 'test' 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+\\)$")); + + "START: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "STOP: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)\n" + + "EVENT: io\\.micrometer\\.observation\\.tck\\.ObservationValidatorTests\\.lambda\\$eventAfterStopShouldBeInvalid\\$\\d+\\(ObservationValidatorTests\\.java:\\d+\\)$")); } @Test