Skip to content

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
wants to merge 6 commits into from
Closed

Auto-configure Mongo metrics #23990

wants to merge 6 commits into from

Conversation

onobc
Copy link
Contributor

@onobc onobc commented Nov 2, 2020

The current mechanism to enable Micrometer metrics for Mongo is to add the MongoMetricsCommandListener to the MongoClientSettings.Builder. This code proposal simply auto-configures in this command listener when appropriate.

ℹ️ There is also a MongoMetricsConnectionPoolListener that would add more Mongo (connection pool) metrics. However, I left it out because it has a dependency on legacy com.mongodb.MongoClient as well as some Mongo event classes that were removed in Mongo 4 Java driver. I am planning on submitting a fix to Micrometer to bring it up to speed w/ Mongo 4 driver. Once that is available, I will circle back here and auto-configure it in.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 2, 2020
@ConditionalOnClass(MongoMetricsCommandListener.class)
@ConditionalOnProperty(name = "management.metrics.mongo.command-listener.enabled", havingValue = "true",
matchIfMissing = true)
static class MongoMetricsCommandListenerConfiguration {
Copy link
Contributor Author

@onobc onobc Nov 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split this out into a nested config in anticipation of adding the metrics connection pool listener I alluded to in the initial description. They would be guarded by different classes and properties.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be called MongoCommandMetricsConfiguration?

Copy link
Member

@wilkinsona wilkinsona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for the PR, @Bono007. On first review, this looks excellent. I've left a few minor comments for your consideration when you have a moment.

Unfortunately, it's too late to add enhancements in 2.4, but we should hopefully be able to merge this for 2.5.

@wilkinsona wilkinsona added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 9, 2020
@wilkinsona wilkinsona added this to the 2.5.x milestone Nov 9, 2020
@onobc
Copy link
Contributor Author

onobc commented Nov 11, 2020

Thanks very much for the PR, @Bono007. On first review, this looks excellent. I've left a few minor comments for your consideration when you have a moment.

Thanks @wilkinsona . I am just now getting back to this and will update w/ suggestions shortly.

Unfortunately, it's too late to add enhancements in 2.4, but we should hopefully be able to merge this for 2.5.

No worries.

@wilkinsona wilkinsona changed the title Adds auto-configuration for Mongo metrics Auto-configure Mongo metrics Nov 11, 2020
@jonatan-ivanov
Copy link
Member

@Bono007 Would you be up to merging this and #25758 together so that both mongo metrics listeners and tag providers will be auto-configured?

@onobc
Copy link
Contributor Author

onobc commented Mar 25, 2021

Perfect timing @jonatan-ivanov. My plan was to add the connection pool metrics listener auto-config this weekend. I will instead happily merge your proposal into this one in the next 12-36hrs.

@wilkinsona are you ok w/ me combining #25758 into here?

@wilkinsona
Copy link
Member

The code in #25758 in its current form needs to be reworked as the additions have been made in spring-boot-autoconfigure rather than spring-boot-actuator-autoconfigure. I haven't looked at things in detail, but I suspect it may be quicker to redo the tag listener part on top of what's already proposed here rather than trying to merge things together.

@onobc
Copy link
Contributor Author

onobc commented Mar 25, 2021

Yep I saw that as well. Sorry for the lack of clarification @wilkinsona - I did not mean literally "merge" - but rather bring the code into this MR.

Sounds good. I will do this in the 24-36hrs.

}

