Skip to content

Commit 473ac85

Browse files
authoredApr 10, 2024··
feat: add support for debug logging (#1935)
1 parent 95af7e3 commit 473ac85

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed
 

‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ mysql-socket-factory-connector-j-8–1.8.0-jar-with-dependencies.jar
3939
postgres-socket-factory-1.8.0-jar-with-dependencies.jar
4040
```
4141

42+
### Debug Logging
43+
44+
The Java Connector supports optional debug logging to help diagnose problems with
45+
the background certificate refresh. To enable it, add the following to the file
46+
`/src/main/resources/application.yml`:
47+
48+
```
49+
logging.level.root=DEBUG
50+
```
51+
4252
---
4353

4454
### Firewall configuration

‎core/src/main/java/com/google/cloud/sql/core/Connector.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,28 @@ Socket connect(ConnectionConfig config, long timeoutMs) throws IOException {
110110
DefaultConnectionInfoCache instance = getConnection(config);
111111
try {
112112

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

116+
SSLSocket socket = instance.createSslSocket(timeoutMs);
115117
socket.setKeepAlive(true);
116118
socket.setTcpNoDelay(true);
119+
socket.connect(new InetSocketAddress(instanceIp, serverProxyPort));
117120

118-
String instanceIp = instance.getConnectionMetadata(timeoutMs).getPreferredIpAddress();
121+
try {
122+
socket.startHandshake();
123+
} catch (IOException e) {
124+
logger.debug("TLS handshake failed!");
125+
throw e;
126+
}
119127

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

123130
return socket;
124131
} catch (IOException e) {
132+
logger.debug(
133+
String.format(
134+
"[%s] Socket connection failed! Trigger a refresh.", config.getCloudSqlInstance()));
125135
instance.forceRefresh();
126136
throw e;
127137
}
@@ -142,11 +152,14 @@ DefaultConnectionInfoCache getConnection(ConnectionConfig config) {
142152
}
143153

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

149161
public void close() {
162+
logger.debug("Close all connections and remove them from cache.");
150163
this.instances.forEach((key, c) -> c.close());
151164
this.instances.clear();
152165
}

‎core/src/main/java/com/google/cloud/sql/core/Refresher.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ void forceRefresh() {
182182
/** Force a new refresh of the instance data if the client certificate has expired. */
183183
void refreshIfExpired() {
184184
ConnectionInfo info = getConnectionInfo(DEFAULT_CONNECT_TIMEOUT_MS);
185+
logger.debug(
186+
String.format(
187+
"[%s] Now = %s, Current client certificate expiration = %s",
188+
name, Instant.now().toString(), info.getExpiration()));
185189
if (Instant.now().isAfter(info.getExpiration())) {
186190
logger.debug(
187191
String.format(
@@ -268,11 +272,11 @@ private ListenableFuture<ConnectionInfo> handleRefreshResult(
268272
// No refresh retry when the TerminalException is raised.
269273
final Throwable cause = e.getCause();
270274
if (cause instanceof TerminalException) {
271-
logger.info(String.format("[%s] Refresh Operation: Failed! No retry.", name), e);
275+
logger.debug(String.format("[%s] Refresh Operation: Failed! No retry.", name), e);
272276
throw (TerminalException) cause;
273277
}
274278

275-
logger.info(
279+
logger.debug(
276280
String.format(
277281
"[%s] Refresh Operation: Failed! Starting next refresh operation immediately.", name),
278282
e);

0 commit comments

Comments
 (0)
Please sign in to comment.