Browse Source

Polishing.

Remove BDD comments.
Minor formatting.

Original pull request #5083
See #5082
issue/aot-doc
Jens Schauder 2 months ago
parent
commit
d83754ecf9
No known key found for this signature in database
GPG Key ID: 2BE5D185CD2A1CE6
  1. 32
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/observability/MongoObservationCommandListenerTests.java

32
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/observability/MongoObservationCommandListenerTests.java

@ -75,6 +75,7 @@ class MongoObservationCommandListenerTests {
@AfterEach @AfterEach
void tearDown() { void tearDown() {
Observation currentObservation = observationRegistry.getCurrentObservation(); Observation currentObservation = observationRegistry.getCurrentObservation();
if (currentObservation != null) { if (currentObservation != null) {
currentObservation.stop(); currentObservation.stop();
@ -85,40 +86,32 @@ class MongoObservationCommandListenerTests {
@Test @Test
void commandStartedShouldNotInstrumentWhenAdminDatabase() { void commandStartedShouldNotInstrumentWhenAdminDatabase() {
// when
listener.commandStarted(new CommandStartedEvent(null, 0, 0, null, "admin", "", null)); listener.commandStarted(new CommandStartedEvent(null, 0, 0, null, "admin", "", null));
// then
assertThat(meterRegistry).hasNoMetrics(); assertThat(meterRegistry).hasNoMetrics();
} }
@Test @Test
void commandStartedShouldNotInstrumentWhenNoRequestContext() { void commandStartedShouldNotInstrumentWhenNoRequestContext() {
// when
listener.commandStarted(new CommandStartedEvent(null, 0, 0, null, "some name", "", null)); listener.commandStarted(new CommandStartedEvent(null, 0, 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasNoMetrics(); assertThat(meterRegistry).hasNoMetrics();
} }
@Test @Test
void commandStartedShouldNotInstrumentWhenNoParentSampleInRequestContext() { void commandStartedShouldNotInstrumentWhenNoParentSampleInRequestContext() {
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null)); listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasMeterWithName("spring.data.mongodb.command.active"); assertThat(meterRegistry).hasMeterWithName("spring.data.mongodb.command.active");
} }
@Test // GH-4994 @Test // GH-4994
void commandStartedShouldAlwaysIncludeCollection() { void commandStartedShouldAlwaysIncludeCollection() {
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "hello", null)); listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "hello", null));
// then
// although command 'hello' is collection-less, metric must have tag "db.mongodb.collection" // although command 'hello' is collection-less, metric must have tag "db.mongodb.collection"
assertThat(meterRegistry).hasMeterWithNameAndTags( assertThat(meterRegistry).hasMeterWithNameAndTags(
"spring.data.mongodb.command.active", "spring.data.mongodb.command.active",
@ -140,18 +133,15 @@ class MongoObservationCommandListenerTests {
new BsonDocument("collection", new BsonString("user")))); new BsonDocument("collection", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(context, 0, 0, null, "insert", null, null, 0)); listener.commandSucceeded(new CommandSucceededEvent(context, 0, 0, null, "insert", null, null, 0));
// then
assertThatTimerRegisteredWithTags(); assertThatTimerRegisteredWithTags();
} }
@Test @Test
void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestContext() { void successfullyCompletedCommandShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry); Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, // listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, //
new ConnectionDescription( // new ConnectionDescription( //
new ServerId( // new ServerId( //
@ -160,18 +150,15 @@ class MongoObservationCommandListenerTests {
new BsonDocument("collection", new BsonString("user")))); new BsonDocument("collection", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0)); listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
// then
assertThatTimerRegisteredWithTags(); assertThatTimerRegisteredWithTags();
} }
@Test @Test
void successfullyCompletedCommandWithCollectionHavingCommandNameShouldCreateTimerWhenParentSampleInRequestContext() { void successfullyCompletedCommandWithCollectionHavingCommandNameShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry); Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, // listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, //
new ConnectionDescription( // new ConnectionDescription( //
new ServerId( // new ServerId( //
@ -181,18 +168,15 @@ class MongoObservationCommandListenerTests {
new BsonDocument("aggregate", new BsonString("user")))); new BsonDocument("aggregate", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "aggregate", null, null, 0)); listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "aggregate", null, null, 0));
// then
assertThatTimerRegisteredWithTags(); assertThatTimerRegisteredWithTags();
} }
@Test @Test
void successfullyCompletedCommandWithoutClusterInformationShouldCreateTimerWhenParentSampleInRequestContext() { void successfullyCompletedCommandWithoutClusterInformationShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry); Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert", listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert",
new BsonDocument("collection", new BsonString("user")))); new BsonDocument("collection", new BsonString("user"))));
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0)); listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
@ -207,11 +191,9 @@ class MongoObservationCommandListenerTests {
@Test @Test
void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() { void commandWithErrorShouldCreateTimerWhenParentSampleInRequestContext() {
// given
Observation parent = Observation.start("name", observationRegistry); Observation parent = Observation.start("name", observationRegistry);
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, // listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, //
new ConnectionDescription( // new ConnectionDescription( //
new ServerId( // new ServerId( //
@ -222,20 +204,17 @@ class MongoObservationCommandListenerTests {
listener.commandFailed( // listener.commandFailed( //
new CommandFailedEvent(traceRequestContext, 0, 0, null, "db", "insert", 0, new IllegalAccessException())); new CommandFailedEvent(traceRequestContext, 0, 0, null, "db", "insert", 0, new IllegalAccessException()));
// then
assertThatTimerRegisteredWithTags(); assertThatTimerRegisteredWithTags();
} }
@Test // GH-4481 @Test // GH-4481
void completionShouldIgnoreIncompatibleObservationContext() { void completionShouldIgnoreIncompatibleObservationContext() {
// given
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
Observation observation = mock(Observation.class); Observation observation = mock(Observation.class);
traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation); traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation);
// when
listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0)); listener.commandSucceeded(new CommandSucceededEvent(traceRequestContext, 0, 0, null, "insert", null, null, 0));
verify(observation).getContext(); verify(observation).getContext();
@ -245,13 +224,11 @@ class MongoObservationCommandListenerTests {
@Test // GH-4481 @Test // GH-4481
void failureShouldIgnoreIncompatibleObservationContext() { void failureShouldIgnoreIncompatibleObservationContext() {
// given
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
Observation observation = mock(Observation.class); Observation observation = mock(Observation.class);
traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation); traceRequestContext.put(ObservationThreadLocalAccessor.KEY, observation);
// when
listener.commandFailed(new CommandFailedEvent(traceRequestContext, 0, 0, null, "db", "insert", 0, null)); listener.commandFailed(new CommandFailedEvent(traceRequestContext, 0, 0, null, "db", "insert", 0, null));
verify(observation).getContext(); verify(observation).getContext();
@ -261,7 +238,6 @@ class MongoObservationCommandListenerTests {
@Test // GH-4321 @Test // GH-4321
void shouldUseObservationConvention() { void shouldUseObservationConvention() {
// given
MongoHandlerObservationConvention customObservationConvention = new MongoHandlerObservationConvention() { MongoHandlerObservationConvention customObservationConvention = new MongoHandlerObservationConvention() {
@Override @Override
public boolean supportsContext(Observation.Context context) { public boolean supportsContext(Observation.Context context) {
@ -276,22 +252,18 @@ class MongoObservationCommandListenerTests {
this.listener = new MongoObservationCommandListener(observationRegistry, mock(ConnectionString.class), this.listener = new MongoObservationCommandListener(observationRegistry, mock(ConnectionString.class),
customObservationConvention); customObservationConvention);
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null)); listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "", null));
// then
assertThat(meterRegistry).hasMeterWithName("custom.name.active"); assertThat(meterRegistry).hasMeterWithName("custom.name.active");
} }
@Test // GH-5064 @Test // GH-5064
void completionRestoresParentObservation() { void completionRestoresParentObservation() {
// given
Observation parent = Observation.start("name", observationRegistry); Observation parent = Observation.start("name", observationRegistry);
observationRegistry.setCurrentObservationScope(parent.openScope()); observationRegistry.setCurrentObservationScope(parent.openScope());
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert", listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert",
new BsonDocument("collection", new BsonString("user")))); new BsonDocument("collection", new BsonString("user"))));
@ -306,12 +278,10 @@ class MongoObservationCommandListenerTests {
@Test // GH-5064 @Test // GH-5064
void failureRestoresParentObservation() { void failureRestoresParentObservation() {
// given
Observation parent = Observation.start("name", observationRegistry); Observation parent = Observation.start("name", observationRegistry);
observationRegistry.setCurrentObservationScope(parent.openScope()); observationRegistry.setCurrentObservationScope(parent.openScope());
RequestContext traceRequestContext = getContext(); RequestContext traceRequestContext = getContext();
// when
listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert", listener.commandStarted(new CommandStartedEvent(traceRequestContext, 0, 0, null, "database", "insert",
new BsonDocument("collection", new BsonString("user")))); new BsonDocument("collection", new BsonString("user"))));

Loading…
Cancel
Save