Skip to content

Commit 33aa2ba

Browse files
authored
Add databaseName to command succeeded/failed events/logs (#1213)
JAVA-4875
1 parent c7346ba commit 33aa2ba

File tree

12 files changed

+302
-84
lines changed

12 files changed

+302
-84
lines changed

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

+35-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public abstract class CommandEvent {
3131
private final int requestId;
3232
private final ConnectionDescription connectionDescription;
3333
private final String commandName;
34+
private final String databaseName;
35+
3436
private final long operationId;
3537

3638
/**
@@ -41,30 +43,49 @@ public abstract class CommandEvent {
4143
* @param requestId the request id
4244
* @param connectionDescription the connection description
4345
* @param commandName the command name
44-
* @since 4.10
46+
* @param databaseName the database name
47+
* @since 4.11
4548
*/
4649
public CommandEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
47-
final ConnectionDescription connectionDescription, final String commandName) {
50+
final ConnectionDescription connectionDescription, final String commandName, final String databaseName) {
4851
this.requestContext = requestContext;
4952
this.requestId = requestId;
5053
this.connectionDescription = connectionDescription;
5154
this.commandName = commandName;
55+
this.databaseName = databaseName;
5256
this.operationId = operationId;
5357
}
5458

59+
/**
60+
* Construct an instance.
61+
*
62+
* @param requestContext the request context
63+
* @param operationId the operation id
64+
* @param requestId the request id
65+
* @param connectionDescription the connection description
66+
* @param commandName the command name
67+
* @since 4.10
68+
* @deprecated Prefer {@link CommandEvent#CommandEvent(RequestContext, long, int, ConnectionDescription, String, String)}
69+
*/
70+
@Deprecated
71+
public CommandEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
72+
final ConnectionDescription connectionDescription, final String commandName) {
73+
this(requestContext, -1, requestId, connectionDescription, commandName, "");
74+
}
75+
5576
/**
5677
* Construct an instance.
5778
* @param requestContext the request context
5879
* @param requestId the request id
5980
* @param connectionDescription the connection description
6081
* @param commandName the command name
6182
* @since 4.4
62-
* @deprecated Prefer {@link CommandEvent#CommandEvent(RequestContext, long, int, ConnectionDescription, String)}
83+
* @deprecated Prefer {@link CommandEvent#CommandEvent(RequestContext, long, int, ConnectionDescription, String, String)}
6384
*/
6485
@Deprecated
6586
public CommandEvent(@Nullable final RequestContext requestContext, final int requestId,
6687
final ConnectionDescription connectionDescription, final String commandName) {
67-
this(requestContext, -1, requestId, connectionDescription, commandName);
88+
this(requestContext, -1, requestId, connectionDescription, commandName, "");
6889
}
6990

