Skip to content

Add structured log messages to CMAP. #1114

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 22 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
import static com.mongodb.internal.connection.ProtocolHelper.getSnapshotTimestamp;
import static com.mongodb.internal.connection.ProtocolHelper.isCommandOk;
import static com.mongodb.internal.logging.StructuredLogMessage.Level.DEBUG;
import static java.lang.String.format;
import static java.util.Arrays.asList;

/**
Expand Down Expand Up @@ -289,9 +288,6 @@ private void initAfterHandshakeFinish(final InternalConnectionInitializationDesc
initialServerDescription = initializationDescription.getServerDescription();
opened.set(true);
sendCompressor = findSendCompressor(description);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Opened connection [%s] to %s", getId(), serverId.getAddress()));
}
}

private Map<Byte, Compressor> createCompressorMap(final List<MongoCompressor> compressorList) {
Expand Down Expand Up @@ -337,13 +333,8 @@ private Compressor createCompressor(final MongoCompressor mongoCompressor) {
@Override
public void close() {
// All but the first call is a no-op
if (!isClosed.getAndSet(true)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Closing connection %s", getId()));
}
if (stream != null) {
if (!isClosed.getAndSet(true) && (stream != null)) {
stream.close();
}
}
}

Expand Down Expand Up @@ -655,9 +646,6 @@ public void receiveMessageAsync(final int responseTo, final SingleResultCallback
return;
}

if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Start receiving response on %s", getId()));
}
readAsync(MESSAGE_HEADER_LENGTH, new MessageHeaderCallback((result, t) -> {
if (t != null) {
close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.internal.event;

import com.mongodb.event.ConnectionCheckOutFailedEvent;
import com.mongodb.event.ConnectionClosedEvent;

/**
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public final class EventReasonMessageResolver {
private static final String MESSAGE_CONNECTION_POOL_WAS_CLOSED = "Connection pool was closed";

public static String getMessage(final ConnectionClosedEvent.Reason reason) {
switch (reason) {
case STALE:
return "Connection became stale because the pool was cleared";
case IDLE:
return "Connection has been available but unused for longer than the configured max idle time";
case ERROR:
return "An error occurred while using the connection";
case POOL_CLOSED:
return MESSAGE_CONNECTION_POOL_WAS_CLOSED;
default:
return "";
}
}

public static String getMessage(final ConnectionCheckOutFailedEvent.Reason reason) {
switch (reason) {
case TIMEOUT:
return "Wait queue timeout elapsed without a connection becoming available";
case CONNECTION_ERROR:
return "An error occurred while trying to establish a new connection";
case POOL_CLOSED:
return MESSAGE_CONNECTION_POOL_WAS_CLOSED;
default:
return "";
}
}

private EventReasonMessageResolver() {
//NOP
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ public final class StructuredLogMessage {
private final List<Entry> entries;

public enum Component {
COMMAND
COMMAND,
CONNECTION
}

public enum Level {
DEBUG
}

public static final class Entry {
public static final String NAME_SERVER_HOST = "serverHost";
public static final String NAME_SERVER_PORT = "serverPort";
private final String name;
private final Object value;

Expand Down
Loading