-
Notifications
You must be signed in to change notification settings - Fork 985
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
Add ObservationValidator #5300
Merged
shakuzen
merged 1 commit into
micrometer-metrics:main
from
jonatan-ivanov:observation-validator
Jul 12, 2024
Merged
Add ObservationValidator #5300
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
...er-observation-test/src/main/java/io/micrometer/observation/tck/ObservationValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer.observation.tck; | ||
|
||
import io.micrometer.common.lang.Nullable; | ||
import io.micrometer.common.util.internal.logging.InternalLogger; | ||
import io.micrometer.common.util.internal.logging.InternalLoggerFactory; | ||
import io.micrometer.observation.Observation.Context; | ||
import io.micrometer.observation.Observation.Event; | ||
import io.micrometer.observation.ObservationHandler; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* An {@link ObservationHandler} that validates the order of events of an Observation (for | ||
* example stop should be called after start) and with a validation message and the | ||
* original context, it publishes the events of these invalid scenarios to the | ||
* {@link Consumer} of your choice. | ||
* | ||
* @author Jonatan Ivanov | ||
*/ | ||
class ObservationValidator implements ObservationHandler<Context> { | ||
|
||
private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(ObservationValidator.class); | ||
|
||
private final Consumer<ValidationResult> consumer; | ||
|
||
private final Predicate<Context> supportsContextPredicate; | ||
|
||
ObservationValidator() { | ||
this(validationResult -> LOGGER.warn(validationResult.toString())); | ||
} | ||
|
||
ObservationValidator(Consumer<ValidationResult> consumer) { | ||
this(consumer, context -> true); | ||
} | ||
|
||
ObservationValidator(Consumer<ValidationResult> consumer, Predicate<Context> supportsContextPredicate) { | ||
this.consumer = consumer; | ||
this.supportsContextPredicate = supportsContextPredicate; | ||
} | ||
|
||
@Override | ||
public void onStart(Context context) { | ||
Status status = context.get(Status.class); | ||
if (status != null) { | ||
consumer.accept(new ValidationResult("Invalid start: Observation has already been started", context)); | ||
} | ||
else { | ||
context.put(Status.class, new Status()); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Context context) { | ||
checkIfObservationWasStartedButNotStopped("Invalid error signal", context); | ||
} | ||
|
||
@Override | ||
public void onEvent(Event event, Context context) { | ||
checkIfObservationWasStartedButNotStopped("Invalid event signal", context); | ||
} | ||
|
||
@Override | ||
public void onScopeOpened(Context context) { | ||
checkIfObservationWasStartedButNotStopped("Invalid scope opening", context); | ||
} | ||
|
||
@Override | ||
public void onScopeClosed(Context context) { | ||
checkIfObservationWasStartedButNotStopped("Invalid scope closing", context); | ||
} | ||
|
||
@Override | ||
public void onScopeReset(Context context) { | ||
checkIfObservationWasStartedButNotStopped("Invalid scope resetting", context); | ||
} | ||
|
||
@Override | ||
public void onStop(Context context) { | ||
Status status = checkIfObservationWasStartedButNotStopped("Invalid stop", context); | ||
if (status != null) { | ||
status.markStopped(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportsContext(Context context) { | ||
return supportsContextPredicate.test(context); | ||
} | ||
|
||
@Nullable | ||
private Status checkIfObservationWasStartedButNotStopped(String prefix, Context context) { | ||
Status status = context.get(Status.class); | ||
if (status == null) { | ||
consumer.accept(new ValidationResult(prefix + ": Observation has not been started yet", context)); | ||
} | ||
else if (status.isStopped()) { | ||
consumer.accept(new ValidationResult(prefix + ": Observation has already been stopped", context)); | ||
} | ||
|
||
return status; | ||
} | ||
|
||
static class ValidationResult { | ||
|
||
private final String message; | ||
|
||
private final Context context; | ||
|
||
ValidationResult(String message, Context context) { | ||
this.message = message; | ||
this.context = context; | ||
} | ||
|
||
String getMessage() { | ||
return message; | ||
} | ||
|
||
Context getContext() { | ||
return context; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getMessage() + " - " + getContext(); | ||
} | ||
|
||
} | ||
|
||
static class Status { | ||
|
||
private boolean stopped = false; | ||
|
||
boolean isStopped() { | ||
return stopped; | ||
} | ||
|
||
void markStopped() { | ||
stopped = true; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
...servation-test/src/test/java/io/micrometer/observation/tck/ObservationValidatorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer.observation.tck; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.Observation.Event; | ||
import io.micrometer.observation.Observation.Scope; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.observation.tck.ObservationValidator.ValidationResult; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link ObservationValidator}. | ||
* | ||
* @author Jonatan Ivanov | ||
*/ | ||
class ObservationValidatorTests { | ||
|
||
private TestConsumer testConsumer; | ||
|
||
private ObservationRegistry registry; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
testConsumer = new TestConsumer(); | ||
registry = ObservationRegistry.create(); | ||
registry.observationConfig().observationHandler(new ObservationValidator(testConsumer)); | ||
} | ||
|
||
@Test | ||
void doubleStartShouldBeInvalid() { | ||
Observation.start("test", registry).start(); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid start: Observation has already been started"); | ||
} | ||
|
||
@Test | ||
void stopBeforeStartShouldBeInvalid() { | ||
Observation.createNotStarted("test", registry).stop(); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid stop: Observation has not been started yet"); | ||
} | ||
|
||
@Test | ||
void errorBeforeStartShouldBeInvalid() { | ||
Observation.createNotStarted("test", registry).error(new RuntimeException()); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid error signal: Observation has not been started yet"); | ||
} | ||
|
||
@Test | ||
void eventBeforeStartShouldBeInvalid() { | ||
Observation.createNotStarted("test", registry).event(Event.of("test")); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid event signal: Observation has not been started yet"); | ||
} | ||
|
||
@Test | ||
void scopeBeforeStartShouldBeInvalid() { | ||
Scope scope = Observation.createNotStarted("test", registry).openScope(); | ||
scope.reset(); | ||
scope.close(); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid scope opening: Observation has not been started yet\n" | ||
+ "Invalid scope resetting: Observation has not been started yet\n" | ||
+ "Invalid scope closing: Observation has not been started yet"); | ||
} | ||
|
||
@Test | ||
void observeAfterStartShouldBeInvalid() { | ||
Observation.start("test", registry).observe(() -> ""); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid start: Observation has already been started"); | ||
} | ||
|
||
@Test | ||
void doubleStopShouldBeInvalid() { | ||
Observation observation = Observation.start("test", registry); | ||
observation.stop(); | ||
observation.stop(); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid stop: Observation has already been stopped"); | ||
} | ||
|
||
@Test | ||
void errorAfterStopShouldBeInvalid() { | ||
Observation observation = Observation.start("test", registry); | ||
observation.stop(); | ||
observation.error(new RuntimeException()); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid error signal: Observation has already been stopped"); | ||
} | ||
|
||
@Test | ||
void eventAfterStopShouldBeInvalid() { | ||
Observation observation = Observation.start("test", registry); | ||
observation.stop(); | ||
observation.event(Event.of("test")); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid event signal: Observation has already been stopped"); | ||
} | ||
|
||
@Test | ||
void scopeAfterStopShouldBeInvalid() { | ||
Observation observation = Observation.start("test", registry); | ||
observation.stop(); | ||
Scope scope = observation.openScope(); | ||
scope.reset(); | ||
scope.close(); | ||
assertThat(testConsumer.toString()).isEqualTo("Invalid scope opening: Observation has already been stopped\n" | ||
+ "Invalid scope resetting: Observation has already been stopped\n" | ||
+ "Invalid scope closing: Observation has already been stopped"); | ||
} | ||
|
||
@Test | ||
void startEventStopShouldBeValid() { | ||
Observation.start("test", registry).event(Event.of("test")).stop(); | ||
assertThat(testConsumer.toString()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void startEventErrorStopShouldBeValid() { | ||
Observation.start("test", registry).event(Event.of("test")).error(new RuntimeException()).stop(); | ||
assertThat(testConsumer.toString()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void startErrorEventStopShouldBeValid() { | ||
Observation.start("test", registry).error(new RuntimeException()).event(Event.of("test")).stop(); | ||
assertThat(testConsumer.toString()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void startScopeEventStopShouldBeValid() { | ||
Observation observation = Observation.start("test", registry); | ||
observation.openScope().close(); | ||
observation.event(Event.of("test")); | ||
observation.stop(); | ||
assertThat(testConsumer.toString()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void startScopeEventErrorStopShouldBeValid() { | ||
Observation observation = Observation.start("test", registry); | ||
Scope scope = observation.openScope(); | ||
observation.event(Event.of("test")); | ||
observation.error(new RuntimeException()); | ||
scope.close(); | ||
observation.stop(); | ||
assertThat(testConsumer.toString()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void startScopeErrorEventStopShouldBeValid() { | ||
Observation observation = Observation.start("test", registry); | ||
Scope scope = observation.openScope(); | ||
observation.error(new RuntimeException()); | ||
observation.event(Event.of("test")); | ||
scope.close(); | ||
observation.stop(); | ||
assertThat(testConsumer.toString()).isEmpty(); | ||
} | ||
|
||
static class TestConsumer implements Consumer<ValidationResult> { | ||
|
||
private final StringBuilder stringBuilder = new StringBuilder(); | ||
|
||
@Override | ||
public void accept(ValidationResult validationResult) { | ||
stringBuilder.append(validationResult.getMessage()).append("\n"); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return stringBuilder.toString().trim(); | ||
} | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approved and merged, but I wonder if it would be better to throw an exception by default (what gets used with TestObservationRegistry). I worry that a log message in tests will very easily be missed. The things we're checking for here should plainly be instrumentation bugs and so unless there's a bug in this validator, I'm not sure it should be a problem that an exception is thrown. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. The validator is installed by default, but I can't think of a case where duplicate stops calls is not considered as a bug in the instrumentation. If somehow this is expected, people should overrride the consumer and log things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue with this is that the validator was intentionally made package private so it is all kept as an implementation detail. As things are, there's no option for users to override the consumer. We could make that public API so it is possible, but we'll have to make sure the API is right before going GA.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have a few options to make this more flexible:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my preference would be to switch it to throw an exception and not make anything configurable until we get feedback from a user why the validator's assumptions about correct instrumentation are wrong for their use case. Catching and ignoring the exception thrown in a test is always a workaround on a case-by-case basis. The challenge, as always, is getting enough feedback during the milestone phase before releasing GA, but it's also good for us to understand such use cases since I think we are lacking imagination to come up with them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼 In order to do that, we need to surface at least an exception so that users can handle it as needed: #5307