7091
/**
@@ -114,6 +135,16 @@ public String getCommandName() {
114135
return commandName;
115136
}
116137

138+
/**
139+
* Gets the database on which the operation will be executed.
140+
*
141+
* @return the database name
142+
* @since 4.11
143+
*/
144+
public String getDatabaseName() {
145+
return databaseName;
146+
}
147+
117148
/**
118149
* Gets the request context associated with this event.
119150
*

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ public final class CommandFailedEvent extends CommandEvent {
3434
private final long elapsedTimeNanos;
3535
private final Throwable throwable;
3636

37+
/**
38+
* Construct an instance.
39+
*
40+
* @param requestContext the request context
41+
* @param operationId the operation id
42+
* @param requestId the request id
43+
* @param connectionDescription the connection description
44+
* @param commandName the command name
45+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
46+
* @param throwable the throwable cause of the failure
47+
* @since 4.11
48+
*/
49+
public CommandFailedEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
50+
final ConnectionDescription connectionDescription, final String commandName,
51+
final String databaseName, final long elapsedTimeNanos, final Throwable throwable) {
52+
super(requestContext, operationId, requestId, connectionDescription, commandName, databaseName);
53+
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
54+
this.elapsedTimeNanos = elapsedTimeNanos;
55+
this.throwable = throwable;
56+
}
57+
3758
/**
3859
* Construct an instance.
3960
*
@@ -45,7 +66,9 @@ public final class CommandFailedEvent extends CommandEvent {
4566
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
4667
* @param throwable the throwable cause of the failure
4768
* @since 4.10
69+
* {@link CommandFailedEvent#CommandFailedEvent(RequestContext, long, int, ConnectionDescription, String, String, long, Throwable)}
4870
*/
71+
@Deprecated
4972
public CommandFailedEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
5073
final ConnectionDescription connectionDescription, final String commandName, final long elapsedTimeNanos,
5174
final Throwable throwable) {
@@ -65,7 +88,7 @@ public CommandFailedEvent(@Nullable final RequestContext requestContext, final l
6588
* @param throwable the throwable cause of the failure
6689
* @since 4.4
6790
* @deprecated Prefer
68-
* {@link CommandFailedEvent#CommandFailedEvent(RequestContext, long, int, ConnectionDescription, String, long, Throwable)}
91+
* {@link CommandFailedEvent#CommandFailedEvent(RequestContext, long, int, ConnectionDescription, String, String, long, Throwable)}
6992
*/
7093
@Deprecated
7194
public CommandFailedEvent(@Nullable final RequestContext requestContext, final int requestId,

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* @since 3.1
2828
*/
2929
public final class CommandStartedEvent extends CommandEvent {
30-
private final String databaseName;
3130
private final BsonDocument command;
3231

3332
/**
@@ -45,8 +44,7 @@ public final class CommandStartedEvent extends CommandEvent {
4544
public CommandStartedEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
4645
final ConnectionDescription connectionDescription, final String databaseName, final String commandName,
4746
final BsonDocument command) {
48-
super(requestContext, operationId, requestId, connectionDescription, commandName);
49-
this.databaseName = databaseName;
47+
super(requestContext, operationId, requestId, connectionDescription, commandName, databaseName);
5048
this.command = command;
5149
}
5250

@@ -87,15 +85,6 @@ public CommandStartedEvent(final int requestId, final ConnectionDescription conn
8785
this(null, requestId, connectionDescription, databaseName, commandName, command);
8886
}
8987

90-
/**
91-
* Gets the database on which the operation will be executed.
92-
*
93-
* @return the database name
94-
*/
95-
public String getDatabaseName() {
96-
return databaseName;
97-
}
98-
9988
/**
10089
* Gets the command document. The document is only usable within the method that delivered the event. If it's needed for longer, it
10190
* must be cloned via {@link Object#clone()}.

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ public final class CommandSucceededEvent extends CommandEvent {
3434
private final BsonDocument response;
3535
private final long elapsedTimeNanos;
3636

37+
/**
38+
* Construct an instance.
39+
*
40+
* @param requestContext the request context
41+
* @param operationId the operation id
42+
* @param requestId the request id
43+
* @param connectionDescription the connection description
44+
* @param commandName the command name
45+
* @param databaseName the database name
46+
* @param response the command response
47+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
48+
* @since 4.11
49+
*/
50+
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
51+
final ConnectionDescription connectionDescription, final String commandName,
52+
final String databaseName, final BsonDocument response, final long elapsedTimeNanos) {
53+
super(requestContext, operationId, requestId, connectionDescription, commandName, databaseName);
54+
this.response = response;
55+
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
56+
this.elapsedTimeNanos = elapsedTimeNanos;
57+
}
58+
3759
/**
3860
* Construct an instance.
3961
*
@@ -45,7 +67,10 @@ public final class CommandSucceededEvent extends CommandEvent {
4567
* @param response the command response
4668
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
4769
* @since 4.10
70+
* @deprecated Prefer
71+
* {@link CommandSucceededEvent#CommandSucceededEvent(RequestContext, long, int, ConnectionDescription, String, String, BsonDocument, long)}
4872
*/
73+
@Deprecated
4974
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
5075
final ConnectionDescription connectionDescription, final String commandName, final BsonDocument response,
5176
final long elapsedTimeNanos) {
@@ -65,7 +90,7 @@ public CommandSucceededEvent(@Nullable final RequestContext requestContext, fina
6590
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
6691
* @since 4.4
6792
* @deprecated Prefer
68-
* {@link CommandSucceededEvent#CommandSucceededEvent(RequestContext, long, int, ConnectionDescription, String, BsonDocument, long)}
93+
* {@link CommandSucceededEvent#CommandSucceededEvent(RequestContext, long, int, ConnectionDescription, String, String, BsonDocument, long)}
6994
*/
7095
@Deprecated
7196
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final int requestId,

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class LoggingCommandEventSender implements CommandEventSender {
9797
@Override
9898
public void sendStartedEvent() {
9999
if (loggingRequired()) {
100-
String messagePrefix = "Command \"{}\" started on database {}";
100+
String messagePrefix = "Command \"{}\" started on database \"{}\"";
101101
String command = redactionRequired ? "{}" : getTruncatedJsonCommand(commandDocument);
102102

103103
logEventMessage(messagePrefix, "Command started", null, entries -> {
@@ -131,19 +131,20 @@ public void sendFailedEvent(final Throwable t) {
131131
long elapsedTimeNanos = System.nanoTime() - startTimeNanos;
132132

133133
if (loggingRequired()) {
134-
String messagePrefix = "Command \"{}\" failed in {} ms";
134+
String messagePrefix = "Command \"{}\" failed on database \"{}\" in {} ms";
135135

136136
logEventMessage(messagePrefix, "Command failed", commandEventException,
137137
entries -> {
138138
entries.add(new Entry(COMMAND_NAME, commandName));
139+
entries.add(new Entry(DATABASE_NAME, message.getNamespace().getDatabaseName()));
139140
entries.add(new Entry(DURATION_MS, elapsedTimeNanos / NANOS_PER_MILLI));
140141
},
141142
entries -> entries.add(new Entry(COMMAND_CONTENT, null)));
142143
}
143144

144145
if (eventRequired()) {
145-
sendCommandFailedEvent(message, commandName, description, elapsedTimeNanos, commandEventException, commandListener,
146-
requestContext, operationContext);
146+
sendCommandFailedEvent(message, message.getNamespace().getDatabaseName(), commandName, description, elapsedTimeNanos,
147+
commandEventException, commandListener, requestContext, operationContext);
147148
}
148149
}
149150

@@ -161,7 +162,7 @@ private void sendSucceededEvent(final BsonDocument reply) {
161162
long elapsedTimeNanos = System.nanoTime() - startTimeNanos;
162163

163164
if (loggingRequired()) {
164-
String format = "Command \"{}\" succeeded in {} ms using a connection with driver-generated ID {}"
165+
String format = "Command \"{}\" succeeded on database \"{}\" in {} ms using a connection with driver-generated ID {}"
165166
+ "[ and server-generated ID {}] to {}:{}[ with service ID {}]. The request ID is {}"
166167
+ " and the operation ID is {}. Command reply: {}";
167168

@@ -171,14 +172,15 @@ private void sendSucceededEvent(final BsonDocument reply) {
171172
logEventMessage("Command succeeded", null,
172173
entries -> {
173174
entries.add(new Entry(COMMAND_NAME, commandName));
175+
entries.add(new Entry(DATABASE_NAME, message.getNamespace().getDatabaseName()));
174176
entries.add(new Entry(DURATION_MS, elapsedTimeNanos / NANOS_PER_MILLI));
175177
},
176178
entries -> entries.add(new Entry(REPLY, replyString)), format);
177179
}
178180

179181
if (eventRequired()) {
180182
BsonDocument responseDocumentForEvent = redactionRequired ? new BsonDocument() : reply;
181-
sendCommandSucceededEvent(message, commandName, responseDocumentForEvent, description,
183+
sendCommandSucceededEvent(message, message.getNamespace().getDatabaseName(), commandName, responseDocumentForEvent, description,
182184
elapsedTimeNanos, commandListener, requestContext, operationContext);
183185
}
184186
}

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -289,28 +289,30 @@ static void sendCommandStartedEvent(final RequestMessage message, final String d
289289
}
290290
}
291291

292-
static void sendCommandSucceededEvent(final RequestMessage message, final String commandName, final BsonDocument response,
293-
final ConnectionDescription connectionDescription, final long elapsedTimeNanos,
292+
static void sendCommandSucceededEvent(final RequestMessage message, final String databaseName, final String commandName,
293+
final BsonDocument response, final ConnectionDescription connectionDescription, final long elapsedTimeNanos,
294294
final CommandListener commandListener, final RequestContext requestContext, final OperationContext operationContext) {
295295
notNull("requestContext", requestContext);
296296
try {
297297
commandListener.commandSucceeded(new CommandSucceededEvent(getRequestContextForEvent(requestContext),
298-
operationContext.getId(), message.getId(), connectionDescription, commandName, response, elapsedTimeNanos));
298+
operationContext.getId(), message.getId(), connectionDescription, commandName, databaseName, response,
299+
elapsedTimeNanos));
299300
} catch (Exception e) {
300301
if (PROTOCOL_EVENT_LOGGER.isWarnEnabled()) {
301302
PROTOCOL_EVENT_LOGGER.warn(format("Exception thrown raising command succeeded event to listener %s", commandListener), e);
302303
}
303304
}
304305
}
305306

306-
static void sendCommandFailedEvent(final RequestMessage message, final String commandName,
307+
static void sendCommandFailedEvent(final RequestMessage message, final String databaseName, final String commandName,
307308
final ConnectionDescription connectionDescription, final long elapsedTimeNanos,
308309
final Throwable throwable, final CommandListener commandListener, final RequestContext requestContext,
309310
final OperationContext operationContext) {
310311
notNull("requestContext", requestContext);
311312
try {
312313
commandListener.commandFailed(new CommandFailedEvent(getRequestContextForEvent(requestContext),
313-
operationContext.getId(), message.getId(), connectionDescription, commandName, elapsedTimeNanos, throwable));
314+
operationContext.getId(), message.getId(), connectionDescription, commandName, databaseName, elapsedTimeNanos,
315+
throwable));
314316
} catch (Exception e) {
315317
if (PROTOCOL_EVENT_LOGGER.isWarnEnabled()) {
316318
PROTOCOL_EVENT_LOGGER.warn(format("Exception thrown raising command failed event to listener %s", commandListener), e);

driver-core/src/test/functional/com/mongodb/internal/connection/TestCommandListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ else if (!observeSensitiveCommands) {
250250
lock.lock();
251251
try {
252252
events.add(new CommandSucceededEvent(event.getRequestContext(), event.getOperationId(), event.getRequestId(),
253-
event.getConnectionDescription(), event.getCommandName(),
253+
event.getConnectionDescription(), event.getCommandName(), event.getDatabaseName(),
254254
event.getResponse() == null ? null : event.getResponse().clone(),
255255
event.getElapsedTime(TimeUnit.NANOSECONDS)));
256256
commandCompletedCondition.signal();

0 commit comments

Comments
 (0)