-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bqjdbc): Per connection logs - Add PerConnectionFileHandler #12899
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
75a80e0
add PerConnectionFileHandler
Neenu1995 00a5cb4
add tests
Neenu1995 560a31c
Apply suggestion from @gemini-code-assist[bot]
Neenu1995 99d3f90
address gemini comments
Neenu1995 a5b81c7
remove lambda
Neenu1995 76d67a9
nit
Neenu1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| drivers/** | ||
| target-it/** | ||
| *logs/** | ||
| *logs**/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * 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.google.cloud.bigquery.jdbc; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.logging.FileHandler; | ||
| import java.util.logging.Handler; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.LogRecord; | ||
|
|
||
| /** | ||
| * Custom logging handler that dynamically creates and routes log records to per-connection specific | ||
| * log files using the connection ID retrieved from BigQueryJdbcMdc. | ||
| */ | ||
| class PerConnectionFileHandler extends Handler { | ||
| private final String baseLogPath; | ||
| private final Level level; | ||
| private final ConcurrentHashMap<String, FileHandler> handlers = new ConcurrentHashMap<>(); | ||
| private FileHandler defaultHandler; | ||
|
|
||
| PerConnectionFileHandler(String baseLogPath, Level level) { | ||
| this.baseLogPath = baseLogPath; | ||
|
Neenu1995 marked this conversation as resolved.
Outdated
|
||
| this.level = level; | ||
|
|
||
| try { | ||
| if (!baseLogPath.isEmpty()) { | ||
| Path dir = Paths.get(baseLogPath); | ||
| if (!Files.exists(dir)) { | ||
| Files.createDirectories(dir); | ||
| } | ||
| } | ||
|
|
||
| String defaultPath = getLogFilePath("Jdbc-default"); | ||
| this.defaultHandler = new FileHandler(defaultPath, 0, 1, true); | ||
| this.defaultHandler.setLevel(level); | ||
| this.defaultHandler.setFormatter(BigQueryJdbcRootLogger.getFormatter()); | ||
| } catch (IOException e) { | ||
| System.err.println("Failed to initialize default log file: " + e.getMessage()); | ||
|
Neenu1995 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| private String getLogFilePath(String id) { | ||
| String path = baseLogPath; | ||
|
Neenu1995 marked this conversation as resolved.
Outdated
|
||
| if (!path.isEmpty() && !path.endsWith("/")) { | ||
| path = path + "/"; | ||
| } | ||
| return path + "BigQuery-" + id + ".log"; | ||
| } | ||
|
|
||
| @Override | ||
| public void publish(LogRecord record) { | ||
| if (!isLoggable(record)) { | ||
| return; | ||
| } | ||
|
|
||
| String connectionId = BigQueryJdbcMdc.getConnectionId(); | ||
| FileHandler handler = defaultHandler; | ||
|
|
||
| if (connectionId != null && !connectionId.isEmpty()) { | ||
| handler = | ||
| handlers.computeIfAbsent( | ||
| connectionId, | ||
| id -> { | ||
| try { | ||
|
Neenu1995 marked this conversation as resolved.
Outdated
|
||
| String filePath = getLogFilePath(id); | ||
| FileHandler fh = new FileHandler(filePath, 0, 1, true); | ||
| fh.setLevel(level); | ||
| fh.setFormatter(BigQueryJdbcRootLogger.getFormatter()); | ||
| return fh; | ||
| } catch (IOException e) { | ||
| System.err.println( | ||
| "Failed to create log file for connection " + id + ": " + e.getMessage()); | ||
|
Neenu1995 marked this conversation as resolved.
Outdated
|
||
| return defaultHandler; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (handler != null) { | ||
|
Neenu1995 marked this conversation as resolved.
|
||
| handler.publish(record); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() { | ||
| if (defaultHandler != null) { | ||
| defaultHandler.flush(); | ||
| } | ||
| for (FileHandler h : handlers.values()) { | ||
| h.flush(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws SecurityException { | ||
| if (defaultHandler != null) { | ||
| defaultHandler.close(); | ||
| } | ||
| for (FileHandler h : handlers.values()) { | ||
| h.close(); | ||
| } | ||
| } | ||
|
Neenu1995 marked this conversation as resolved.
|
||
|
|
||
| public void closeHandler(String connectionId) { | ||
| if (connectionId != null) { | ||
|
Neenu1995 marked this conversation as resolved.
|
||
| FileHandler handler = handlers.remove(connectionId); | ||
| if (handler != null) { | ||
| handler.close(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
100 changes: 100 additions & 0 deletions
100
...query-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * 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.google.cloud.bigquery.jdbc; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.LogRecord; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.mockito.Mockito; | ||
|
|
||
| public class PerConnectionFileHandlerTest { | ||
|
|
||
| @TempDir | ||
| Path tempDir; | ||
|
|
||
| private PerConnectionFileHandler handler; | ||
| private BigQueryConnection mockConnection; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| handler = new PerConnectionFileHandler(tempDir.toString(), Level.INFO); | ||
| mockConnection = Mockito.mock(BigQueryConnection.class); | ||
| BigQueryJdbcMdc.clear(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| if (handler != null) { | ||
| handler.close(); | ||
| } | ||
| BigQueryJdbcMdc.clear(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInitialization() { | ||
| Path defaultLog = tempDir.resolve("BigQuery-Jdbc-default.log"); | ||
| assertTrue(Files.exists(defaultLog)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPublishDefault() throws IOException { | ||
| LogRecord record = new LogRecord(Level.INFO, "Test message default"); | ||
| handler.publish(record); | ||
| handler.flush(); | ||
|
|
||
| Path defaultLog = tempDir.resolve("BigQuery-Jdbc-default.log"); | ||
| String content = new String(Files.readAllBytes(defaultLog)); | ||
| assertTrue(content.contains("Test message default")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPublishConnectionSpecific() throws IOException { | ||
| BigQueryJdbcMdc.registerInstance(mockConnection, "123"); | ||
|
|
||
| LogRecord record = new LogRecord(Level.INFO, "Test message connection 123"); | ||
| handler.publish(record); | ||
| handler.flush(); | ||
|
|
||
| Path connLog = tempDir.resolve("BigQuery-JdbcConnection-123.log"); | ||
| assertTrue(Files.exists(connLog)); | ||
| String content = new String(Files.readAllBytes(connLog)); | ||
| assertTrue(content.contains("Test message connection 123")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCloseHandler() { | ||
| BigQueryJdbcMdc.registerInstance(mockConnection, "456"); | ||
|
|
||
| LogRecord record = new LogRecord(Level.INFO, "Test message connection 456"); | ||
| handler.publish(record); | ||
| handler.flush(); | ||
|
|
||
| Path connLog = tempDir.resolve("BigQuery-JdbcConnection-456.log"); | ||
| assertTrue(Files.exists(connLog)); | ||
|
|
||
| handler.closeHandler("JdbcConnection-456"); | ||
| assertTrue(Files.exists(connLog)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.