Skip to content

Commit 7bdd1d7

Browse files
authored
Add operationId to command events (#1101)
JAVA-4907
1 parent db1fdbc commit 7bdd1d7

File tree

15 files changed

+193
-156
lines changed

15 files changed

+193
-156
lines changed

driver-core/src/main/com/mongodb/event/CommandEvent.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ public abstract class CommandEvent {
3131
private final int requestId;
3232
private final ConnectionDescription connectionDescription;
3333
private final String commandName;
34+
private final long operationId;
35+
36+
/**
37+
* Construct an instance.
38+
*
39+
* @param requestContext the request context
40+
* @param operationId the operation id
41+
* @param requestId the request id
42+
* @param connectionDescription the connection description
43+
* @param commandName the command name
44+
* @since 4.10
45+
*/
46+
public CommandEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
47+
final ConnectionDescription connectionDescription, final String commandName) {
48+
this.requestContext = requestContext;
49+
this.requestId = requestId;
50+
this.connectionDescription = connectionDescription;
51+
this.commandName = commandName;
52+
this.operationId = operationId;
53+
}
3454

3555
/**
3656
* Construct an instance.
@@ -39,13 +59,12 @@ public abstract class CommandEvent {
3959
* @param connectionDescription the connection description
4060
* @param commandName the command name
4161
* @since 4.4
62+
* @deprecated Prefer {@link CommandEvent#CommandEvent(RequestContext, long, int, ConnectionDescription, String)}
4263
*/
64+
@Deprecated
4365
public CommandEvent(@Nullable final RequestContext requestContext, final int requestId,
4466
final ConnectionDescription connectionDescription, final String commandName) {
45-
this.requestContext = requestContext;
46-
this.requestId = requestId;
47-
this.connectionDescription = connectionDescription;
48-
this.commandName = commandName;
67+
this(requestContext, -1, requestId, connectionDescription, commandName);
4968
}
5069

5170
/**
@@ -58,6 +77,16 @@ public CommandEvent(final int requestId, final ConnectionDescription connectionD
5877
this(null, requestId, connectionDescription, commandName);
5978
}
6079

80+
/**
81+
* Gets the operation identifier
82+
*
83+
* @return the operation identifier
84+
* @since 4.10
85+
*/
86+
public long getOperationId() {
87+
return operationId;
88+
}
89+
6190
/**
6291
* Gets the request identifier
6392
*

driver-core/src/main/com/mongodb/event/CommandFailedEvent.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,54 @@ public final class CommandFailedEvent extends CommandEvent {
3636

3737
/**
3838
* Construct an instance.
39+
*
3940
* @param requestContext the request context
40-
* @param requestId the requestId
41+
* @param operationId the operation id
42+
* @param requestId the request id
4143
* @param connectionDescription the connection description
4244
* @param commandName the command name
4345
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
4446
* @param throwable the throwable cause of the failure
45-
* @since 4.4
47+
* @since 4.10
4648
*/
47-
public CommandFailedEvent(@Nullable final RequestContext requestContext, final int requestId,
49+
public CommandFailedEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
4850
final ConnectionDescription connectionDescription, final String commandName, final long elapsedTimeNanos,
4951
final Throwable throwable) {
50-
super(requestContext, requestId, connectionDescription, commandName);
52+
super(requestContext, operationId, requestId, connectionDescription, commandName);
5153
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
5254
this.elapsedTimeNanos = elapsedTimeNanos;
5355
this.throwable = throwable;
5456
}
5557

58+
/**
59+
* Construct an instance.
60+
* @param requestContext the request context
61+
* @param requestId the requestId
62+
* @param connectionDescription the connection description
63+
* @param commandName the command name
64+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
65+
* @param throwable the throwable cause of the failure
66+
* @since 4.4
67+
* @deprecated Prefer
68+
* {@link CommandFailedEvent#CommandFailedEvent(RequestContext, long, int, ConnectionDescription, String, long, Throwable)}
69+
*/
70+
@Deprecated
71+
public CommandFailedEvent(@Nullable final RequestContext requestContext, final int requestId,
72+
final ConnectionDescription connectionDescription, final String commandName, final long elapsedTimeNanos,
73+
final Throwable throwable) {
74+
this(requestContext, -1, requestId, connectionDescription, commandName, elapsedTimeNanos, throwable);
75+
}
76+
5677
/**
5778
* Construct an instance.
5879
* @param requestId the requestId
5980
* @param connectionDescription the connection description
6081
* @param commandName the command name
6182
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
6283
* @param throwable the throwable cause of the failure
84+
* {@link CommandFailedEvent#CommandFailedEvent(RequestContext, long, int, ConnectionDescription, String, long, Throwable)}
6385
*/
86+
@Deprecated
6487
public CommandFailedEvent(final int requestId, final ConnectionDescription connectionDescription,
6588
final String commandName, final long elapsedTimeNanos, final Throwable throwable) {
6689
this(null, requestId, connectionDescription, commandName, elapsedTimeNanos, throwable);

driver-core/src/main/com/mongodb/event/CommandStartedEvent.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ public final class CommandStartedEvent extends CommandEvent {
3030
private final String databaseName;
3131
private final BsonDocument command;
3232

33+
/**
34+
* Construct an instance.
35+
*
36+
* @param requestContext the request context
37+
* @param operationId the operation id
38+
* @param requestId the request id
39+
* @param connectionDescription the connection description
40+
* @param databaseName the database name
41+
* @param commandName the command name
42+
* @param command the command as a BSON document
43+
* @since 4.10
44+
*/
45+
public CommandStartedEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
46+
final ConnectionDescription connectionDescription, final String databaseName, final String commandName,
47+
final BsonDocument command) {
48+
super(requestContext, operationId, requestId, connectionDescription, commandName);
49+
this.databaseName = databaseName;
50+
this.command = command;
51+
}
52+
3353
/**
3454
* Construct an instance.
3555
*
@@ -40,13 +60,14 @@ public final class CommandStartedEvent extends CommandEvent {
4060
* @param commandName the command name
4161
* @param command the command as a BSON document
4262
* @since 4.4
63+
* @deprecated Prefer {@link
64+
* CommandStartedEvent#CommandStartedEvent(RequestContext, long, int, ConnectionDescription, String, String, BsonDocument)}
4365
*/
66+
@Deprecated
4467
public CommandStartedEvent(@Nullable final RequestContext requestContext, final int requestId,
4568
final ConnectionDescription connectionDescription, final String databaseName, final String commandName,
4669
final BsonDocument command) {
47-
super(requestContext, requestId, connectionDescription, commandName);
48-
this.command = command;
49-
this.databaseName = databaseName;
70+
this(requestContext, -1, requestId, connectionDescription, databaseName, commandName, command);
5071
}
5172

5273
/**
@@ -57,7 +78,10 @@ public CommandStartedEvent(@Nullable final RequestContext requestContext, final
5778
* @param databaseName the database name
5879
* @param commandName the command name
5980
* @param command the command as a BSON document
81+
* @deprecated Prefer {@link
82+
* CommandStartedEvent#CommandStartedEvent(RequestContext, long, int, ConnectionDescription, String, String, BsonDocument)}
6083
*/
84+
@Deprecated
6185
public CommandStartedEvent(final int requestId, final ConnectionDescription connectionDescription,
6286
final String databaseName, final String commandName, final BsonDocument command) {
6387
this(null, requestId, connectionDescription, databaseName, commandName, command);

driver-core/src/main/com/mongodb/event/CommandSucceededEvent.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,54 @@ public final class CommandSucceededEvent extends CommandEvent {
3636

3737
/**
3838
* Construct an instance.
39+
*
3940
* @param requestContext the request context
41+
* @param operationId the operation id
4042
* @param requestId the request id
4143
* @param connectionDescription the connection description
4244
* @param commandName the command name
4345
* @param response the command response
4446
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
45-
* @since 4.4
47+
* @since 4.10
4648
*/
47-
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final int requestId,
49+
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
4850
final ConnectionDescription connectionDescription, final String commandName, final BsonDocument response,
4951
final long elapsedTimeNanos) {
50-
super(requestContext, requestId, connectionDescription, commandName);
52+
super(requestContext, operationId, requestId, connectionDescription, commandName);
5153
this.response = response;
5254
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
5355
this.elapsedTimeNanos = elapsedTimeNanos;
5456
}
5557

58+
/**
59+
* Construct an instance.
60+
* @param requestContext the request context
61+
* @param requestId the request id
62+
* @param connectionDescription the connection description
63+
* @param commandName the command name
64+
* @param response the command response
65+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
66+
* @since 4.4
67+
* @deprecated Prefer
68+
* {@link CommandSucceededEvent#CommandSucceededEvent(RequestContext, long, int, ConnectionDescription, String, BsonDocument, long)}
69+
*/
70+
@Deprecated
71+
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final int requestId,
72+
final ConnectionDescription connectionDescription, final String commandName, final BsonDocument response,
73+
final long elapsedTimeNanos) {
74+
this(requestContext, -1, requestId, connectionDescription, commandName, response, elapsedTimeNanos);
75+
}
76+
5677
/**
5778
* Construct an instance.
5879
* @param requestId the request id
5980
* @param connectionDescription the connection description
6081
* @param commandName the command name
6182
* @param response the command response
6283
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
84+
* {@link CommandSucceededEvent#CommandSucceededEvent(RequestContext, long, int, ConnectionDescription, String, BsonDocument, long)}
6385
*/
86+
@Deprecated
6487
public CommandSucceededEvent(final int requestId, final ConnectionDescription connectionDescription, final String commandName,
6588
final BsonDocument response, final long elapsedTimeNanos) {
6689
this(null, requestId, connectionDescription, commandName, response, elapsedTimeNanos);

driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public void sendStartedEvent() {
101101
BsonDocument commandDocumentForEvent = redactionRequired
102102
? new BsonDocument() : commandDocument;
103103

104-
sendCommandStartedEvent(message, message.getNamespace().getDatabaseName(),
105-
commandName, commandDocumentForEvent, description, assertNotNull(commandListener), requestContext);
104+
sendCommandStartedEvent(message, message.getNamespace().getDatabaseName(), commandName, commandDocumentForEvent, description,
105+
assertNotNull(commandListener), requestContext, operationContext);
106106
}
107107
// the buffer underlying the command document may be released after the started event, so set to null to ensure it's not used
108108
// when sending the failed or succeeded event
@@ -134,7 +134,7 @@ public void sendFailedEvent(final Throwable t) {
134134

135135
if (eventRequired()) {
136136
sendCommandFailedEvent(message, commandName, description, elapsedTimeNanos, commandEventException, commandListener,
137-
requestContext);
137+
requestContext, operationContext);
138138
}
139139
}
140140

@@ -171,7 +171,7 @@ private void sendSucceededEvent(final BsonDocument reply) {
171171
if (eventRequired()) {
172172
BsonDocument responseDocumentForEvent = redactionRequired ? new BsonDocument() : reply;
173173
sendCommandSucceededEvent(message, commandName, responseDocumentForEvent, description,
174-
elapsedTimeNanos, commandListener, requestContext);
174+
elapsedTimeNanos, commandListener, requestContext, operationContext);
175175
}
176176
}
177177

@@ -208,8 +208,9 @@ private void appendCommonLogFragment(final List<Entry> entries, final StringBuil
208208
entries.add(new Entry("serviceId", descriptionServiceId));
209209
}
210210

211-
builder.append(". The request ID is %s.");
211+
builder.append(". The request ID is %s and the operation ID is %s.");
212212
entries.add(new Entry("requestId", message.getId()));
213+
entries.add(new Entry("operationId", operationContext.getId()));
213214
}
214215

