Skip to content

Add MongoClientSettingsBuilderCustomizer hook for the Mongo client#15716

Merged
codeconsole merged 1 commit into
apache:8.0.xfrom
codeconsole:feat/mongo-client-settings-customizer
Jun 5, 2026
Merged

Add MongoClientSettingsBuilderCustomizer hook for the Mongo client#15716
codeconsole merged 1 commit into
apache:8.0.xfrom
codeconsole:feat/mongo-client-settings-customizer

Conversation

@codeconsole
Copy link
Copy Markdown
Contributor

What

Adds a MongoClientSettingsBuilderCustomizer extension point to GORM for MongoDB. Any beans of this type in the application context are applied, in order, to the MongoClientSettings.Builder of the default connection source before the MongoClient is created.

Why

There is currently no supported way to customize MongoClientSettings for things that have no grails.mongodb.* configuration equivalent — most notably registering a driver com.mongodb.event.CommandListener for metrics/tracing (e.g. Micrometer's MongoMetricsCommandListener), but also programmatic connection-pool tuning, read/write concerns, etc. Today that requires replacing the datastore wiring.

Mirrors Spring Boot's MongoClientSettingsBuilderCustomizer of the same name, giving GORM-MongoDB users the same ergonomics.

How

  • New functional interface org.grails.datastore.mapping.mongo.connections.MongoClientSettingsBuilderCustomizer.
  • MongoConnectionSourceFactory gains an @Autowired(required = false) List<MongoClientSettingsBuilderCustomizer> applied in create() just before MongoClients.create(...). The factory is already registered with bean.autowire = true in MongoDbDataStoreSpringInitializer, so customizer beans are injected automatically. No behavior change when none are defined.

Example

@Bean
MongoClientSettingsBuilderCustomizer mongoMetrics(MeterRegistry registry) {
    return { builder -> builder.addCommandListener(new MongoMetricsCommandListener(registry)) }
}

Tests

Added to MongoConnectionSourceFactorySpec:

  • a registered customizer is applied, and its command listener lands in the built settings
  • multiple customizers run in registration order

@testlens-app

This comment has been minimized.

@matrei
Copy link
Copy Markdown
Contributor

matrei commented Jun 5, 2026

@codeconsole Can this target 8?
Decided in the weekly meeting May 27th, point 1, 7.x.x is not accepting new features.
Focus should now be on 8+.

@codeconsole codeconsole changed the base branch from 7.2.x to 8.0.x June 5, 2026 06:17
@codeconsole
Copy link
Copy Markdown
Contributor Author

@codeconsole Can this target 8? Decided in the weekly meeting May 27th, point 1, 7.x.x is not accepting new features. Focus should now be on 8+.

@matrei done, how about a review ;P

Allow application beans to customize the MongoClientSettings.Builder before the
MongoClient is created for the default connection source. This is the supported
extension point for settings with no grails.mongodb.* equivalent — e.g.
registering a driver CommandListener for metrics/tracing, tuning the connection
pool, or configuring read/write concerns programmatically.

Any MongoClientSettingsBuilderCustomizer beans in the context are injected into
MongoConnectionSourceFactory (registered with autowire) and applied, in order,
in create(). Mirrors Spring Boot's interface of the same name.
@codeconsole codeconsole force-pushed the feat/mongo-client-settings-customizer branch from ef4d76c to d37eba7 Compare June 5, 2026 16:34
@codeconsole codeconsole merged commit 41f9fb9 into apache:8.0.x Jun 5, 2026
33 of 36 checks passed
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 5, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 48.3694%. Comparing base (098660a) to head (d37eba7).
⚠️ Report is 2 commits behind head on 8.0.x.

Files with missing lines Patch % Lines
...go/connections/MongoConnectionSourceFactory.groovy 50.0000% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #15716        +/-   ##
==================================================
- Coverage     48.3729%   48.3694%   -0.0035%     
- Complexity      15108      15109         +1     
==================================================
  Files            1870       1870                
  Lines           85457      85459         +2     
  Branches        14900      14901         +1     
==================================================
- Hits            41338      41336         -2     
- Misses          37784      37785         +1     
- Partials         6335       6338         +3     
Files with missing lines Coverage Δ
...go/connections/MongoConnectionSourceFactory.groovy 60.7143% <50.0000%> (-0.8242%) ⬇️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants