Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax Observation Scope validation #5464

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,22 @@ public void onEvent(Event event, Context context) {
@Override
public void onScopeOpened(Context context) {
addHistoryElement(context, EventName.SCOPE_OPEN);
checkIfObservationWasStartedButNotStopped("Invalid scope opening", context);
// In some cases (Reactor) scope open can happen after the observation is stopped
checkIfObservationWasStarted("Invalid scope opening", context);
}

@Override
public void onScopeClosed(Context context) {
addHistoryElement(context, EventName.SCOPE_CLOSE);
checkIfObservationWasStartedButNotStopped("Invalid scope closing", context);
// In some cases (Reactor) scope close can happen after the observation is stopped
checkIfObservationWasStarted("Invalid scope closing", context);
}

@Override
public void onScopeReset(Context context) {
addHistoryElement(context, EventName.SCOPE_RESET);
checkIfObservationWasStartedButNotStopped("Invalid scope resetting", context);
// In some cases (Reactor) scope reset can happen after the observation is stopped
checkIfObservationWasStarted("Invalid scope resetting", context);
}

@Override
Expand All @@ -122,13 +125,20 @@ private void addHistoryElement(Context context, EventName eventName) {
}

@Nullable
private Status checkIfObservationWasStartedButNotStopped(String prefix, Context context) {
private Status checkIfObservationWasStarted(String prefix, Context context) {
Status status = context.get(Status.class);
if (status == null) {
consumer.accept(new ValidationResult(
prefix + ": Observation '" + context.getName() + "' has not been started yet", context));
}
else if (status.isStopped()) {

return status;
}

@Nullable
private Status checkIfObservationWasStartedButNotStopped(String prefix, Context context) {
Status status = checkIfObservationWasStarted(prefix, context);
if (status != null && status.isStopped()) {
consumer.accept(new ValidationResult(
prefix + ": Observation '" + context.getName() + "' has already been stopped", context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,56 +154,27 @@ void eventAfterStopShouldBeInvalid() {

@Test
@SuppressWarnings("resource")
void scopeOpenAfterStopShouldBeInvalid() {
assertThatThrownBy(() -> {
Observation observation = Observation.start("test", registry);
observation.stop();
observation.openScope();
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid scope opening: Observation 'test' has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope opening: Observation 'test' 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+\\)$"));
void scopeOpenAfterStopShouldBeValid() {
Observation observation = Observation.start("test", registry);
observation.stop();
observation.openScope();
}

@Test
@SuppressWarnings("resource")
void scopeResetAfterStopShouldBeInvalid() {
assertThatThrownBy(() -> {
Observation observation = Observation.start("test", registry);
Scope scope = observation.openScope();
observation.stop();
scope.reset();
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid scope resetting: Observation 'test' has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope resetting: Observation 'test' 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+\\)$"));
void scopeResetAfterStopShouldBeValid() {
Observation observation = Observation.start("test", registry);
Scope scope = observation.openScope();
observation.stop();
scope.reset();
}

@Test
void scopeCloseAfterStopShouldBeInvalid() {
assertThatThrownBy(() -> {
Observation observation = Observation.start("test", registry);
Scope scope = observation.openScope();
observation.stop();
scope.close();
}).isExactlyInstanceOf(InvalidObservationException.class)
.hasNoCause()
.hasMessage("Invalid scope closing: Observation 'test' has already been stopped")
.satisfies(exception -> assertThat(exception.toString()).matches(
"(?s)^io\\.micrometer\\.observation\\.tck\\.InvalidObservationException: Invalid scope closing: Observation 'test' 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+\\)$"));
void scopeCloseAfterStopShouldBeValid() {
Observation observation = Observation.start("test", registry);
Scope scope = observation.openScope();
observation.stop();
scope.close();
}

@Test
Expand Down