3030import com .mongodb .connection .SslSettings ;
3131import com .mongodb .connection .TransportSettings ;
3232import com .mongodb .event .CommandListener ;
33- import com .mongodb .internal .VisibleForTesting ;
3433import com .mongodb .lang .Nullable ;
3534import com .mongodb .spi .dns .DnsClient ;
3635import com .mongodb .spi .dns .InetAddressResolver ;
37- import io . micrometer . observation . ObservationRegistry ;
36+ import com . mongodb . tracing . TracingSettings ;
3837import org .bson .UuidRepresentation ;
3938import org .bson .codecs .BsonCodecProvider ;
4039import org .bson .codecs .BsonValueCodecProvider ;
5958import static com .mongodb .assertions .Assertions .isTrueArgument ;
6059import static com .mongodb .assertions .Assertions .notNull ;
6160import static com .mongodb .internal .TimeoutSettings .convertAndValidateTimeout ;
62- import static com .mongodb .internal .VisibleForTesting .AccessModifier .PRIVATE ;
6361import static java .util .Arrays .asList ;
6462import static java .util .concurrent .TimeUnit .MILLISECONDS ;
6563import static org .bson .codecs .configuration .CodecRegistries .fromProviders ;
@@ -119,18 +117,10 @@ public final class MongoClientSettings {
119117 private final ContextProvider contextProvider ;
120118 private final DnsClient dnsClient ;
121119 private final InetAddressResolver inetAddressResolver ;
120+ private final TracingSettings tracingSettings ;
122121 @ Nullable
123122 private final Long timeoutMS ;
124123
125- @ VisibleForTesting (otherwise = PRIVATE )
126- // If set, this will enable/disable tracing even when an observationRegistry has been passed.
127- public static final String ENV_OTEL_ENABLED = "OTEL_JAVA_INSTRUMENTATION_MONGODB_ENABLED" ;
128- @ VisibleForTesting (otherwise = PRIVATE )
129- // If set, this will truncate the command payload captured in the tracing span to the specified length.
130- public static final String ENV_OTEL_QUERY_TEXT_MAX_LENGTH = "OTEL_JAVA_INSTRUMENTATION_MONGODB_QUERY_TEXT_MAX_LENGTH" ;
131- private final ObservationRegistry observationRegistry ;
132- private final boolean enableCommandPayloadTracing ;
133-
134124 /**
135125 * Gets the default codec registry. It includes the following providers:
136126 *
@@ -235,6 +225,7 @@ public static final class Builder {
235225 private final ConnectionPoolSettings .Builder connectionPoolSettingsBuilder = ConnectionPoolSettings .builder ();
236226 private final ServerSettings .Builder serverSettingsBuilder = ServerSettings .builder ();
237227 private final SslSettings .Builder sslSettingsBuilder = SslSettings .builder ();
228+ private final TracingSettings .Builder tracingSettingsBuilder = TracingSettings .builder ();
238229 private MongoCredential credential ;
239230 private String applicationName ;
240231 private List <MongoCompressor > compressorList = Collections .emptyList ();
@@ -250,8 +241,6 @@ public static final class Builder {
250241 private ContextProvider contextProvider ;
251242 private DnsClient dnsClient ;
252243 private InetAddressResolver inetAddressResolver ;
253- private ObservationRegistry observationRegistry ;
254- private boolean enableCommandPayloadTracing ;
255244
256245 private Builder () {
257246 }
@@ -282,15 +271,14 @@ private Builder(final MongoClientSettings settings) {
282271 socketSettingsBuilder .applySettings (settings .getSocketSettings ());
283272 connectionPoolSettingsBuilder .applySettings (settings .getConnectionPoolSettings ());
284273 sslSettingsBuilder .applySettings (settings .getSslSettings ());
274+ tracingSettingsBuilder .applySettings (settings .getTracingSettings ());
285275
286276 if (settings .heartbeatConnectTimeoutSetExplicitly ) {
287277 heartbeatConnectTimeoutMS = settings .heartbeatSocketSettings .getConnectTimeout (MILLISECONDS );
288278 }
289279 if (settings .heartbeatSocketTimeoutSetExplicitly ) {
290280 heartbeatSocketTimeoutMS = settings .heartbeatSocketSettings .getReadTimeout (MILLISECONDS );
291281 }
292- observationRegistry = settings .observationRegistry ;
293- enableCommandPayloadTracing = settings .enableCommandPayloadTracing ;
294282 }
295283
296284 /**
@@ -415,6 +403,18 @@ public Builder applyToSslSettings(final Block<SslSettings.Builder> block) {
415403 return this ;
416404 }
417405
406+ /**
407+ * Applies the {@link TracingSettings.Builder} block and then sets the tracingSettings.
408+ *
409+ * @param block the block to apply to the TracingSettings.
410+ * @return this
411+ * @see MongoClientSettings#getTracingSettings()
412+ */
413+ public Builder applyToTracingSettings (final Block <TracingSettings .Builder > block ) {
414+ notNull ("block" , block ).apply (tracingSettingsBuilder );
415+ return this ;
416+ }
417+
418418 /**
419419 * Sets the read preference.
420420 *
@@ -739,47 +739,6 @@ Builder heartbeatSocketTimeoutMS(final int heartbeatSocketTimeoutMS) {
739739 return this ;
740740 }
741741
742- /**
743- * Sets the observation registry to use for creating tracing Spans for operations, commands and transactions.
744- *
745- * <p> If set the environment variable {@value ENV_OTEL_ENABLED} is used to enable or disable the creation of tracing spans.
746- *
747- * <p> If set the environment variable {@value ENV_OTEL_QUERY_TEXT_MAX_LENGTH} is used to determine the maximum length
748- * of command payloads captured in tracing spans. If the environment variable is not set, the entire command payloads is
749- * captured.
750- *
751- * @param observationRegistry the observation registry
752- * @return this
753- * @since 5.7
754- */
755- @ Alpha (Reason .CLIENT )
756- public Builder observationRegistry (final ObservationRegistry observationRegistry ) {
757- this .observationRegistry = observationRegistry ;
758- return this ;
759- }
760-
761- /**
762- * Sets the observation registry to use for creating tracing Spans for operations, commands and transactions.
763- *
764- * <p> If set the environment variable {@value ENV_OTEL_ENABLED} is used to enable or disable the creation of tracing spans.
765- *
766- * <p> If set the environment variable {@value ENV_OTEL_QUERY_TEXT_MAX_LENGTH} is used to determine the maximum length
767- * of command payloads captured in tracing spans. If the environment variable is not set, the entire command payloads is
768- * captured.
769- *
770- * @param observationRegistry the observation registry
771- * @param enableCommandPayload whether command payloads should be captured in tracing spans. This may have performance
772- * implications so should be used with care.
773- * @return this
774- * @since 5.7
775- */
776- @ Alpha (Reason .CLIENT )
777- public Builder observationRegistry (final ObservationRegistry observationRegistry , final boolean enableCommandPayload ) {
778- this .observationRegistry = observationRegistry ;
779- this .enableCommandPayloadTracing = enableCommandPayload ;
780- return this ;
781- }
782-
783742 /**
784743 * Build an instance of {@code MongoClientSettings}.
785744 *
@@ -1097,25 +1056,14 @@ public ContextProvider getContextProvider() {
10971056 return contextProvider ;
10981057 }
10991058
1100- /** Get the ObservationRegistry to create tracing Spans for operations, commands and transactions .
1059+ /** Get the tracing settings .
11011060 *
1102- * @return the configured ObservationRegistry
1061+ * @return the configured TracingSettings
11031062 * @since 5.7
11041063 */
1105- @ Nullable
11061064 @ Alpha (Reason .CLIENT )
1107- public ObservationRegistry getObservationRegistry () {
1108- return observationRegistry ;
1109- }
1110-
1111- /** Returns true if command payload tracing is enabled.
1112- *
1113- * @return the enableCommandPayloadTracing value
1114- * @since 5.7
1115- */
1116- @ Alpha (Reason .CLIENT )
1117- public boolean isCommandPayloadTracingEnabled () {
1118- return enableCommandPayloadTracing ;
1065+ public TracingSettings getTracingSettings () {
1066+ return tracingSettings ;
11191067 }
11201068
11211069 @ Override
@@ -1215,6 +1163,7 @@ private MongoClientSettings(final Builder builder) {
12151163 socketSettings = builder .socketSettingsBuilder .build ();
12161164 connectionPoolSettings = builder .connectionPoolSettingsBuilder .build ();
12171165 sslSettings = builder .sslSettingsBuilder .build ();
1166+ tracingSettings = builder .tracingSettingsBuilder .build ();
12181167 compressorList = builder .compressorList ;
12191168 uuidRepresentation = builder .uuidRepresentation ;
12201169 serverApi = builder .serverApi ;
@@ -1234,8 +1183,5 @@ private MongoClientSettings(final Builder builder) {
12341183 heartbeatConnectTimeoutSetExplicitly = builder .heartbeatConnectTimeoutMS != 0 ;
12351184 contextProvider = builder .contextProvider ;
12361185 timeoutMS = builder .timeoutMS ;
1237-
1238- observationRegistry = builder .observationRegistry ;
1239- enableCommandPayloadTracing = builder .enableCommandPayloadTracing ;
12401186 }
12411187}
0 commit comments