215216
private String getTruncatedJsonCommand(final BsonDocument commandDocument) {

driver-core/src/main/com/mongodb/internal/connection/ProtocolHelper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ private static boolean isNodeIsRecoveringError(final int errorCode, final String
277277

278278
static void sendCommandStartedEvent(final RequestMessage message, final String databaseName, final String commandName,
279279
final BsonDocument command, final ConnectionDescription connectionDescription,
280-
final CommandListener commandListener, final RequestContext requestContext) {
280+
final CommandListener commandListener, final RequestContext requestContext, final OperationContext operationContext) {
281281
notNull("requestContext", requestContext);
282282
try {
283-
commandListener.commandStarted(new CommandStartedEvent(getRequestContextForEvent(requestContext), message.getId(),
283+
commandListener.commandStarted(new CommandStartedEvent(getRequestContextForEvent(requestContext), operationContext.getId(), message.getId(),
284284
connectionDescription, databaseName, commandName, command));
285285
} catch (Exception e) {
286286
if (PROTOCOL_EVENT_LOGGER.isWarnEnabled()) {
@@ -291,11 +291,11 @@ static void sendCommandStartedEvent(final RequestMessage message, final String d
291291

292292
static void sendCommandSucceededEvent(final RequestMessage message, final String commandName, final BsonDocument response,
293293
final ConnectionDescription connectionDescription, final long elapsedTimeNanos,
294-
final CommandListener commandListener, final RequestContext requestContext) {
294+
final CommandListener commandListener, final RequestContext requestContext, final OperationContext operationContext) {
295295
notNull("requestContext", requestContext);
296296
try {
297-
commandListener.commandSucceeded(new CommandSucceededEvent(getRequestContextForEvent(requestContext), message.getId(),
298-
connectionDescription, commandName, response, elapsedTimeNanos));
297+
commandListener.commandSucceeded(new CommandSucceededEvent(getRequestContextForEvent(requestContext),
298+
operationContext.getId(), message.getId(), connectionDescription, commandName, response, elapsedTimeNanos));
299299
} catch (Exception e) {
300300
if (PROTOCOL_EVENT_LOGGER.isWarnEnabled()) {
301301
PROTOCOL_EVENT_LOGGER.warn(format("Exception thrown raising command succeeded event to listener %s", commandListener), e);
@@ -305,11 +305,12 @@ static void sendCommandSucceededEvent(final RequestMessage message, final String
305305

306306
static void sendCommandFailedEvent(final RequestMessage message, final String commandName,
307307
final ConnectionDescription connectionDescription, final long elapsedTimeNanos,
308-
final Throwable throwable, final CommandListener commandListener, final RequestContext requestContext) {
308+
final Throwable throwable, final CommandListener commandListener, final RequestContext requestContext,
309+
final OperationContext operationContext) {
309310
notNull("requestContext", requestContext);
310311
try {
311-
commandListener.commandFailed(new CommandFailedEvent(getRequestContextForEvent(requestContext), message.getId(),
312-
connectionDescription, commandName, elapsedTimeNanos, throwable));
312+
commandListener.commandFailed(new CommandFailedEvent(getRequestContextForEvent(requestContext),
313+
operationContext.getId(), message.getId(), connectionDescription, commandName, elapsedTimeNanos, throwable));
313314
} catch (Exception e) {
314315
if (PROTOCOL_EVENT_LOGGER.isWarnEnabled()) {
315316
PROTOCOL_EVENT_LOGGER.warn(format("Exception thrown raising command failed event to listener %s", commandListener), e);

driver-core/src/main/com/mongodb/internal/connection/SendMessageCallback.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)