Skip to content

Commit a41ad61

Browse files
committed
Add internal driver information.
1 parent 73bdbd8 commit a41ad61

File tree

1 file changed

+90
-64
lines changed

1 file changed

+90
-64
lines changed

driver-core/src/main/com/mongodb/internal/connection/ClientMetadata.java

Lines changed: 90 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,27 @@
4848
/**
4949
* Represents metadata of the current MongoClient.
5050
*
51+
* Metadata is used to identify the client in the server logs and metrics.
52+
*
5153
* <p>This class is not part of the public API and may be removed or changed at any time</p>
5254
*/
5355
@ThreadSafe
5456
public class ClientMetadata {
5557
private static final String SEPARATOR = "|";
5658
private static final int MAXIMUM_CLIENT_METADATA_ENCODED_SIZE = 512;
5759
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
60+
private final String applicationName;
5861
private BsonDocument clientMetadataBsonDocument;
62+
private DriverInformation driverInformation;
5963

6064
public ClientMetadata(@Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation) {
65+
this.applicationName = applicationName;
6166
withLock(readWriteLock.writeLock(), () -> {
62-
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, mongoDriverInformation);
67+
this.driverInformation = DriverInformation.from(
68+
mongoDriverInformation.getDriverNames(),
69+
mongoDriverInformation.getDriverVersions(),
70+
mongoDriverInformation.getDriverPlatforms());
71+
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, driverInformation);
6372
});
6473
}
6574

@@ -70,14 +79,18 @@ public BsonDocument getBsonDocument() {
7079
return withLock(readWriteLock.readLock(), () -> clientMetadataBsonDocument);
7180
}
7281

73-
public void append(final MongoDriverInformation mongoDriverInformation) {
74-
withLock(readWriteLock.writeLock(), () ->
75-
this.clientMetadataBsonDocument = updateClientMetadataDocument(clientMetadataBsonDocument.clone(), mongoDriverInformation)
76-
);
82+
public void append(final MongoDriverInformation mongoDriverInformationToAppend) {
83+
withLock(readWriteLock.writeLock(), () -> {
84+
this.driverInformation.append(
85+
mongoDriverInformationToAppend.getDriverNames(),
86+
mongoDriverInformationToAppend.getDriverVersions(),
87+
mongoDriverInformationToAppend.getDriverPlatforms());
88+
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, driverInformation);
89+
});
7790
}
7891

