Skip to content

Commit 5ea6b53

Browse files
jyeminDavideD
authored andcommitted
OGM-1589: Add hibernate-ogm metadata to MongoClient construction
This change allows users to track which connections are coming from hibernate-ogm by examining the MongoDB server logs, by looking for log statements like this: { "t": { "$date": "2024-09-05T11:52:33.248-04:00" }, "s": "D2", "c": "COMMAND", "id": 21965, "ctx": "conn304", "commandArgs": { "isMaster": 1, "helloOk": true, "client": { "driver": { "name": "mongo-java-driver|legacy|hibernate-ogm", "version": "4.11.3|5.4.2" }, "os": { "type": "Darwin", "name": "Mac OS X", "architecture": "x86_64", "version": "14.6.1" }, "platform": "Java/Amazon.com Inc./1.8.0_422-b05" }, "$db": "admin" } } Note the driver name and version refer to both the underlying MongoDB Java driver and hibernate-ogm.
1 parent 5bc01d4 commit 5ea6b53

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

mongodb/src/main/java/org/hibernate/ogm/datastore/mongodb/impl/MongoDBDatastoreProvider.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
import com.mongodb.MongoClient;
1414
import com.mongodb.MongoClientOptions;
1515
import com.mongodb.MongoCredential;
16+
import com.mongodb.MongoDriverInformation;
1617
import com.mongodb.MongoException;
1718
import com.mongodb.ServerAddress;
1819
import com.mongodb.client.MongoCursor;
1920
import com.mongodb.client.MongoDatabase;
2021

2122
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
2223
import org.hibernate.engine.jndi.spi.JndiService;
24+
import org.hibernate.ogm.cfg.impl.Version;
2325
import org.hibernate.ogm.cfg.spi.Hosts;
2426
import org.hibernate.ogm.datastore.mongodb.MongoDBDialect;
2527
import org.hibernate.ogm.datastore.mongodb.binarystorage.GridFSStorageManager;
@@ -160,7 +162,11 @@ protected MongoClient createMongoClient(MongoDBConfiguration config) {
160162
for ( Hosts.HostAndPort hostAndPort : config.getHosts() ) {
161163
serverAddresses.add( new ServerAddress( hostAndPort.getHost(), hostAndPort.getPort() ) );
162164
}
163-
return new MongoClient( serverAddresses, credential, clientOptions );
165+
return new MongoClient( serverAddresses, credential, clientOptions,
166+
MongoDriverInformation.builder()
167+
.driverName( "hibernate-ogm" )
168+
.driverVersion( Version.getVersionString() )
169+
.build() );
164170
}
165171
catch (RuntimeException e) {
166172
throw log.unableToInitializeMongoDB( e );

0 commit comments

Comments
 (0)