Skip to content

fix(bigquery-jdbc): migrate direct Cloud Logging to GAPIC LoggingClient - #15367

Closed
keshavdandeva wants to merge 3 commits into
fix/jdbc-telemetry-v2-2-otel-programmatic-universefrom
fix/jdbc-telemetry-v2-3-gapic-cloud-logging
Closed

keshavdandeva wants to merge 3 commits into
fix/jdbc-telemetry-v2-2-otel-programmatic-universefrom
fix/jdbc-telemetry-v2-3-gapic-cloud-logging

Conversation

@keshavdandeva

Copy link
Copy Markdown
Contributor

Summary

Migrates driver-managed direct Cloud Logging (enableGcpLogExporter=true) from the legacy com.google.cloud.logging.Logging client to a package-private GcpLogWriter backed by GAPIC com.google.cloud.logging.v2.LoggingClient, propagating per-connection universeDomain, TransportChannelProvider (proxy and SSLTrustStore), HeaderProvider, OAuth endpoint overrides, and logging.write-scoped credentials.

Key Decisions

  • GAPIC LoggingClient via GcpLogWriter: Legacy GrpcLoggingRpc builds its own gRPC channel and does not accept a custom TransportChannelProvider. Using GAPIC LoggingClient lets direct Cloud Logging reuse the connection's proxy/TrustStore TransportChannelProvider (supporting both authenticated and unauthenticated proxies) as well as universeDomain and HeaderProvider.
  • Non-blocking async writes with lock-free bounded flush: Keeps the best-effort log sink lightweight using an AtomicBoolean closed flag, a ThreadLocal re-entrancy guard, a ConcurrentHashMap of in-flight ApiFutures, and GAX's non-blocking ThrowException flow-control behavior. close() awaits pending writes up to a bounded timeout and falls back to shutdownNow() while suppressing duplicate cancellation warnings.
  • Per-JVM cached MonitoredResource environment detection: Lazily detects the host monitored resource once per JVM and stamps project_id per connection/project instead of calling MonitoredResourceUtil.getResource(projectId, null) on every connection.
  • Shared flush deadline and checked connect-time errors: OpenTelemetryJulHandler.flush() waits on a single shared deadline across all active writers. Initialization failures (invalid/mismatched universeDomain, missing project ID, transport initialization errors) throw checked BigQueryJdbcException, and BigQueryConnection releases its cached OpenTelemetry SDK if log writer creation fails.

Behavior Changes

  • Direct Cloud Logging now honors per-connection universeDomain, ProxyHost/ProxyPort/ProxyUid/ProxyPwd, SSLTrustStore*, OAuth EndpointOverrides, and connection headers (user-agent, x-goog-request-reason), and derives https://www.googleapis.com/auth/logging.write scope when needed.
  • Cloud Logging entries now populate traceSampled alongside trace and spanId.
  • Flow-control overflow and async export failures are non-blocking, log the first error once, and report dropped or aborted write counts on close.
  • Invalid Cloud Logging setup fails connection creation with a checked BigQueryJdbcException (SQLException).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +217 to +228
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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);
      }

Comment on lines +185 to +190
if (credentials instanceof ServiceAccountCredentials) {
String saProjectId = ((ServiceAccountCredentials) credentials).getProjectId();
if (saProjectId != null && !saProjectId.trim().isEmpty()) {
return saProjectId.trim();
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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();
}
}

@keshavdandeva

Copy link
Copy Markdown
Contributor Author

Superseded by #15373, rebased onto the split OpenTelemetry trace PRs (#15371 → #15372).

@keshavdandeva
keshavdandeva deleted the fix/jdbc-telemetry-v2-3-gapic-cloud-logging branch December 2, 2026 00:26
@keshavdandeva
keshavdandeva removed this pull request from stack #15369 December 2, 2026 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant