-
Notifications
You must be signed in to change notification settings - Fork 41.4k
Auto-configure Mongo metrics #23990
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
Closed
Closed
Auto-configure Mongo metrics #23990
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c0eab89
Adds auto-configuration for Mongo metrics
onobc c1148e7
Renames property name and updates readme
onobc a6b31a2
Add auto-configuration for MongoMetricsConnectionPoolListener
onobc 901d072
Adds Mongo metrics enabled properties to additional-spring-configurat…
onobc 79cf5ac
Dropped "listener" from the naming and removed link to Mongo deprecat…
onobc 48e1151
Updated copyrights.
onobc 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
108 changes: 108 additions & 0 deletions
108
...org/springframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfiguration.java
This file contains hidden or 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,108 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* 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 org.springframework.boot.actuate.autoconfigure.metrics; | ||
|
||
import com.mongodb.MongoClientSettings; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.binder.mongodb.DefaultMongoMetricsCommandTagsProvider; | ||
import io.micrometer.core.instrument.binder.mongodb.DefaultMongoMetricsConnectionPoolTagsProvider; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandListener; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandTagsProvider; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolListener; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolTagsProvider; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Mongo metrics. | ||
* | ||
* @author Chris Bono | ||
* @author Jonatan Ivanov | ||
* @since 2.5.0 | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureBefore(MongoAutoConfiguration.class) | ||
@AutoConfigureAfter({ MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class }) | ||
@ConditionalOnClass(MongoClientSettings.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@ConditionalOnBean(MeterRegistry.class) | ||
public class MongoMetricsAutoConfiguration { | ||
|
||
@ConditionalOnClass(MongoMetricsCommandListener.class) | ||
@ConditionalOnProperty(name = "management.metrics.mongo.command.enabled", havingValue = "true", | ||
matchIfMissing = true) | ||
static class MongoCommandMetricsConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
MongoMetricsCommandListener mongoMetricsCommandListener(MeterRegistry meterRegistry, | ||
MongoMetricsCommandTagsProvider mongoMetricsCommandTagsProvider) { | ||
return new MongoMetricsCommandListener(meterRegistry, mongoMetricsCommandTagsProvider); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
MongoMetricsCommandTagsProvider mongoMetricsCommandTagsProvider() { | ||
return new DefaultMongoMetricsCommandTagsProvider(); | ||
} | ||
|
||
@Bean | ||
MongoClientSettingsBuilderCustomizer mongoMetricsCommandListenerClientSettingsBuilderCustomizer( | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MongoMetricsCommandListener mongoMetricsCommandListener) { | ||
return (clientSettingsBuilder) -> clientSettingsBuilder.addCommandListener(mongoMetricsCommandListener); | ||
} | ||
|
||
} | ||
|
||
@ConditionalOnClass(MongoMetricsConnectionPoolListener.class) | ||
@ConditionalOnProperty(name = "management.metrics.mongo.connectionpool.enabled", havingValue = "true", | ||
matchIfMissing = true) | ||
static class MongoConnectionPoolMetricsConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
MongoMetricsConnectionPoolListener mongoMetricsConnectionPoolListener(MeterRegistry meterRegistry, | ||
MongoMetricsConnectionPoolTagsProvider mongoMetricsConnectionPoolTagsProvider) { | ||
return new MongoMetricsConnectionPoolListener(meterRegistry, mongoMetricsConnectionPoolTagsProvider); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
MongoMetricsConnectionPoolTagsProvider mongoMetricsConnectionPoolTagsProvider() { | ||
return new DefaultMongoMetricsConnectionPoolTagsProvider(); | ||
} | ||
|
||
@Bean | ||
MongoClientSettingsBuilderCustomizer mongoMetricsConnectionPoolListenerClientSettingsBuilderCustomizer( | ||
MongoMetricsConnectionPoolListener mongoMetricsConnectionPoolListener) { | ||
return (clientSettingsBuilder) -> clientSettingsBuilder | ||
.applyToConnectionPoolSettings((connectionPoolSettingsBuilder) -> connectionPoolSettingsBuilder | ||
.addConnectionPoolListener(mongoMetricsConnectionPoolListener)); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or 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
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationTests.java
This file contains hidden or 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
200 changes: 200 additions & 0 deletions
200
...pringframework/boot/actuate/autoconfigure/metrics/MongoMetricsAutoConfigurationTests.java
This file contains hidden or 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,200 @@ | ||
/* | ||
* Copyright 2012-2021 the original author or authors. | ||
* | ||
* 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 org.springframework.boot.actuate.autoconfigure.metrics; | ||
|
||
import java.util.List; | ||
|
||
import com.mongodb.MongoClientSettings; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.connection.ConnectionPoolSettings; | ||
import com.mongodb.event.ConnectionPoolListener; | ||
import io.micrometer.core.instrument.binder.mongodb.DefaultMongoMetricsCommandTagsProvider; | ||
import io.micrometer.core.instrument.binder.mongodb.DefaultMongoMetricsConnectionPoolTagsProvider; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandListener; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandTagsProvider; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolListener; | ||
import io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolTagsProvider; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; | ||
import org.springframework.boot.test.context.FilteredClassLoader; | ||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.boot.test.context.runner.ContextConsumer; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Tests for {@link MongoMetricsAutoConfiguration}. | ||
* | ||
* @author Chris Bono | ||
*/ | ||
class MongoMetricsAutoConfigurationTests { | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(MongoMetricsAutoConfiguration.class)); | ||
|
||
@Test | ||
void whenThereIsAMeterRegistryThenMetricsCommandListenerIsAdded() { | ||
this.contextRunner.with(MetricsRun.simple()) | ||
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)).run((context) -> { | ||
assertThat(context).hasSingleBean(MongoMetricsCommandListener.class); | ||
assertThat(getActualMongoClientSettingsUsedToConstructClient(context)).isNotNull() | ||
.extracting(MongoClientSettings::getCommandListeners).asList() | ||
.containsExactly(context.getBean(MongoMetricsCommandListener.class)); | ||
assertThat(getMongoMetricsCommandTagsProviderUsedToConstructListener(context)) | ||
.isInstanceOf(DefaultMongoMetricsCommandTagsProvider.class); | ||
}); | ||
} | ||
|
||
@Test | ||
void whenThereIsAMeterRegistryThenMetricsConnectionPoolListenerIsAdded() { | ||
this.contextRunner.with(MetricsRun.simple()) | ||
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)).run((context) -> { | ||
assertThat(context).hasSingleBean(MongoMetricsConnectionPoolListener.class); | ||
assertThat(getConnectionPoolListenersFromClient(context)) | ||
.containsExactly(context.getBean(MongoMetricsConnectionPoolListener.class)); | ||
assertThat(getMongoMetricsConnectionPoolTagsProviderUsedToConstructListener(context)) | ||
.isInstanceOf(DefaultMongoMetricsConnectionPoolTagsProvider.class); | ||
}); | ||
} | ||
|
||
@Test | ||
void whenThereIsNoMeterRegistryThenNoMetricsCommandListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.run((context) -> assertThatMetricsCommandListenerNotAdded()); | ||
} | ||
|
||
@Test | ||
void whenThereIsNoMeterRegistryThenNoMetricsConnectionPoolListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.run((context) -> assertThatMetricsConnectionPoolListenerNotAdded()); | ||
} | ||
|
||
@Test | ||
void whenThereIsACustomMetricsCommandTagsProviderItIsUsed() { | ||
final MongoMetricsCommandTagsProvider customTagsProvider = mock(MongoMetricsCommandTagsProvider.class); | ||
this.contextRunner.with(MetricsRun.simple()) | ||
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withBean("customMongoMetricsCommandTagsProvider", MongoMetricsCommandTagsProvider.class, | ||
() -> customTagsProvider) | ||
.run((context) -> assertThat(getMongoMetricsCommandTagsProviderUsedToConstructListener(context)) | ||
.isSameAs(customTagsProvider)); | ||
} | ||
|
||
@Test | ||
void whenThereIsACustomMetricsConnectionPoolTagsProviderItIsUsed() { | ||
final MongoMetricsConnectionPoolTagsProvider customTagsProvider = mock( | ||
MongoMetricsConnectionPoolTagsProvider.class); | ||
this.contextRunner.with(MetricsRun.simple()) | ||
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withBean("customMongoMetricsConnectionPoolTagsProvider", MongoMetricsConnectionPoolTagsProvider.class, | ||
() -> customTagsProvider) | ||
.run((context) -> assertThat(getMongoMetricsConnectionPoolTagsProviderUsedToConstructListener(context)) | ||
.isSameAs(customTagsProvider)); | ||
} | ||
|
||
@Test | ||
void whenThereIsNoMongoClientSettingsOnClasspathThenNoMetricsCommandListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withClassLoader(new FilteredClassLoader(MongoClientSettings.class)) | ||
.run((context) -> assertThatMetricsCommandListenerNotAdded()); | ||
} | ||
|
||
@Test | ||
void whenThereIsNoMongoClientSettingsOnClasspathThenNoMetricsConnectionPoolListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withClassLoader(new FilteredClassLoader(MongoClientSettings.class)) | ||
.run((context) -> assertThatMetricsConnectionPoolListenerNotAdded()); | ||
} | ||
|
||
@Test | ||
void whenThereIsNoMongoMetricsCommandListenerOnClasspathThenNoMetricsCommandListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withClassLoader(new FilteredClassLoader(MongoMetricsCommandListener.class)) | ||
.run((context) -> assertThatMetricsCommandListenerNotAdded()); | ||
} | ||
|
||
@Test | ||
void whenThereIsNoMongoMetricsConnectionPoolListenerOnClasspathThenNoMetricsConnectionPoolListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withClassLoader(new FilteredClassLoader(MongoMetricsConnectionPoolListener.class)) | ||
.run((context) -> assertThatMetricsConnectionPoolListenerNotAdded()); | ||
} | ||
|
||
@Test | ||
void whenMetricsCommandListenerEnabledPropertyFalseThenNoMetricsCommandListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withPropertyValues("management.metrics.mongo.command.enabled:false") | ||
.run((context) -> assertThatMetricsCommandListenerNotAdded()); | ||
} | ||
|
||
@Test | ||
void whenMetricsConnectionPoolListenerEnabledPropertyFalseThenNoMetricsConnectionPoolListenerIsAdded() { | ||
this.contextRunner.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class)) | ||
.withPropertyValues("management.metrics.mongo.connectionpool.enabled:false") | ||
.run((context) -> assertThatMetricsConnectionPoolListenerNotAdded()); | ||
} | ||
|
||
private ContextConsumer<AssertableApplicationContext> assertThatMetricsCommandListenerNotAdded() { | ||
return (context) -> { | ||
assertThat(context).doesNotHaveBean(MongoMetricsCommandListener.class); | ||
assertThat(getActualMongoClientSettingsUsedToConstructClient(context)).isNotNull() | ||
.extracting(MongoClientSettings::getCommandListeners).asList().isEmpty(); | ||
}; | ||
} | ||
|
||
private ContextConsumer<AssertableApplicationContext> assertThatMetricsConnectionPoolListenerNotAdded() { | ||
return (context) -> { | ||
assertThat(context).doesNotHaveBean(MongoMetricsConnectionPoolListener.class); | ||
assertThat(getConnectionPoolListenersFromClient(context)).isEmpty(); | ||
}; | ||
} | ||
|
||
private MongoClientSettings getActualMongoClientSettingsUsedToConstructClient( | ||
final AssertableApplicationContext context) { | ||
final MongoClient mongoClient = context.getBean(MongoClient.class); | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return (MongoClientSettings) ReflectionTestUtils.getField(mongoClient, "settings"); | ||
} | ||
|
||
private List<ConnectionPoolListener> getConnectionPoolListenersFromClient( | ||
final AssertableApplicationContext context) { | ||
MongoClientSettings mongoClientSettings = getActualMongoClientSettingsUsedToConstructClient(context); | ||
ConnectionPoolSettings connectionPoolSettings = mongoClientSettings.getConnectionPoolSettings(); | ||
@SuppressWarnings("unchecked") | ||
List<ConnectionPoolListener> listeners = (List<ConnectionPoolListener>) ReflectionTestUtils | ||
.getField(connectionPoolSettings, "connectionPoolListeners"); | ||
return listeners; | ||
} | ||
|
||
private MongoMetricsCommandTagsProvider getMongoMetricsCommandTagsProviderUsedToConstructListener( | ||
final AssertableApplicationContext context) { | ||
MongoMetricsCommandListener listener = context.getBean(MongoMetricsCommandListener.class); | ||
return (MongoMetricsCommandTagsProvider) ReflectionTestUtils.getField(listener, "tagsProvider"); | ||
} | ||
|
||
private MongoMetricsConnectionPoolTagsProvider getMongoMetricsConnectionPoolTagsProviderUsedToConstructListener( | ||
final AssertableApplicationContext context) { | ||
MongoMetricsConnectionPoolListener listener = context.getBean(MongoMetricsConnectionPoolListener.class); | ||
return (MongoMetricsConnectionPoolTagsProvider) ReflectionTestUtils.getField(listener, "tagsProvider"); | ||
} | ||
|
||
} |
Oops, something went wrong.
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.
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.
2012-2021
is correct here.