48
48
/**
49
49
* Represents metadata of the current MongoClient.
50
50
*
51
+ * Metadata is used to identify the client in the server logs and metrics.
52
+ *
51
53
* <p>This class is not part of the public API and may be removed or changed at any time</p>
52
54
*/
53
55
@ ThreadSafe
54
56
public class ClientMetadata {
55
57
private static final String SEPARATOR = "|" ;
56
58
private static final int MAXIMUM_CLIENT_METADATA_ENCODED_SIZE = 512 ;
57
59
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock ();
60
+ private final String applicationName ;
58
61
private BsonDocument clientMetadataBsonDocument ;
62
+ private DriverInformation driverInformation ;
59
63
60
64
public ClientMetadata (@ Nullable final String applicationName , final MongoDriverInformation mongoDriverInformation ) {
65
+ this .applicationName = applicationName ;
61
66
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 );
63
72
});
64
73
}
65
74
@@ -70,14 +79,18 @@ public BsonDocument getBsonDocument() {
70
79
return withLock (readWriteLock .readLock (), () -> clientMetadataBsonDocument );
71
80
}
72
81
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
+ });
77
90
}
78
91
79
92
private static BsonDocument createClientMetadataDocument (@ Nullable final String applicationName ,
80
- @ Nullable final MongoDriverInformation mongoDriverInformation ) {
93
+ final DriverInformation driverInformation ) {
81
94
if (applicationName != null ) {
82
95
isTrueArgument ("applicationName UTF-8 encoding length <= 128" ,
83
96
applicationName .getBytes (StandardCharsets .UTF_8 ).length <= 128 );
@@ -86,27 +99,26 @@ private static BsonDocument createClientMetadataDocument(@Nullable final String
86
99
// client fields are added in "preservation" order:
87
100
BsonDocument client = new BsonDocument ();
88
101
tryWithLimit (client , d -> putAtPath (d , "application.name" , applicationName ));
89
- MongoDriverInformation baseDriverInfor = getDriverInformation ( null );
102
+
90
103
// required fields:
91
104
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 ( ));
94
107
});
95
108
tryWithLimit (client , d -> putAtPath (d , "os.type" , getOperatingSystemType (getOperatingSystemName ())));
96
109
// full driver information:
97
- MongoDriverInformation fullDriverInfo = getDriverInformation (mongoDriverInformation );
98
110
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 ()));
101
113
});
102
114
103
115
// optional fields:
104
116
FaasEnvironment faasEnvironment = getFaasEnvironment ();
105
117
ClientMetadata .ContainerRuntime containerRuntime = ClientMetadata .ContainerRuntime .determineExecutionContainer ();
106
118
ClientMetadata .Orchestrator orchestrator = ClientMetadata .Orchestrator .determineExecutionOrchestrator ();
107
119
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 ())));
110
122
tryWithLimit (client , d -> putAtPath (d , "os.name" , getOperatingSystemName ()));
111
123
tryWithLimit (client , d -> putAtPath (d , "os.architecture" , getProperty ("os.arch" , "unknown" )));
112
124
tryWithLimit (client , d -> putAtPath (d , "os.version" , getProperty ("os.version" , "unknown" )));
@@ -122,45 +134,6 @@ private static BsonDocument createClientMetadataDocument(@Nullable final String
122
134
return client ;
123
135
}
124
136
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
-
164
137
private static void putAtPath (final BsonDocument d , final String path , @ Nullable final String value ) {
165
138
if (value == null ) {
166
139
return ;
@@ -292,17 +265,6 @@ static ClientMetadata.Orchestrator determineExecutionOrchestrator() {
292
265
}
293
266
}
294
267
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
-
306
268
private static String listToString (final List <String > listOfStrings ) {
307
269
StringBuilder stringBuilder = new StringBuilder ();
308
270
int i = 0 ;
@@ -343,4 +305,68 @@ private static boolean nameStartsWith(final String name, final String... prefixe
343
305
}
344
306
return false ;
345
307
}
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
+ }
346
372
}
0 commit comments