7992
private static BsonDocument createClientMetadataDocument(@Nullable final String applicationName,
80-
@Nullable final MongoDriverInformation mongoDriverInformation) {
93+
final DriverInformation driverInformation) {
8194
if (applicationName != null) {
8295
isTrueArgument("applicationName UTF-8 encoding length <= 128",
8396
applicationName.getBytes(StandardCharsets.UTF_8).length <= 128);
@@ -86,27 +99,26 @@ private static BsonDocument createClientMetadataDocument(@Nullable final String
8699
// client fields are added in "preservation" order:
87100
BsonDocument client = new BsonDocument();
88101
tryWithLimit(client, d -> putAtPath(d, "application.name", applicationName));
89-
MongoDriverInformation baseDriverInfor = getDriverInformation(null);
102+
90103
// required fields:
91104
tryWithLimit(client, d -> {
92-
putAtPath(d, "driver.name", listToString(baseDriverInfor.getDriverNames()));
93-
putAtPath(d, "driver.version", listToString(baseDriverInfor.getDriverVersions()));
105+
putAtPath(d, "driver.name", driverInformation.getInitialDriverName());
106+
putAtPath(d, "driver.version", driverInformation.getInitialDriverVersion());
94107
});
95108
tryWithLimit(client, d -> putAtPath(d, "os.type", getOperatingSystemType(getOperatingSystemName())));
96109
// full driver information:
97-
MongoDriverInformation fullDriverInfo = getDriverInformation(mongoDriverInformation);
98110
tryWithLimit(client, d -> {
99-
putAtPath(d, "driver.name", listToString(fullDriverInfo.getDriverNames()));
100-
putAtPath(d, "driver.version", listToString(fullDriverInfo.getDriverVersions()));
111+
putAtPath(d, "driver.name", listToString(driverInformation.getAllDriverNames()));
112+
putAtPath(d, "driver.version", listToString(driverInformation.getAllDriverVersions()));
101113
});
102114

103115
// optional fields:
104116
FaasEnvironment faasEnvironment = getFaasEnvironment();
105117
ClientMetadata.ContainerRuntime containerRuntime = ClientMetadata.ContainerRuntime.determineExecutionContainer();
106118
ClientMetadata.Orchestrator orchestrator = ClientMetadata.Orchestrator.determineExecutionOrchestrator();
107119

108-
tryWithLimit(client, d -> putAtPath(d, "platform", listToString(baseDriverInfor.getDriverPlatforms())));
109-
tryWithLimit(client, d -> putAtPath(d, "platform", listToString(fullDriverInfo.getDriverPlatforms())));
120+
tryWithLimit(client, d -> putAtPath(d, "platform", driverInformation.getInitialDriverPlatform()));
121+
tryWithLimit(client, d -> putAtPath(d, "platform", listToString(driverInformation.getAllDriverPlatforms())));
110122
tryWithLimit(client, d -> putAtPath(d, "os.name", getOperatingSystemName()));
111123
tryWithLimit(client, d -> putAtPath(d, "os.architecture", getProperty("os.arch", "unknown")));
112124
tryWithLimit(client, d -> putAtPath(d, "os.version", getProperty("os.version", "unknown")));
@@ -122,45 +134,6 @@ private static BsonDocument createClientMetadataDocument(@Nullable final String
122134
return client;
123135
}
124136

125-
/**
126-
* Modifies the given client metadata document by appending the driver information.
127-
* Driver name and version are appended atomically to the existing driver name and version if they do not exceed
128-
* {@value MAXIMUM_CLIENT_METADATA_ENCODED_SIZE} bytes.
129-
* <p>
130-
* Platform is appended separately to the existing platform if it does not exceed {@value MAXIMUM_CLIENT_METADATA_ENCODED_SIZE} bytes.
131-
*/
132-
private static BsonDocument updateClientMetadataDocument(final BsonDocument clientMetadataDocument,
133-
final MongoDriverInformation driverInformationToAppend) {
134-
BsonDocument currentDriverInformation = clientMetadataDocument.getDocument("driver");
135-
136-
List<String> driverNamesToAppend = driverInformationToAppend.getDriverNames();
137-
List<String> driverVersionsToAppend = driverInformationToAppend.getDriverVersions();
138-
List<String> driverPlatformsToAppend = driverInformationToAppend.getDriverPlatforms();
139-
140-
List<String> updatedDriverNames = new ArrayList<>(driverNamesToAppend.size() + 1);
141-
List<String> updatedDriverVersions = new ArrayList<>(driverVersionsToAppend.size() + 1);
142-
List<String> updateDriverPlatforms = new ArrayList<>(driverPlatformsToAppend.size() + 1);
143-
144-
updatedDriverNames.add(currentDriverInformation.getString("name").getValue());
145-
updatedDriverNames.addAll(driverNamesToAppend);
146-
147-
updatedDriverVersions.add(currentDriverInformation.getString("version").getValue());
148-
updatedDriverVersions.addAll(driverVersionsToAppend);
149-
150-
updateDriverPlatforms.add(clientMetadataDocument.getString("platform").getValue());
151-
updateDriverPlatforms.addAll(driverPlatformsToAppend);
152-
153-
tryWithLimit(clientMetadataDocument, d -> {
154-
putAtPath(d, "driver.name", listToString(updatedDriverNames));
155-
putAtPath(d, "driver.version", listToString(updatedDriverVersions));
156-
});
157-
tryWithLimit(clientMetadataDocument, d -> {
158-
putAtPath(d, "platform", listToString(updateDriverPlatforms));
159-
});
160-
return clientMetadataDocument;
161-
}
162-
163-
164137
private static void putAtPath(final BsonDocument d, final String path, @Nullable final String value) {
165138
if (value == null) {
166139
return;
@@ -292,17 +265,6 @@ static ClientMetadata.Orchestrator determineExecutionOrchestrator() {
292265
}
293266
}
294267

295-
static MongoDriverInformation getDriverInformation(@Nullable final MongoDriverInformation mongoDriverInformation) {
296-
MongoDriverInformation.Builder builder = mongoDriverInformation != null ? MongoDriverInformation.builder(mongoDriverInformation)
297-
: MongoDriverInformation.builder();
298-
return builder
299-
.driverName(MongoDriverVersion.NAME)
300-
.driverVersion(MongoDriverVersion.VERSION)
301-
.driverPlatform(format("Java/%s/%s", getProperty("java.vendor", "unknown-vendor"),
302-
getProperty("java.runtime.version", "unknown-version")))
303-
.build();
304-
}
305-
306268
private static String listToString(final List<String> listOfStrings) {
307269
StringBuilder stringBuilder = new StringBuilder();
308270
int i = 0;
@@ -343,4 +305,68 @@ private static boolean nameStartsWith(final String name, final String... prefixe
343305
}
344306
return false;
345307
}
308+
309+
/**
310+
* Holds driver information of client.driver field
311+
* in {@link ClientMetadata#clientMetadataBsonDocument}.
312+
*/
313+
private static class DriverInformation {
314+
private final List<String> driverNames;
315+
private final List<String> driverVersions;
316+
private final List<String> driverPlatforms;
317+
private final String initialPlatform;
318+
319+
public DriverInformation() {
320+
this.driverNames = new ArrayList<>();
321+
driverNames.add(MongoDriverVersion.NAME);
322+
323+
this.driverVersions = new ArrayList<>();
324+
driverVersions.add(MongoDriverVersion.VERSION);
325+
326+
this.initialPlatform = format("Java/%s/%s", getProperty("java.vendor", "unknown-vendor"),
327+
getProperty("java.runtime.version", "unknown-version"));
328+
this.driverPlatforms = new ArrayList<>();
329+
driverPlatforms.add(initialPlatform);
330+
}
331+
332+
static DriverInformation from(final List<String> driverNames,
333+
final List<String> driverVersions,
334+
final List<String> driverPlatforms) {
335+
DriverInformation driverInformation = new DriverInformation();
336+
return driverInformation.append(driverNames, driverVersions, driverPlatforms);
337+
}
338+
339+
DriverInformation append(final List<String> driverNames,
340+
final List<String> driverVersions,
341+
final List<String> driverPlatforms) {
342+
this.driverNames.addAll(driverNames);
343+
this.driverVersions.addAll(driverVersions);
344+
this.driverPlatforms.addAll(driverPlatforms);
345+
return this;
346+
}
347+
348+
public String getInitialDriverPlatform() {
349+
return initialPlatform;
350+
}
351+
352+
public String getInitialDriverName() {
353+
return MongoDriverVersion.NAME;
354+
}
355+
356+
public String getInitialDriverVersion() {
357+
return MongoDriverVersion.VERSION;
358+
}
359+
360+
public List<String> getAllDriverNames() {
361+
return driverNames;
362+
}
363+
364+
public List<String> getAllDriverVersions() {
365+
return driverVersions;
366+
}
367+
368+
public List<String> getAllDriverPlatforms() {
369+
return driverPlatforms;
370+
}
371+
}
346372
}

0 commit comments

Comments
 (0)