Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 15 additions & 32 deletions driver-core/src/main/com/mongodb/MongoDriverInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.mongodb;

import com.mongodb.annotations.Internal;
import com.mongodb.annotations.NotThreadSafe;
import com.mongodb.annotations.Sealed;
import com.mongodb.internal.client.DriverInformation;
import com.mongodb.internal.client.DriverInformationHelper;
import com.mongodb.internal.connection.ConcreteMongoDriverInformation;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -47,8 +47,9 @@
* @since 3.4
* @mongodb.server.release 3.4
*/
public final class MongoDriverInformation {
private final List<DriverInformation> driverInformationList;
@Sealed
public abstract class MongoDriverInformation {


/**
* Convenience method to create a Builder.
Expand All @@ -66,50 +67,36 @@ public static Builder builder() {
* @return a builder
*/
public static Builder builder(final MongoDriverInformation mongoDriverInformation) {
return new Builder(mongoDriverInformation);
return new Builder((ConcreteMongoDriverInformation) mongoDriverInformation);
}

/**
* Returns the driverNames
*
* @return the driverNames
*/
public List<String> getDriverNames() {
return DriverInformationHelper.getNames(driverInformationList);
}
public abstract List<String> getDriverNames();

/**
* Returns the driverVersions
*
* @return the driverVersions
*/
public List<String> getDriverVersions() {
return DriverInformationHelper.getVersions(driverInformationList);
}
public abstract List<String> getDriverVersions();

/**
* Returns the driverPlatforms
*
* @return the driverPlatforms
*/
public List<String> getDriverPlatforms() {
return DriverInformationHelper.getPlatforms(driverInformationList);
}

/**
* For internal use only
*/
@Internal
public List<DriverInformation> getDriverInformationList() {
return driverInformationList;
}
public abstract List<String> getDriverPlatforms();

/**
*
*/
@NotThreadSafe
public static final class Builder {
private final MongoDriverInformation mongoDriverInformation;
private final ConcreteMongoDriverInformation mongoDriverInformation;
private String driverName;
private String driverVersion;
private String driverPlatform;
Expand Down Expand Up @@ -156,25 +143,21 @@ public Builder driverPlatform(final String driverPlatform) {
*/
public MongoDriverInformation build() {
DriverInformation driverInformation = new DriverInformation(driverName, driverVersion, driverPlatform);
if (mongoDriverInformation.driverInformationList.contains(driverInformation)) {
if (mongoDriverInformation.getDriverInformationList().contains(driverInformation)) {
return mongoDriverInformation;
}

List<DriverInformation> driverInformationList = new ArrayList<>(mongoDriverInformation.driverInformationList);
List<DriverInformation> driverInformationList = new ArrayList<>(mongoDriverInformation.getDriverInformationList());
driverInformationList.add(driverInformation);
return new MongoDriverInformation(Collections.unmodifiableList(driverInformationList));
return new ConcreteMongoDriverInformation(Collections.unmodifiableList(driverInformationList));
}

private Builder() {
mongoDriverInformation = new MongoDriverInformation(Collections.emptyList());
mongoDriverInformation = new ConcreteMongoDriverInformation(Collections.emptyList());
}

private Builder(final MongoDriverInformation driverInformation) {
private Builder(final ConcreteMongoDriverInformation driverInformation) {
this.mongoDriverInformation = notNull("driverInformation", driverInformation);
}
}

private MongoDriverInformation(final List<DriverInformation> driverInformation) {
this.driverInformationList = notNull("driverInformation", driverInformation);
}
}
11 changes: 6 additions & 5 deletions driver-core/src/main/com/mongodb/MongoNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.mongodb;

import com.mongodb.annotations.Immutable;
import com.mongodb.annotations.Internal;
import org.bson.codecs.pojo.annotations.BsonCreator;
import org.bson.codecs.pojo.annotations.BsonIgnore;
import org.bson.codecs.pojo.annotations.BsonProperty;
Expand All @@ -44,12 +43,14 @@ public final class MongoNamespace {
private static final Set<Character> PROHIBITED_CHARACTERS_IN_DATABASE_NAME =
new HashSet<>(asList('\0', '/', '\\', ' ', '"', '.'));

@Internal
/**
* The collection name in which to execute a command.
* @deprecated there is no replacement for this constant, as it is only needed for the OP_QUERY wire protocol message, which has
* been replaced by OP_MSG
*/
@Deprecated
public static final String COMMAND_COLLECTION_NAME = "$cmd";

@Internal
public static final MongoNamespace ADMIN_DB_COMMAND_NAMESPACE = new MongoNamespace("admin", COMMAND_COLLECTION_NAME);

private final String databaseName;
private final String collectionName;
@BsonIgnore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal;

import com.mongodb.MongoNamespace;

public final class MongoNamespaceHelper {

public static final String COMMAND_COLLECTION_NAME = "$cmd";
public static final MongoNamespace ADMIN_DB_COMMAND_NAMESPACE = new MongoNamespace("admin", COMMAND_COLLECTION_NAME);

private MongoNamespaceHelper() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.mongodb.annotations.ThreadSafe;
import com.mongodb.internal.VisibleForTesting;
import com.mongodb.internal.client.DriverInformation;
import com.mongodb.internal.client.DriverInformationHelper;
import com.mongodb.lang.Nullable;
import org.bson.BsonBinaryWriter;
import org.bson.BsonDocument;
Expand All @@ -41,7 +40,7 @@

import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.internal.Locks.withLock;
import static com.mongodb.internal.client.DriverInformationHelper.INITIAL_DRIVER_INFORMATION;
import static com.mongodb.internal.connection.ConcreteMongoDriverInformation.INITIAL_DRIVER_INFORMATION;
import static com.mongodb.internal.connection.FaasEnvironment.getFaasEnvironment;
import static java.lang.System.getProperty;
import static java.nio.file.Paths.get;
Expand All @@ -67,7 +66,7 @@ public ClientMetadata(@Nullable final String applicationName, final MongoDriverI
this.driverInformationList = new ArrayList<>();
withLock(readWriteLock.writeLock(), () -> {
driverInformationList.add(INITIAL_DRIVER_INFORMATION);
driverInformationList.addAll(mongoDriverInformation.getDriverInformationList());
driverInformationList.addAll(((ConcreteMongoDriverInformation) mongoDriverInformation).getDriverInformationList());
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, driverInformationList);
});
}
Expand All @@ -82,7 +81,8 @@ public BsonDocument getBsonDocument() {
public void append(final MongoDriverInformation mongoDriverInformationToAppend) {
withLock(readWriteLock.writeLock(), () -> {
boolean hasAddedNewData = false;
for (DriverInformation driverInformation : mongoDriverInformationToAppend.getDriverInformationList()) {
for (DriverInformation driverInformation
: ((ConcreteMongoDriverInformation) mongoDriverInformationToAppend).getDriverInformationList()) {
if (!driverInformationList.contains(driverInformation)) {
hasAddedNewData = true;
driverInformationList.add(driverInformation);
Expand Down Expand Up @@ -112,9 +112,10 @@ private static BsonDocument createClientMetadataDocument(@Nullable final String
});
tryWithLimit(clientMetadata, d -> putAtPath(d, "os.type", getOperatingSystemType(getOperatingSystemName())));
// full driver information:
ConcreteMongoDriverInformation mongoDriverInformation = new ConcreteMongoDriverInformation(driverInformationList);
tryWithLimit(clientMetadata, d -> {
putAtPath(d, "driver.name", listToString(DriverInformationHelper.getNames(driverInformationList)));
putAtPath(d, "driver.version", listToString(DriverInformationHelper.getVersions(driverInformationList)));
putAtPath(d, "driver.name", listToString(mongoDriverInformation.getDriverNames()));
putAtPath(d, "driver.version", listToString(mongoDriverInformation.getDriverVersions()));
});

// optional fields:
Expand All @@ -123,7 +124,7 @@ private static BsonDocument createClientMetadataDocument(@Nullable final String
ClientMetadata.Orchestrator orchestrator = ClientMetadata.Orchestrator.determineExecutionOrchestrator();

tryWithLimit(clientMetadata, d -> putAtPath(d, "platform", INITIAL_DRIVER_INFORMATION.getDriverPlatform()));
tryWithLimit(clientMetadata, d -> putAtPath(d, "platform", listToString(DriverInformationHelper.getPlatforms(driverInformationList))));
tryWithLimit(clientMetadata, d -> putAtPath(d, "platform", listToString(mongoDriverInformation.getDriverPlatforms())));
tryWithLimit(clientMetadata, d -> putAtPath(d, "os.name", getOperatingSystemName()));
tryWithLimit(clientMetadata, d -> putAtPath(d, "os.architecture", getProperty("os.arch", "unknown")));
tryWithLimit(clientMetadata, d -> putAtPath(d, "os.version", getProperty("os.version", "unknown")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.mongodb.ReadPreference;
import com.mongodb.ServerApi;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.internal.MongoNamespaceHelper;
import com.mongodb.internal.TimeoutContext;
import com.mongodb.internal.connection.MessageSequences.EmptyMessageSequences;
import com.mongodb.internal.session.SessionContext;
Expand Down Expand Up @@ -283,7 +284,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon

private int writeOpQuery(final ByteBufferBsonOutput bsonOutput) {
bsonOutput.writeInt32(0);
bsonOutput.writeCString(new MongoNamespace(getDatabase(), "$cmd").getFullName());
bsonOutput.writeCString(new MongoNamespace(getDatabase(), MongoNamespaceHelper.COMMAND_COLLECTION_NAME).getFullName());
bsonOutput.writeInt32(0);
bsonOutput.writeInt32(-1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,58 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.client;

package com.mongodb.internal.connection;

import com.mongodb.MongoDriverInformation;
import com.mongodb.internal.build.MongoDriverVersion;
import com.mongodb.internal.client.DriverInformation;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.mongodb.assertions.Assertions.notNull;
import static java.lang.String.format;
import static java.lang.System.getProperty;

public final class DriverInformationHelper {

public final class ConcreteMongoDriverInformation extends MongoDriverInformation {
public static final DriverInformation INITIAL_DRIVER_INFORMATION =
new DriverInformation(MongoDriverVersion.NAME, MongoDriverVersion.VERSION,
format("Java/%s/%s", getProperty("java.vendor", "unknown-vendor"),
getProperty("java.runtime.version", "unknown-version")));
getProperty("java.runtime.version", "unknown-version")));

private final List<DriverInformation> driverInformationList;

public ConcreteMongoDriverInformation(final List<DriverInformation> driverInformation) {
this.driverInformationList = notNull("driverInformation", driverInformation);
}

public static List<String> getNames(final List<DriverInformation> driverInformation) {
return getDriverField(DriverInformation::getDriverName, driverInformation);
@Override
public List<String> getDriverNames() {
return getDriverField(DriverInformation::getDriverName);
}

public static List<String> getVersions(final List<DriverInformation> driverInformation) {
return getDriverField(DriverInformation::getDriverVersion, driverInformation);
@Override
public List<String> getDriverVersions() {
return getDriverField(DriverInformation::getDriverVersion);
}

public static List<String> getPlatforms(final List<DriverInformation> driverInformation) {
return getDriverField(DriverInformation::getDriverPlatform, driverInformation);
@Override
public List<String> getDriverPlatforms() {
return getDriverField(DriverInformation::getDriverPlatform);
}

private static List<String> getDriverField(final Function<DriverInformation, String> fieldSupplier,
final List<DriverInformation> driverInformation) {
return Collections.unmodifiableList(driverInformation.stream()
public List<DriverInformation> getDriverInformationList() {
return driverInformationList;
}

private List<String> getDriverField(final Function<DriverInformation, String> fieldSupplier) {
return Collections.unmodifiableList(driverInformationList.stream()
.map(fieldSupplier)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
}

private DriverInformationHelper() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import com.mongodb.internal.diagnostics.logging.Logger;
import com.mongodb.internal.diagnostics.logging.Loggers;
import com.mongodb.internal.logging.StructuredLogger;
import com.mongodb.internal.observability.micrometer.Span;
import com.mongodb.internal.session.SessionContext;
import com.mongodb.internal.time.Timeout;
import com.mongodb.internal.tracing.Span;
import com.mongodb.lang.Nullable;
import org.bson.BsonBinaryReader;
import org.bson.BsonDocument;
Expand Down Expand Up @@ -94,9 +94,9 @@
import static com.mongodb.internal.connection.ProtocolHelper.getSnapshotTimestamp;
import static com.mongodb.internal.connection.ProtocolHelper.isCommandOk;
import static com.mongodb.internal.logging.LogMessage.Level.DEBUG;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.HighCardinalityKeyNames.QUERY_TEXT;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.RESPONSE_STATUS_CODE;
import static com.mongodb.internal.thread.InterruptionUtil.translateInterruptedException;
import static com.mongodb.internal.tracing.MongodbObservation.HighCardinalityKeyNames.QUERY_TEXT;
import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.RESPONSE_STATUS_CODE;
import static java.util.Arrays.asList;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import com.mongodb.internal.TimeoutContext;
import com.mongodb.internal.TimeoutSettings;
import com.mongodb.internal.VisibleForTesting;
import com.mongodb.internal.observability.micrometer.Span;
import com.mongodb.internal.observability.micrometer.TracingManager;
import com.mongodb.internal.session.SessionContext;
import com.mongodb.internal.tracing.Span;
import com.mongodb.internal.tracing.TracingManager;
import com.mongodb.lang.Nullable;
import com.mongodb.selector.ServerSelector;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.mongodb.internal.tracing;
package com.mongodb.internal.observability.micrometer;

import com.mongodb.MongoNamespace;
import com.mongodb.lang.Nullable;
Expand All @@ -33,11 +33,11 @@
import java.io.PrintWriter;
import java.io.StringWriter;

import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.EXCEPTION_MESSAGE;
import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.EXCEPTION_STACKTRACE;
import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.EXCEPTION_TYPE;
import static com.mongodb.internal.tracing.MongodbObservation.MONGODB_OBSERVATION;
import static com.mongodb.observability.MicrometerObservabilitySettings.ENV_OBSERVABILITY_QUERY_TEXT_MAX_LENGTH;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.EXCEPTION_MESSAGE;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.EXCEPTION_STACKTRACE;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.LowCardinalityKeyNames.EXCEPTION_TYPE;
import static com.mongodb.internal.observability.micrometer.MongodbObservation.MONGODB_OBSERVATION;
import static com.mongodb.internal.observability.micrometer.TracingManager.ENV_OBSERVABILITY_QUERY_TEXT_MAX_LENGTH;
import static java.lang.System.getenv;
import static java.util.Optional.ofNullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.mongodb.internal.tracing;
package com.mongodb.internal.observability.micrometer;

import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.Observation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.mongodb.internal.tracing;
package com.mongodb.internal.observability.micrometer;

import com.mongodb.MongoNamespace;
import com.mongodb.lang.Nullable;
Expand Down
Loading