|
23 | 23 | import com.mongodb.MongoException; |
24 | 24 | import com.mongodb.MongoInternalException; |
25 | 25 | import com.mongodb.MongoInterruptedException; |
26 | | -import com.mongodb.MongoNamespace; |
27 | 26 | import com.mongodb.MongoOperationTimeoutException; |
28 | 27 | import com.mongodb.MongoSocketClosedException; |
29 | 28 | import com.mongodb.MongoSocketReadException; |
30 | 29 | import com.mongodb.MongoSocketReadTimeoutException; |
31 | 30 | import com.mongodb.MongoSocketWriteException; |
32 | 31 | import com.mongodb.MongoSocketWriteTimeoutException; |
33 | 32 | import com.mongodb.ServerAddress; |
34 | | -import com.mongodb.UnixServerAddress; |
35 | 33 | import com.mongodb.annotations.NotThreadSafe; |
36 | 34 | import com.mongodb.connection.AsyncCompletionHandler; |
37 | 35 | import com.mongodb.connection.ClusterConnectionMode; |
|
54 | 52 | import com.mongodb.internal.session.SessionContext; |
55 | 53 | import com.mongodb.internal.time.Timeout; |
56 | 54 | import com.mongodb.internal.tracing.Span; |
57 | | -import com.mongodb.internal.tracing.TracingManager; |
58 | 55 | import com.mongodb.lang.Nullable; |
59 | | -import io.micrometer.common.KeyValues; |
60 | 56 | import org.bson.BsonBinaryReader; |
61 | 57 | import org.bson.BsonDocument; |
62 | 58 | import org.bson.ByteBuf; |
|
100 | 96 | import static com.mongodb.internal.logging.LogMessage.Level.DEBUG; |
101 | 97 | import static com.mongodb.internal.thread.InterruptionUtil.translateInterruptedException; |
102 | 98 | import static com.mongodb.internal.tracing.MongodbObservation.HighCardinalityKeyNames.QUERY_TEXT; |
103 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.CLIENT_CONNECTION_ID; |
104 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.COLLECTION; |
105 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.COMMAND_NAME; |
106 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.CURSOR_ID; |
107 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.NAMESPACE; |
108 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.NETWORK_TRANSPORT; |
109 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.QUERY_SUMMARY; |
110 | 99 | import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.RESPONSE_STATUS_CODE; |
111 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.SERVER_ADDRESS; |
112 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.SERVER_CONNECTION_ID; |
113 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.SERVER_PORT; |
114 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.SERVER_TYPE; |
115 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.SESSION_ID; |
116 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.SYSTEM; |
117 | | -import static com.mongodb.internal.tracing.MongodbObservation.LowCardinalityKeyNames.TRANSACTION_NUMBER; |
118 | 100 | import static java.util.Arrays.asList; |
119 | 101 |
|
120 | 102 | /** |
@@ -456,7 +438,16 @@ private <T> T sendAndReceiveInternal(final CommandMessage message, final Decoder |
456 | 438 | Span tracingSpan; |
457 | 439 | try (ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(this)) { |
458 | 440 | message.encode(bsonOutput, operationContext); |
459 | | - tracingSpan = createTracingSpan(message, operationContext, bsonOutput); |
| 441 | + tracingSpan = operationContext |
| 442 | + .getTracingManager() |
| 443 | + .createTracingSpan(message, |
| 444 | + operationContext, |
| 445 | + () -> message.getCommandDocument(bsonOutput), |
| 446 | + cmdName -> SECURITY_SENSITIVE_COMMANDS.contains(cmdName) |
| 447 | + || SECURITY_SENSITIVE_HELLO_COMMANDS.contains(cmdName), |
| 448 | + () -> getDescription().getServerAddress(), |
| 449 | + () -> getDescription().getConnectionId() |
| 450 | + ); |
460 | 451 |
|
461 | 452 | boolean isLoggingCommandNeeded = isLoggingCommandNeeded(); |
462 | 453 | boolean isTracingCommandPayloadNeeded = tracingSpan != null && operationContext.getTracingManager().isCommandPayloadEnabled(); |
@@ -1031,103 +1022,4 @@ private ClusterId getClusterId() { |
1031 | 1022 | return description.getConnectionId().getServerId().getClusterId(); |
1032 | 1023 | } |
1033 | 1024 |
|
1034 | | - /** |
1035 | | - * Creates a tracing span for the given command message. |
1036 | | - * <p> |
1037 | | - * The span is only created if tracing is enabled and the command is not security-sensitive. |
1038 | | - * It attaches various tags to the span, such as database system, namespace, query summary, opcode, |
1039 | | - * server address, port, server type, client and server connection IDs, and, if applicable, |
1040 | | - * transaction number and session ID. For cursor fetching commands, the parent context is retrieved using the cursor ID. |
1041 | | - * If command payload tracing is enabled, the command document is also attached as a tag. |
1042 | | - * |
1043 | | - * @param message the command message to trace |
1044 | | - * @param operationContext the operation context containing tracing and session information |
1045 | | - * @param bsonOutput the BSON output used to serialize the command |
1046 | | - * @return the created {@link Span}, or {@code null} if tracing is not enabled or the command is security-sensitive |
1047 | | - */ |
1048 | | - @Nullable |
1049 | | - private Span createTracingSpan(final CommandMessage message, final OperationContext operationContext, final ByteBufferBsonOutput bsonOutput) { |
1050 | | - |
1051 | | - TracingManager tracingManager = operationContext.getTracingManager(); |
1052 | | - BsonDocument command = message.getCommandDocument(bsonOutput); |
1053 | | - |
1054 | | - String commandName = command.getFirstKey(); |
1055 | | - |
1056 | | - if (!tracingManager.isEnabled() |
1057 | | - || SECURITY_SENSITIVE_COMMANDS.contains(commandName) |
1058 | | - || SECURITY_SENSITIVE_HELLO_COMMANDS.contains(commandName)) { |
1059 | | - return null; |
1060 | | - } |
1061 | | - |
1062 | | - Span operationSpan = operationContext.getTracingSpan(); |
1063 | | - Span span = tracingManager |
1064 | | - .addSpan(commandName, operationSpan != null ? operationSpan.context() : null); |
1065 | | - |
1066 | | - if (command.containsKey("getMore")) { |
1067 | | - long cursorId = command.getInt64("getMore").longValue(); |
1068 | | - span.tagLowCardinality(CURSOR_ID.withValue(String.valueOf(cursorId))); |
1069 | | - if (operationSpan != null) { |
1070 | | - operationSpan.tagLowCardinality(CURSOR_ID.withValue(String.valueOf(cursorId))); |
1071 | | - } |
1072 | | - } |
1073 | | - |
1074 | | - tagNamespace(span, operationSpan, message, commandName); |
1075 | | - tagServerAndConnectionInfo(span, message); |
1076 | | - tagSessionAndTransactionInfo(span, operationContext); |
1077 | | - |
1078 | | - return span; |
1079 | | - } |
1080 | | - |
1081 | | - private void tagNamespace(final Span span, @Nullable final Span parentSpan, final CommandMessage message, final String commandName) { |
1082 | | - String namespace; |
1083 | | - String collection = ""; |
1084 | | - if (parentSpan != null) { |
1085 | | - MongoNamespace parentNamespace = parentSpan.getNamespace(); |
1086 | | - if (parentNamespace != null) { |
1087 | | - namespace = parentNamespace.getDatabaseName(); |
1088 | | - collection = |
1089 | | - MongoNamespace.COMMAND_COLLECTION_NAME.equalsIgnoreCase(parentNamespace.getCollectionName()) ? "" |
1090 | | - : parentNamespace.getCollectionName(); |
1091 | | - } else { |
1092 | | - namespace = message.getDatabase(); |
1093 | | - } |
1094 | | - } else { |
1095 | | - namespace = message.getDatabase(); |
1096 | | - } |
1097 | | - String summary = commandName + " " + namespace + (collection.isEmpty() ? "" : "." + collection); |
1098 | | - |
1099 | | - KeyValues keyValues = KeyValues.of( |
1100 | | - SYSTEM.withValue("mongodb"), |
1101 | | - NAMESPACE.withValue(namespace), |
1102 | | - QUERY_SUMMARY.withValue(summary), |
1103 | | - COMMAND_NAME.withValue(commandName)); |
1104 | | - |
1105 | | - if (!collection.isEmpty()) { |
1106 | | - keyValues = keyValues.and(COLLECTION.withValue(collection)); |
1107 | | - } |
1108 | | - span.tagLowCardinality(keyValues); |
1109 | | - } |
1110 | | - |
1111 | | - private void tagServerAndConnectionInfo(final Span span, final CommandMessage message) { |
1112 | | - span.tagLowCardinality(KeyValues.of( |
1113 | | - SERVER_ADDRESS.withValue(serverId.getAddress().getHost()), |
1114 | | - SERVER_PORT.withValue(String.valueOf(serverId.getAddress().getPort())), |
1115 | | - SERVER_TYPE.withValue(message.getSettings().getServerType().name()), |
1116 | | - CLIENT_CONNECTION_ID.withValue(String.valueOf(this.description.getConnectionId().getLocalValue())), |
1117 | | - SERVER_CONNECTION_ID.withValue(String.valueOf(this.description.getConnectionId().getServerValue())), |
1118 | | - NETWORK_TRANSPORT.withValue(getServerAddress() instanceof UnixServerAddress ? "unix" : "tcp") |
1119 | | - )); |
1120 | | - } |
1121 | | - |
1122 | | - private void tagSessionAndTransactionInfo(final Span span, final OperationContext operationContext) { |
1123 | | - SessionContext sessionContext = operationContext.getSessionContext(); |
1124 | | - if (sessionContext.hasSession() && !sessionContext.isImplicitSession()) { |
1125 | | - span.tagLowCardinality(KeyValues.of( |
1126 | | - TRANSACTION_NUMBER.withValue(String.valueOf(sessionContext.getTransactionNumber())), |
1127 | | - SESSION_ID.withValue(String.valueOf(sessionContext.getSessionId() |
1128 | | - .get(sessionContext.getSessionId().getFirstKey()) |
1129 | | - .asBinary().asUuid())) |
1130 | | - )); |
1131 | | - } |
1132 | | - } |
1133 | 1025 | } |
0 commit comments