Skip to content

Add operationId to command events #1101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions driver-core/src/main/com/mongodb/event/CommandEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ public abstract class CommandEvent {
private final int requestId;
private final ConnectionDescription connectionDescription;
private final String commandName;
private final long operationId;

/**
* Construct an instance.
*
* @param requestContext the request context
* @param operationId the operation id
* @param requestId the request id
* @param connectionDescription the connection description
* @param commandName the command name
* @since 4.10
*/
public CommandEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
final ConnectionDescription connectionDescription, final String commandName) {
this.requestContext = requestContext;
this.requestId = requestId;
this.connectionDescription = connectionDescription;
this.commandName = commandName;
this.operationId = operationId;
}

/**
* Construct an instance.
Expand All @@ -39,13 +59,12 @@ public abstract class CommandEvent {
* @param connectionDescription the connection description
* @param commandName the command name
* @since 4.4
* @deprecated Prefer {@link CommandEvent#CommandEvent(RequestContext, long, int, ConnectionDescription, String)}
*/
@Deprecated
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of annoying that we have to preserve BC on constructors like this but it's what we do.

public CommandEvent(@Nullable final RequestContext requestContext, final int requestId,
final ConnectionDescription connectionDescription, final String commandName) {
this.requestContext = requestContext;
this.requestId = requestId;
this.connectionDescription = connectionDescription;
this.commandName = commandName;
this(requestContext, -1, requestId, connectionDescription, commandName);
}

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

/**
* Gets the operation identifier
*
* @return the operation identifier
* @since 4.10
*/
public long getOperationId() {
return operationId;
}

/**
* Gets the request identifier
*
Expand Down
31 changes: 27 additions & 4 deletions driver-core/src/main/com/mongodb/event/CommandFailedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,54 @@ public final class CommandFailedEvent extends CommandEvent {

/**
* Construct an instance.
*
* @param requestContext the request context
* @param requestId the requestId
* @param operationId the operation id
* @param requestId the request id
* @param connectionDescription the connection description
* @param commandName the command name
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
* @param throwable the throwable cause of the failure
* @since 4.4
* @since 4.10
*/
public CommandFailedEvent(@Nullable final RequestContext requestContext, final int requestId,
public CommandFailedEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
final ConnectionDescription connectionDescription, final String commandName, final long elapsedTimeNanos,
final Throwable throwable) {
super(requestContext, requestId, connectionDescription, commandName);
super(requestContext, operationId, requestId, connectionDescription, commandName);
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
this.elapsedTimeNanos = elapsedTimeNanos;
this.throwable = throwable;
}

/**
* Construct an instance.
* @param requestContext the request context
* @param requestId the requestId
* @param connectionDescription the connection description
* @param commandName the command name
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
* @param throwable the throwable cause of the failure
* @since 4.4
* @deprecated Prefer
* {@link CommandFailedEvent#CommandFailedEvent(RequestContext, long, int, ConnectionDescription, String, long, Throwable)}
*/
@Deprecated
public CommandFailedEvent(@Nullable final RequestContext requestContext, final int requestId,
final ConnectionDescription connectionDescription, final String commandName, final long elapsedTimeNanos,
final Throwable throwable) {
this(requestContext, -1, requestId, connectionDescription, commandName, elapsedTimeNanos, throwable);
}