@ConditionalOnClass(MongoMetricsConnectionPoolListener.class)
@ConditionalOnProperty(name = "management.metrics.mongo.connectionpoollistener.enabled", havingValue = "true",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wilkinsona it is a bit hard to not split connectionpoolllistener with hyphens in this case. I resisted the urge though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a pretty painful property name. I think we could improve the situation by keeping the fact that there's a listener involved as a lower-level detail and not exposing it in the property name. How about one of the following:

  1. management.metrics.mongo.connectionpool
  2. management.metrics.mongo.connection
  3. management.metrics.mongo.pool

Given the naming used in Micrometer, I'm leaning towards 1.

* `mongodb.driver.pool.checkedout` that reports the count of connections that are currently in use
* `mongodb.driver.pool.waitqueuesize` that reports the current size of the wait queue for a connection from the pool

NOTE: The `waitqueuesize` metric was https://github.com/mongodb/specifications/blob/master/source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#why-are-waitqueuesize-and-waitqueuemultiple-deprecated[removed] in MongoDB 4.x
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonatan-ivanov I went ahead and added the info about the waitqueuesize deprecation here as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation that's linked to here doesn't seem to be directly related to the metric. The documentation is talking about the removal of a configuration setting rather than a metric for observing the current wait queue size. Given that Micrometer's MongoMetricsConnectionPoolListener provides the metric without any reliance of Mongo Driver APIs, I don't think there's any need to mention the deprecation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Ideally those docs would live in Micrometer anyways and at best this would just point to their docs. I will remove.

@ConditionalOnClass(MongoMetricsConnectionPoolListener.class)
@ConditionalOnProperty(name = "management.metrics.mongo.connectionpoollistener.enabled", havingValue = "true",
matchIfMissing = true)
static class MongoMetricsConnectionPoolListenerConfiguration {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically everything in this nested config class was shift/lift from @jonatan-ivanov MR. Jonatan, I did not carry over the customizers concept as adding all listeners to the client settings is a more general Mongo auto-config feature and not needed specifically for the metric listeners. I could see that we may want to add that later though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be called MongoConnectionPoolMetricsConfiguration?

Copy link
Member

@wilkinsona wilkinsona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again, @Bono007. I've left a few comments for you consideration.

I've also opened micrometer-metrics/micrometer#2541 which may have an effect on this changes. This is just FYI at the moment. If the Micrometer team adopt my suggestion, we can make any updates in Boot at that time.

* `mongodb.driver.pool.checkedout` that reports the count of connections that are currently in use
* `mongodb.driver.pool.waitqueuesize` that reports the current size of the wait queue for a connection from the pool

NOTE: The `waitqueuesize` metric was https://github.com/mongodb/specifications/blob/master/source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#why-are-waitqueuesize-and-waitqueuemultiple-deprecated[removed] in MongoDB 4.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation that's linked to here doesn't seem to be directly related to the metric. The documentation is talking about the removal of a configuration setting rather than a metric for observing the current wait queue size. Given that Micrometer's MongoMetricsConnectionPoolListener provides the metric without any reliance of Mongo Driver APIs, I don't think there's any need to mention the deprecation here.

}

@ConditionalOnClass(MongoMetricsConnectionPoolListener.class)
@ConditionalOnProperty(name = "management.metrics.mongo.connectionpoollistener.enabled", havingValue = "true",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a pretty painful property name. I think we could improve the situation by keeping the fact that there's a listener involved as a lower-level detail and not exposing it in the property name. How about one of the following:

  1. management.metrics.mongo.connectionpool
  2. management.metrics.mongo.connection
  3. management.metrics.mongo.pool

Given the naming used in Micrometer, I'm leaning towards 1.

public class MongoMetricsAutoConfiguration {

@ConditionalOnClass(MongoMetricsCommandListener.class)
@ConditionalOnProperty(name = "management.metrics.mongo.commandlistener.enabled", havingValue = "true",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need listener in the name of this property. How about management.metrics.mongo.command.enabled instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[APPLY_THIS_TO_ALL_NAMING_COMMENTS]

@wilkinsona I agree that dropping the "listener" from all the naming things makes it much better and will follow up w/ that sometime today.

@ConditionalOnClass(MongoMetricsCommandListener.class)
@ConditionalOnProperty(name = "management.metrics.mongo.command-listener.enabled", havingValue = "true",
matchIfMissing = true)
static class MongoMetricsCommandListenerConfiguration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be called MongoCommandMetricsConfiguration?

@ConditionalOnClass(MongoMetricsConnectionPoolListener.class)
@ConditionalOnProperty(name = "management.metrics.mongo.connectionpoollistener.enabled", havingValue = "true",
matchIfMissing = true)
static class MongoMetricsConnectionPoolListenerConfiguration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be called MongoConnectionPoolMetricsConfiguration?

@onobc
Copy link
Contributor Author

onobc commented Mar 31, 2021

Thanks again, @Bono007. I've left a few comments for you consideration.

I've also opened micrometer-metrics/micrometer#2541 which may have an effect on this changes. This is just FYI at the moment. If the Micrometer team adopt my suggestion, we can make any updates in Boot at that time.

Oh man, that was me. I created these and while I did see the other tags provider I failed to snap to the naming not containing "Metrics". Uggh, naming. :) I will submit a proposal for these changes in Micrometer.

@onobc
Copy link
Contributor Author

onobc commented Apr 1, 2021

I made the suggested naming changes and yanked the link to the Mongo docs @wilkinsona . I like it much better. Please take a scan when you can. Thanks.

@onobc onobc requested a review from wilkinsona April 1, 2021 14:06
@@ -0,0 +1,108 @@
/*
* Copyright 2012-2020 the original author or authors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright 2012-2020 the original author or authors.
* Copyright 2020-2021 the original author or authors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2012-2021is correct here.

@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(MongoAutoConfiguration.class)
@AutoConfigureAfter({ MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
@ConditionalOnClass(MongoClientSettings.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the MongoClient available here, so that we could do @ConditionalOnClass(MongoClient.class) instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since MongoClientSettings is shared by both the reactive and blocking client this allows the listeners to be shared by both flavors.

@@ -0,0 +1,200 @@
/*
* Copyright 2012-2020 the original author or authors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright 2012-2020 the original author or authors.
* Copyright 2020-2021 the original author or authors.

Copy link
Member

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.

@wilkinsona wilkinsona self-assigned this Apr 6, 2021
@wilkinsona wilkinsona modified the milestones: 2.5.x, 2.5.0-RC1 Apr 6, 2021
@jonatan-ivanov
Copy link
Member

fyi: the tags providers were renamed, they are available in the snapshot: https://github.com/micrometer-metrics/micrometer/pull/2548/files

wilkinsona pushed a commit that referenced this pull request Apr 6, 2021
wilkinsona added a commit that referenced this pull request Apr 6, 2021
@wilkinsona wilkinsona closed this in 87dd329 Apr 6, 2021
@wilkinsona
Copy link
Member

Thanks again for the PR, @Bono007. I've merged the proposed changes along with a small polishing commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants