Skip to content
Draft
35 changes: 35 additions & 0 deletions driver-core/src/main/com/mongodb/FeatureHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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;

/**
* The handle of one feature label of the telemetry POC. A framework registers the handle once with
* {@link MongoDriverInformation#register} and keeps it. The framework calls {@link #used()} at the execution site of the feature, on
* first use.
* <p>
* The handle is a latch: it flips once, then stays flipped. After the first call, every later call is one cheap read. The handle never
* touches any set or map on any use path.
* <p>
* POC scope: this interface is the entire public label surface of the telemetry POC.
*/
public interface FeatureHandle {

/**
* Flips the latch on first use of the feature.
*/
void used();
}
18 changes: 18 additions & 0 deletions driver-core/src/main/com/mongodb/MongoDriverInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.mongodb.annotations.Sealed;
import com.mongodb.internal.client.DriverInformation;
import com.mongodb.internal.connection.ConcreteMongoDriverInformation;
import com.mongodb.internal.telemetry.TelemetryFeatures;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -91,6 +92,23 @@ public static Builder builder(final MongoDriverInformation mongoDriverInformatio
*/
public abstract List<String> getDriverPlatforms();

/**
* Registers one feature label of the telemetry POC and returns its handle. A framework calls this method once per label, on the
* cold path, and keeps the handle. The framework calls {@link FeatureHandle#used()} at the execution site of the feature, on first
* use. The driver's own features register through this same method.
* <p>
* The method does not change the value object: the label store is process-wide. The returned handle stays usable forever, because
* frameworks register lazily, after the handshake.
* <p>
* POC scope: this method is the entire public label surface of the telemetry POC.
*
* @param label the label name of the feature
* @return the handle of the label
*/
public FeatureHandle register(final String label) {
return TelemetryFeatures.register(label);
}

/**
*
*/
Expand Down
46 changes: 44 additions & 2 deletions driver-core/src/main/com/mongodb/client/model/Filters.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.mongodb.client.model.search.SearchCollector;
import com.mongodb.client.model.search.SearchOperator;
import com.mongodb.client.model.search.SearchOptions;
import com.mongodb.internal.telemetry.ApiCounter;
import com.mongodb.internal.telemetry.TelemetryStore;
import com.mongodb.lang.Nullable;
import org.bson.BsonArray;
import org.bson.BsonBoolean;
Expand All @@ -33,6 +35,7 @@
import org.bson.BsonString;
import org.bson.BsonType;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;

Expand Down Expand Up @@ -462,7 +465,14 @@ public static Bson text(final String search, final TextSearchOptions textSearchO
*/
public static Bson where(final String javaScriptExpression) {
notNull("javaScriptExpression", javaScriptExpression);
return new BsonDocument("$where", new BsonString(javaScriptExpression));
// The wrapper implements Bson directly, never BsonDocument. The combinator child check never fires for it.
return new Bson() {
@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
return new BsonDocument("$where", new BsonString(javaScriptExpression));
}
};
}

/**
Expand Down Expand Up @@ -520,6 +530,8 @@ public static Bson elemMatch(final String fieldName, final Bson filter) {
return new Bson() {
@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
countRawChild(filter);
return new BsonDocument(fieldName, new BsonDocument("$elemMatch", filter.toBsonDocument(documentClass, codecRegistry)));
}
};
Expand Down Expand Up @@ -866,7 +878,14 @@ public static Bson jsonSchema(final Bson schema) {
* @since 4.2
*/
public static Bson empty() {
return new BsonDocument();
// The wrapper implements Bson directly, never BsonDocument. The combinator child check never fires for it.
return new Bson() {
@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
return new BsonDocument();
}
};
}


Expand All @@ -882,6 +901,17 @@ private static Bson createNearFilterDocument(final String fieldName, final doubl
return new BsonDocument(fieldName, nearFilter);
}

/**
* Counts one raw child of a combinator node before the child renders. A raw child is a {@code Document} or a {@code BsonDocument}.
* The check is one instanceof pair. The method does not walk the document. A raw root never enters a {@code Filters} class, so the
* counter sees raw nodes inside builder-wrapped trees only.
*/
private static void countRawChild(final Bson child) {
if (child instanceof Document || child instanceof BsonDocument) {
TelemetryStore.increment(ApiCounter.QUERY_RAW);
}
}