/**
* Construct an instance.
* @param requestId the requestId
* @param connectionDescription the connection description
* @param commandName the command name
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
* @param throwable the throwable cause of the failure
* {@link CommandFailedEvent#CommandFailedEvent(RequestContext, long, int, ConnectionDescription, String, long, Throwable)}
*/
@Deprecated
public CommandFailedEvent(final int requestId, final ConnectionDescription connectionDescription,
final String commandName, final long elapsedTimeNanos, final Throwable throwable) {
this(null, requestId, connectionDescription, commandName, elapsedTimeNanos, throwable);
Expand Down
30 changes: 27 additions & 3 deletions driver-core/src/main/com/mongodb/event/CommandStartedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ public final class CommandStartedEvent extends CommandEvent {
private final String databaseName;
private final BsonDocument command;

/**
* Construct an instance.
*
* @param requestContext the request context
* @param operationId the operation id
* @param requestId the request id
* @param connectionDescription the connection description
* @param databaseName the database name
* @param commandName the command name
* @param command the command as a BSON document
* @since 4.10
*/
public CommandStartedEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
final ConnectionDescription connectionDescription, final String databaseName, final String commandName,
final BsonDocument command) {
super(requestContext, operationId, requestId, connectionDescription, commandName);
this.databaseName = databaseName;
this.command = command;
}

/**
* Construct an instance.
*
Expand All @@ -40,13 +60,14 @@ public final class CommandStartedEvent extends CommandEvent {
* @param commandName the command name
* @param command the command as a BSON document
* @since 4.4
* @deprecated Prefer {@link
* CommandStartedEvent#CommandStartedEvent(RequestContext, long, int, ConnectionDescription, String, String, BsonDocument)}
*/
@Deprecated
public CommandStartedEvent(@Nullable final RequestContext requestContext, final int requestId,
final ConnectionDescription connectionDescription, final String databaseName, final String commandName,
final BsonDocument command) {
super(requestContext, requestId, connectionDescription, commandName);
this.command = command;
this.databaseName = databaseName;
this(requestContext, -1, requestId, connectionDescription, databaseName, commandName, command);
}

/**
Expand All @@ -57,7 +78,10 @@ public CommandStartedEvent(@Nullable final RequestContext requestContext, final
* @param databaseName the database name
* @param commandName the command name
* @param command the command as a BSON document
* @deprecated Prefer {@link
* CommandStartedEvent#CommandStartedEvent(RequestContext, long, int, ConnectionDescription, String, String, BsonDocument)}
*/
@Deprecated
public CommandStartedEvent(final int requestId, final ConnectionDescription connectionDescription,
final String databaseName, final String commandName, final BsonDocument command) {
this(null, requestId, connectionDescription, databaseName, commandName, command);
Expand Down
29 changes: 26 additions & 3 deletions driver-core/src/main/com/mongodb/event/CommandSucceededEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,54 @@ public final class CommandSucceededEvent extends CommandEvent {

/**
* Construct an instance.
*
* @param requestContext the request context
* @param operationId the operation id
* @param requestId the request id
* @param connectionDescription the connection description
* @param commandName the command name
* @param response the command response
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
* @since 4.4
* @since 4.10
*/
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final int requestId,
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final long operationId, final int requestId,
final ConnectionDescription connectionDescription, final String commandName, final BsonDocument response,
final long elapsedTimeNanos) {
super(requestContext, requestId, connectionDescription, commandName);
super(requestContext, operationId, requestId, connectionDescription, commandName);
this.response = response;
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
this.elapsedTimeNanos = elapsedTimeNanos;
}

/**
* Construct an instance.
* @param requestContext the request context
* @param requestId the request id
* @param connectionDescription the connection description
* @param commandName the command name
* @param response the command response
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
* @since 4.4
* @deprecated Prefer
* {@link CommandSucceededEvent#CommandSucceededEvent(RequestContext, long, int, ConnectionDescription, String, BsonDocument, long)}
*/
@Deprecated
public CommandSucceededEvent(@Nullable final RequestContext requestContext, final int requestId,
final ConnectionDescription connectionDescription, final String commandName, final BsonDocument response,
final long elapsedTimeNanos) {
this(requestContext, -1, requestId, connectionDescription, commandName, response, elapsedTimeNanos);
}

/**
* Construct an instance.
* @param requestId the request id
* @param connectionDescription the connection description
* @param commandName the command name
* @param response the command response
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
* {@link CommandSucceededEvent#CommandSucceededEvent(RequestContext, long, int, ConnectionDescription, String, BsonDocument, long)}
*/
@Deprecated
public CommandSucceededEvent(final int requestId, final ConnectionDescription connectionDescription, final String commandName,
final BsonDocument response, final long elapsedTimeNanos) {
this(null, requestId, connectionDescription, commandName, response, elapsedTimeNanos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public void sendStartedEvent() {
BsonDocument commandDocumentForEvent = redactionRequired
? new BsonDocument() : commandDocument;

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

if (eventRequired()) {
sendCommandFailedEvent(message, commandName, description, elapsedTimeNanos, commandEventException, commandListener,
requestContext);
requestContext, operationContext);
}
}

Expand Down Expand Up @@ -171,7 +171,7 @@ private void sendSucceededEvent(final BsonDocument reply) {
if (eventRequired()) {
BsonDocument responseDocumentForEvent = redactionRequired ? new BsonDocument() : reply;
sendCommandSucceededEvent(message, commandName, responseDocumentForEvent, description,
elapsedTimeNanos, commandListener, requestContext);
elapsedTimeNanos, commandListener, requestContext, operationContext);
}
}

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

builder.append(". The request ID is %s.");
builder.append(". The request ID is %s and the operation ID is %s.");
entries.add(new Entry("requestId", message.getId()));
entries.add(new Entry("operationId", operationContext.getId()));
}

private String getTruncatedJsonCommand(final BsonDocument commandDocument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ private static boolean isNodeIsRecoveringError(final int errorCode, final String

static void sendCommandStartedEvent(final RequestMessage message, final String databaseName, final String commandName,
final BsonDocument command, final ConnectionDescription connectionDescription,
final CommandListener commandListener, final RequestContext requestContext) {
final CommandListener commandListener, final RequestContext requestContext, final OperationContext operationContext) {
notNull("requestContext", requestContext);
try {
commandListener.commandStarted(new CommandStartedEvent(getRequestContextForEvent(requestContext), message.getId(),
commandListener.commandStarted(new CommandStartedEvent(getRequestContextForEvent(requestContext), operationContext.getId(), message.getId(),
connectionDescription, databaseName, commandName, command));
} catch (Exception e) {
if (PROTOCOL_EVENT_LOGGER.isWarnEnabled()) {
Expand All @@ -291,11 +291,11 @@ static void sendCommandStartedEvent(final RequestMessage message, final String d

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

static void sendCommandFailedEvent(final RequestMessage message, final String commandName,
final ConnectionDescription connectionDescription, final long elapsedTimeNanos,
final Throwable throwable, final CommandListener commandListener, final RequestContext requestContext) {
final Throwable throwable, final CommandListener commandListener, final RequestContext requestContext,
final OperationContext operationContext) {
notNull("requestContext", requestContext);
try {
commandListener.commandFailed(new CommandFailedEvent(getRequestContextForEvent(requestContext), message.getId(),
connectionDescription, commandName, elapsedTimeNanos, throwable));
commandListener.commandFailed(new CommandFailedEvent(getRequestContextForEvent(requestContext),
operationContext.getId(), message.getId(), connectionDescription, commandName, elapsedTimeNanos, throwable));
} catch (Exception e) {
if (PROTOCOL_EVENT_LOGGER.isWarnEnabled()) {
PROTOCOL_EVENT_LOGGER.warn(format("Exception thrown raising command failed event to listener %s", commandListener), e);
Expand Down
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this unused file rather than fix it to use the new API.

This file was deleted.

Loading