-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bqjdbc): Implement Correlated OpenTelemetry Logging Bridge #13002
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
keshavdandeva
merged 11 commits into
jdbc/feature-branch-otel
from
jdbc/otel-jul-handler
May 7, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e0dd660
feat(bqjdbc): Implement Correlated OpenTelemetry Logging Bridge
keshavdandeva 8d72c8a
chore: add gpc logging handler
keshavdandeva 93b15fb
chore: update test and add flush method
keshavdandeva 6f3ac5a
chore: use global handler
keshavdandeva e0ccb57
chore: use formatted logs
keshavdandeva 9655146
fix: java 8 tests
keshavdandeva 996b987
chore: address pr comments
keshavdandeva 6481bff
fix: split unit tests
keshavdandeva 315f9ad
fix tests
keshavdandeva 19121c4
fix: failing tests
keshavdandeva ea35185
chore: address pr feedback
keshavdandeva 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
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
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
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
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
186 changes: 186 additions & 0 deletions
186
...d-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandler.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,186 @@ | ||
| /* | ||
| * 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 com.google.cloud.logging.LogEntry; | ||
| import com.google.cloud.logging.Logging; | ||
| import com.google.cloud.logging.Payload; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.baggage.Baggage; | ||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.logs.LogRecordBuilder; | ||
| import io.opentelemetry.api.logs.Logger; | ||
| import io.opentelemetry.api.logs.Severity; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.context.Context; | ||
| import java.time.Instant; | ||
| import java.util.Collections; | ||
| import java.util.logging.Handler; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.LogRecord; | ||
|
|
||
| /** | ||
| * Custom logging handler that bridges java.util.logging records to OpenTelemetry or Google Cloud | ||
| * Logging. Extracts TraceId, SpanId, and Connection UUID from context. | ||
| */ | ||
| public class OpenTelemetryJulHandler extends Handler { | ||
|
|
||
| public OpenTelemetryJulHandler() {} | ||
|
|
||
| @Override | ||
| public void publish(LogRecord record) { | ||
| if (!isLoggable(record)) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Extract connection ID from baggage | ||
| String connectionId = | ||
| Baggage.fromContext(Context.current()) | ||
| .getEntryValue(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY); | ||
|
|
||
| // Fallback to MDC if not in baggage (if MDC is available and used) | ||
| if (connectionId == null) { | ||
| connectionId = BigQueryJdbcMdc.getConnectionId(); | ||
| } | ||
|
|
||
| if (connectionId == null) { | ||
|
logachev marked this conversation as resolved.
|
||
| return; | ||
| } | ||
|
|
||
| BigQueryJdbcOpenTelemetry.TelemetryConfig config = | ||
| BigQueryJdbcOpenTelemetry.getConnectionConfig(connectionId); | ||
| if (config == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (config.useDirectGcpLogging && config.loggingClient != null) { | ||
| publishToGcp(record, connectionId, config.loggingClient); | ||
| } else if (config.openTelemetry != null) { | ||
| publishToOTel(record, connectionId, config.openTelemetry); | ||
| } | ||
| } catch (Throwable t) { | ||
| // Ignore exceptions to prevent breaking application logging or other handlers | ||
| } | ||
| } | ||
|
|
||
| private void publishToGcp(LogRecord record, String connectionId, Logging loggingClient) { | ||
| Context context = Context.current(); | ||
| SpanContext spanContext = Span.fromContext(context).getSpanContext(); | ||
| String traceId = spanContext.isValid() ? spanContext.getTraceId() : null; | ||
| String spanId = spanContext.isValid() ? spanContext.getSpanId() : null; | ||
|
|
||
| // TODO(b/491238299): May require refinement for structured logging or error handling | ||
|
|
||
| LogEntry.Builder builder = | ||
| LogEntry.newBuilder(Payload.StringPayload.of(formatMessage(record))) | ||
| .setSeverity(mapGcpSeverity(record.getLevel())) | ||
| .setTimestamp(record.getMillis()); | ||
|
|
||
| if (traceId != null) { | ||
| builder.setTrace(traceId); | ||
|
keshavdandeva marked this conversation as resolved.
|
||
| } | ||
| if (spanId != null) { | ||
| builder.setSpanId(spanId); | ||
| } | ||
| if (connectionId != null) { | ||
| builder.addLabel(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY, connectionId); | ||
| } | ||
|
|
||
| loggingClient.write(Collections.singleton(builder.build())); | ||
| } | ||
|
|
||
| private com.google.cloud.logging.Severity mapGcpSeverity(Level level) { | ||
| if (level == Level.SEVERE) return com.google.cloud.logging.Severity.ERROR; | ||
|
logachev marked this conversation as resolved.
|
||
| if (level == Level.WARNING) return com.google.cloud.logging.Severity.WARNING; | ||
| if (level == Level.INFO) return com.google.cloud.logging.Severity.INFO; | ||
| if (level == Level.CONFIG) return com.google.cloud.logging.Severity.INFO; | ||
| if (level == Level.FINE) return com.google.cloud.logging.Severity.DEBUG; | ||
| return com.google.cloud.logging.Severity.DEBUG; | ||
| } | ||
|
|
||
| private void publishToOTel(LogRecord record, String connectionId, OpenTelemetry openTelemetry) { | ||
| String loggerName = record.getLoggerName(); | ||
| Logger logger = | ||
| openTelemetry | ||
| .getLogsBridge() | ||
| .get( | ||
| loggerName != null | ||
| ? loggerName | ||
| : BigQueryJdbcOpenTelemetry.INSTRUMENTATION_SCOPE_NAME); | ||
|
|
||
| LogRecordBuilder builder = | ||
| logger | ||
| .logRecordBuilder() | ||
| .setBody(formatMessage(record)) | ||
| .setSeverity(mapSeverity(record.getLevel())) | ||
| .setTimestamp(Instant.ofEpochMilli(record.getMillis())) | ||
| .setContext(Context.current()); | ||
|
|
||
| if (connectionId != null) { | ||
| builder.setAttribute( | ||
| AttributeKey.stringKey(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY), | ||
| connectionId); | ||
| } | ||
|
|
||
| builder.emit(); | ||
| } | ||
|
|
||
| private Severity mapSeverity(Level level) { | ||
| if (level == Level.SEVERE) return Severity.ERROR; | ||
| if (level == Level.WARNING) return Severity.WARN; | ||
| if (level == Level.INFO) return Severity.INFO; | ||
| if (level == Level.CONFIG) return Severity.INFO; | ||
| if (level == Level.FINE) return Severity.DEBUG; | ||
| if (level == Level.FINER) return Severity.TRACE; | ||
| if (level == Level.FINEST) return Severity.TRACE; | ||
| return Severity.TRACE; | ||
| } | ||
|
|
||
| private String formatMessage(LogRecord record) { | ||
| String message = record.getMessage(); | ||
| Object[] params = record.getParameters(); | ||
| if (params != null && params.length > 0) { | ||
| try { | ||
| return java.text.MessageFormat.format(message, params); | ||
| } catch (IllegalArgumentException e) { | ||
| return message; | ||
| } | ||
| } | ||
| return message; | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() { | ||
| for (BigQueryJdbcOpenTelemetry.TelemetryConfig config : | ||
| BigQueryJdbcOpenTelemetry.getRegisteredConfigs()) { | ||
| if (config.useDirectGcpLogging && config.loggingClient != null) { | ||
| try { | ||
| config.loggingClient.flush(); | ||
| } catch (Exception e) { | ||
| // Ignore failures during flush to protect other connections | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
keshavdandeva marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public void close() throws SecurityException { | ||
| // TODO(b/491238299): Implement with gcp exporter logic | ||
| } | ||
|
keshavdandeva marked this conversation as resolved.
|
||
| } | ||
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
Oops, something went wrong.
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.