Skip to content

Commit

Permalink
feat: add support for debug logging (#1935)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttosta-google committed Apr 10, 2024
1 parent 95af7e3 commit 473ac85
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ mysql-socket-factory-connector-j-8–1.8.0-jar-with-dependencies.jar
postgres-socket-factory-1.8.0-jar-with-dependencies.jar
```

### Debug Logging

The Java Connector supports optional debug logging to help diagnose problems with
the background certificate refresh. To enable it, add the following to the file
`/src/main/resources/application.yml`:

```
logging.level.root=DEBUG
```

---

### Firewall configuration
Expand Down
21 changes: 17 additions & 4 deletions core/src/main/java/com/google/cloud/sql/core/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,28 @@ Socket connect(ConnectionConfig config, long timeoutMs) throws IOException {
DefaultConnectionInfoCache instance = getConnection(config);
try {

SSLSocket socket = instance.createSslSocket(timeoutMs);
String instanceIp = instance.getConnectionMetadata(timeoutMs).getPreferredIpAddress();
logger.debug(String.format("[%s] Connecting to instance.", instanceIp));

SSLSocket socket = instance.createSslSocket(timeoutMs);
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.connect(new InetSocketAddress(instanceIp, serverProxyPort));

String instanceIp = instance.getConnectionMetadata(timeoutMs).getPreferredIpAddress();
try {
socket.startHandshake();
} catch (IOException e) {
logger.debug("TLS handshake failed!");
throw e;
}

socket.connect(new InetSocketAddress(instanceIp, serverProxyPort));
socket.startHandshake();
logger.debug(String.format("[%s] Connected to instance successfully.", instanceIp));

return socket;
} catch (IOException e) {
logger.debug(
String.format(
"[%s] Socket connection failed! Trigger a refresh.", config.getCloudSqlInstance()));
instance.forceRefresh();
throw e;
}
Expand All @@ -142,11 +152,14 @@ DefaultConnectionInfoCache getConnection(ConnectionConfig config) {
}

private DefaultConnectionInfoCache createConnectionInfo(ConnectionConfig config) {
logger.debug(
String.format("[%s] Connection info added to cache.", config.getCloudSqlInstance()));
return new DefaultConnectionInfoCache(
config, adminApi, instanceCredentialFactory, executor, localKeyPair, minRefreshDelayMs);
}

public void close() {
logger.debug("Close all connections and remove them from cache.");
this.instances.forEach((key, c) -> c.close());
this.instances.clear();
}
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/com/google/cloud/sql/core/Refresher.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ void forceRefresh() {
/** Force a new refresh of the instance data if the client certificate has expired. */
void refreshIfExpired() {
ConnectionInfo info = getConnectionInfo(DEFAULT_CONNECT_TIMEOUT_MS);
logger.debug(
String.format(
"[%s] Now = %s, Current client certificate expiration = %s",
name, Instant.now().toString(), info.getExpiration()));
if (Instant.now().isAfter(info.getExpiration())) {
logger.debug(
String.format(
Expand Down Expand Up @@ -268,11 +272,11 @@ private ListenableFuture<ConnectionInfo> handleRefreshResult(
// No refresh retry when the TerminalException is raised.
final Throwable cause = e.getCause();
if (cause instanceof TerminalException) {
logger.info(String.format("[%s] Refresh Operation: Failed! No retry.", name), e);
logger.debug(String.format("[%s] Refresh Operation: Failed! No retry.", name), e);
throw (TerminalException) cause;
}

logger.info(
logger.debug(
String.format(
"[%s] Refresh Operation: Failed! Starting next refresh operation immediately.", name),
e);
Expand Down

0 comments on commit 473ac85

Please sign in to comment.