Polishing.

Replace if statement with ternary operator.

See #4994
This commit is contained in:
Mark Paluch
2025-06-11 14:35:01 +02:00
parent bcbdc71edb
commit 313ee26c27
2 changed files with 7 additions and 11 deletions

View File

@@ -33,6 +33,7 @@ import com.mongodb.event.CommandStartedEvent;
*
* @author Greg Turnquist
* @author Mark Paluch
* @author Michal Domagala
* @since 4.0
*/
class DefaultMongoHandlerObservationConvention implements MongoHandlerObservationConvention {
@@ -54,21 +55,16 @@ class DefaultMongoHandlerObservationConvention implements MongoHandlerObservatio
if (!ObjectUtils.isEmpty(user)) {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_USER.withValue(user));
}
}
if (!ObjectUtils.isEmpty(context.getDatabaseName())) {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.DB_NAME.withValue(context.getDatabaseName()));
}
if (!ObjectUtils.isEmpty(context.getCollectionName())) {
keyValues = keyValues
.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(context.getCollectionName()));
} else {
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(KeyValue.NONE_VALUE));
}
keyValues = keyValues.and(LowCardinalityCommandKeyNames.MONGODB_COLLECTION.withValue(
ObjectUtils.isEmpty(context.getCollectionName()) ? KeyValue.NONE_VALUE : context.getCollectionName()));
if(context.getCommandStartedEvent() == null) {
if (context.getCommandStartedEvent() == null) {
throw new IllegalStateException("not command started event present");
}

View File

@@ -51,6 +51,7 @@ import com.mongodb.event.CommandSucceededEvent;
* @author Greg Turnquist
* @author Mark Paluch
* @author François Kha
* @author Michal Domagala
*/
class MongoObservationCommandListenerTests {
@@ -99,8 +100,8 @@ class MongoObservationCommandListenerTests {
assertThat(meterRegistry).hasMeterWithName("spring.data.mongodb.command.active");
}
@Test
void commandStartedShouldIncludeCollectionIfMissing() {
@Test // GH-4994
void commandStartedShouldAlwaysIncludeCollection() {
// when
listener.commandStarted(new CommandStartedEvent(new MapRequestContext(), 0, 0, null, "some name", "hello", null));
@@ -110,7 +111,6 @@ class MongoObservationCommandListenerTests {
assertThat(meterRegistry).hasMeterWithNameAndTags(
"spring.data.mongodb.command.active",
Tags.of("db.mongodb.collection", "none"));
}
@Test