private static String operatorFilterToString(final String fieldName, final String operator, final Object value) {
return "Operator Filter{"
+ "fieldName='" + fieldName + '\''
Expand All @@ -901,6 +931,7 @@ private SimpleFilter(final String fieldName, final BsonValue value) {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
return new BsonDocument(fieldName, value);
}

Expand Down Expand Up @@ -947,6 +978,7 @@ private static final class OperatorFilter<TItem> implements Bson {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());

writer.writeStartDocument();
Expand Down Expand Up @@ -1003,8 +1035,10 @@ private static class AndFilter implements Bson {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
BsonArray clauses = new BsonArray();
for (Bson filter : filters) {
countRawChild(filter);
clauses.add(filter.toBsonDocument(documentClass, codecRegistry));
}

Expand Down Expand Up @@ -1062,10 +1096,12 @@ private enum Operator {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
BsonDocument orRenderable = new BsonDocument();

BsonArray filtersArray = new BsonArray();
for (Bson filter : filters) {
countRawChild(filter);
filtersArray.add(filter.toBsonDocument(documentClass, codecRegistry));
}

Expand Down Expand Up @@ -1119,6 +1155,7 @@ private static class IterableOperatorFilter<TItem> implements Bson {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());

writer.writeStartDocument();
Expand Down Expand Up @@ -1183,6 +1220,7 @@ private static class SimpleEncodingFilter<TItem> implements Bson {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());

writer.writeStartDocument();
Expand Down Expand Up @@ -1237,6 +1275,8 @@ private static class NotFilter implements Bson {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
countRawChild(filter);
BsonDocument filterDocument = filter.toBsonDocument(documentClass, codecRegistry);
if (filterDocument.size() == 1) {
Map.Entry<String, BsonValue> entry = filterDocument.entrySet().iterator().next();
Expand Down Expand Up @@ -1323,6 +1363,7 @@ private static class GeometryOperatorFilter<TItem> implements Bson {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
writer.writeStartDocument();
writer.writeName(fieldName);
Expand Down Expand Up @@ -1403,6 +1444,7 @@ private static class TextFilter implements Bson {

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
TelemetryStore.increment(ApiCounter.QUERY_BUILDERS);
BsonDocument searchDocument = new BsonDocument("$search", new BsonString(search));

String language = textSearchOptions.getLanguage();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.telemetry;

/**
* One counter key of the telemetry POC registry. The registry covers the two Product P0 questions: query construction and bulk writes.
* <p>
* Append new values at the end only. Never reuse an ordinal. The ordinal is the array index of the counter store. A changed ordinal
* breaks the index.
* <p>
* This class is not part of the public API and may be removed or changed at any time.
*/
public enum ApiCounter {
/**
* The user built a query with a typed builder helper, for example {@code Filters.eq}.
*/
QUERY_BUILDERS("queryBuilders"),
/**
* The user passed a raw filter, as a {@code Document} or a {@code BsonDocument}, to a query method.
*/
QUERY_RAW("queryRaw"),
/**
* The user called the client-level bulk write API.
*/
BULK_WRITE_CLIENT("bulkWriteClient"),
/**
* The user called the collection-level bulk write API.
*/
BULK_WRITE_COLLECTION("bulkWriteCollection"),
/**
* The user started a legacy ordered or unordered bulk operation.
*/
BULK_WRITE_LEGACY("bulkWriteLegacy");

private final String key;

ApiCounter(final String key) {
this.key = key;
}

/**
* Gets the key name of the counter. The sampler writes this name into the log line.
*
* @return the key name
*/
public String key() {
return key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.telemetry;

import com.mongodb.FeatureHandle;

import java.util.concurrent.atomic.AtomicBoolean;

import static com.mongodb.assertions.Assertions.notNull;

/**
* The handle of one feature label of the telemetry POC. {@link TelemetryFeatures#register} creates the handle. The handle is a latch:
* it flips once, then stays flipped.
* <p>
* {@link #used()} touches only the handle's own {@link AtomicBoolean}. The registered set is never touched on any use path, first use
* included. The kill switch turns {@code used()} into a no-op: a use under the switch does not latch.
* <p>
* This class is not part of the public API and may be removed or changed at any time.
*/
public final class DriverFeatureHandle implements FeatureHandle {

final String label;
private final AtomicBoolean used = new AtomicBoolean(false);

DriverFeatureHandle(final String label) {
this.label = notNull("label", label);
}

/**
* Flips the latch on first use. The method is one cheap read in the steady state. The method never throws.
*/
@Override
public void used() {
if (!TelemetryStore.isEnabled()) {
return;
}
if (!used.get()) {
used.compareAndSet(false, true);
}
}

/**
* Reports whether the latch is flipped. The sampler calls this method once per window.
*
* @return true when the feature was used at least once
*/
boolean usedFlag() {
return used.get();
}

/**
* Unflips the latch. Only {@link TelemetryFeatures#resetForTest} calls this method.
*/
void resetLatch() {
used.set(false);
}
}
Loading