Repository navigation
fix(bigquery-jdbc): migrate direct Cloud Logging to GAPIC LoggingClient - #15367
keshavdandeva wants to merge 3 commits into
Conversation
… BigQueryConnection
…nd SDK cleanup on init failure
There was a problem hiding this comment.
Code Review
This pull request replaces the legacy com.google.cloud.logging.Logging client with a new custom GcpLogWriter backed by the GAPIC LoggingClient to handle direct Google Cloud Logging exports with non-blocking flow control and bounded flushes. It also updates BigQueryConnection, BigQueryJdbcOpenTelemetry, and OpenTelemetryJulHandler to integrate with the new writer, and introduces comprehensive unit tests. The review feedback highlights two key improvement opportunities: preventing a potential NullPointerException when connectionId is null by checking it before adding it to the protobuf labels map, and extending project ID resolution to support UserCredentials for local development environments.
| LogEntry.Builder entryBuilder = | ||
| LogEntry.newBuilder() | ||
| .setLogName(LogName.ofProjectLogName(projectId, logId).toString()) | ||
| .setResource(resource) | ||
| .setSeverity(severity) | ||
| .setTimestamp( | ||
| Timestamp.newBuilder() | ||
| .setSeconds(timestamp.getEpochSecond()) | ||
| .setNanos(timestamp.getNano()) | ||
| .build()) | ||
| .setTextPayload(message) | ||
| .putLabels(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY, connectionId); |
There was a problem hiding this comment.
If connectionId is null, calling putLabels with a null value will throw a NullPointerException because protobuf map fields do not allow null keys or values. Since logging can occur from background threads or before a connection ID is registered, we must check if connectionId is non-null before adding it to the labels map. This prevents potential NPEs that could disrupt the logging pipeline.
LogEntry.Builder entryBuilder =
LogEntry.newBuilder()
.setLogName(LogName.ofProjectLogName(projectId, logId).toString())
.setResource(resource)
.setSeverity(severity)
.setTimestamp(
Timestamp.newBuilder()
.setSeconds(timestamp.getEpochSecond())
.setNanos(timestamp.getNano())
.build())
.setTextPayload(message);
if (connectionId != null) {
entryBuilder.putLabels(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY, connectionId);
}| if (credentials instanceof ServiceAccountCredentials) { | ||
| String saProjectId = ((ServiceAccountCredentials) credentials).getProjectId(); | ||
| if (saProjectId != null && !saProjectId.trim().isEmpty()) { | ||
| return saProjectId.trim(); | ||
| } | ||
| } |
There was a problem hiding this comment.
In addition to ServiceAccountCredentials, UserCredentials (e.g., from local gcloud ADC) also contain a project ID that can be resolved. Adding support for UserCredentials ensures the correct project ID is propagated during local development.
| if (credentials instanceof ServiceAccountCredentials) { | |
| String saProjectId = ((ServiceAccountCredentials) credentials).getProjectId(); | |
| if (saProjectId != null && !saProjectId.trim().isEmpty()) { | |
| return saProjectId.trim(); | |
| } | |
| } | |
| if (credentials instanceof ServiceAccountCredentials) { | |
| String saProjectId = ((ServiceAccountCredentials) credentials).getProjectId(); | |
| if (saProjectId != null && !saProjectId.trim().isEmpty()) { | |
| return saProjectId.trim(); | |
| } | |
| } else if (credentials instanceof com.google.auth.oauth2.UserCredentials) { | |
| String userProjectId = ((com.google.auth.oauth2.UserCredentials) credentials).getProjectId(); | |
| if (userProjectId != null && !userProjectId.trim().isEmpty()) { | |
| return userProjectId.trim(); | |
| } | |
| } |
Summary
Migrates driver-managed direct Cloud Logging (
enableGcpLogExporter=true) from the legacycom.google.cloud.logging.Loggingclient to a package-privateGcpLogWriterbacked by GAPICcom.google.cloud.logging.v2.LoggingClient, propagating per-connectionuniverseDomain,TransportChannelProvider(proxy andSSLTrustStore),HeaderProvider, OAuth endpoint overrides, andlogging.write-scoped credentials.Key Decisions
LoggingClientviaGcpLogWriter: LegacyGrpcLoggingRpcbuilds its own gRPC channel and does not accept a customTransportChannelProvider. Using GAPICLoggingClientlets direct Cloud Logging reuse the connection's proxy/TrustStoreTransportChannelProvider(supporting both authenticated and unauthenticated proxies) as well asuniverseDomainandHeaderProvider.AtomicBooleanclosed flag, aThreadLocalre-entrancy guard, aConcurrentHashMapof in-flightApiFutures, and GAX's non-blockingThrowExceptionflow-control behavior.close()awaits pending writes up to a bounded timeout and falls back toshutdownNow()while suppressing duplicate cancellation warnings.MonitoredResourceenvironment detection: Lazily detects the host monitored resource once per JVM and stampsproject_idper connection/project instead of callingMonitoredResourceUtil.getResource(projectId, null)on every connection.OpenTelemetryJulHandler.flush()waits on a single shared deadline across all active writers. Initialization failures (invalid/mismatcheduniverseDomain, missing project ID, transport initialization errors) throw checkedBigQueryJdbcException, andBigQueryConnectionreleases its cached OpenTelemetry SDK if log writer creation fails.Behavior Changes
universeDomain,ProxyHost/ProxyPort/ProxyUid/ProxyPwd,SSLTrustStore*, OAuthEndpointOverrides, and connection headers (user-agent,x-goog-request-reason), and deriveshttps://www.googleapis.com/auth/logging.writescope when needed.traceSampledalongsidetraceandspanId.BigQueryJdbcException(SQLException).