Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
import com.google.cloud.bigquery.storage.v1.BigQueryWriteSettings;
import com.google.cloud.http.HttpTransportOptions;
import com.google.cloud.logging.Logging;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
Expand Down Expand Up @@ -1606,6 +1605,7 @@ TelemetryTransportConfig createTelemetryTransportConfig() {
this.sslTrustStoreType,
this.sslTrustStoreProvider)
.setHttpTransportFactory(this.httpTransportOptions.getHttpTransportFactory())
.setTransportChannelProvider(this.transportChannelProvider)
.build();
}

Expand All @@ -1630,24 +1630,16 @@ private OpenTelemetry getOpenTelemetryInstance() throws SQLException {
this.openTelemetry = openTelemetry;

boolean hasExternalOtel = this.customOpenTelemetry != null || this.useGlobalOpenTelemetry;
Logging localLoggingClient = null;
GcpLogWriter localLogWriter = null;
if (this.enableGcpLogExporter && !hasExternalOtel) {
localLoggingClient =
BigQueryJdbcOpenTelemetry.createLoggingClient(
true,
null,
this.gcpTelemetryCredentials,
transportConfig.projectId,
this.credentials,
this.headerProvider,
this.universeDomain);
localLogWriter = GcpLogWriter.create(this.headerProvider, transportConfig);
}

if (this.enableGcpLogExporter || hasExternalOtel) {
BigQueryJdbcOpenTelemetry.registerConnection(
this.connectionId,
openTelemetry,
localLoggingClient,
localLogWriter,
this.enableGcpLogExporter && !hasExternalOtel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package com.google.cloud.bigquery.jdbc;

import com.google.api.gax.rpc.HeaderProvider;
import com.google.auth.Credentials;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.LoggingOptions;
import com.google.common.annotations.VisibleForTesting;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
Expand Down Expand Up @@ -107,13 +104,13 @@ private static final class CachedSdk {

static class TelemetryConfig {
final OpenTelemetry openTelemetry;
final Logging loggingClient;
final GcpLogWriter gcpLogWriter;
final boolean useDirectGcpLogging;

TelemetryConfig(
OpenTelemetry openTelemetry, Logging loggingClient, Boolean useDirectGcpLogging) {
OpenTelemetry openTelemetry, GcpLogWriter gcpLogWriter, Boolean useDirectGcpLogging) {
this.openTelemetry = openTelemetry;
this.loggingClient = loggingClient;
this.gcpLogWriter = gcpLogWriter;
this.useDirectGcpLogging = useDirectGcpLogging != null ? useDirectGcpLogging : false;
}
}
Expand Down Expand Up @@ -186,63 +183,23 @@ public static synchronized void ensureGlobalHandlerAttached() {
public static void registerConnection(
String connectionId,
OpenTelemetry openTelemetry,
Logging loggingClient,
GcpLogWriter gcpLogWriter,
Boolean useDirectGcpLogging) {
connectionConfigs.put(
connectionId, new TelemetryConfig(openTelemetry, loggingClient, useDirectGcpLogging));
connectionId, new TelemetryConfig(openTelemetry, gcpLogWriter, useDirectGcpLogging));
}

public static void unregisterConnection(String connectionId) {
TelemetryConfig config = connectionConfigs.remove(connectionId);
if (config != null && config.loggingClient != null) {
if (config != null && config.gcpLogWriter != null) {
try {
config.loggingClient.close();
config.gcpLogWriter.close();
} catch (Exception e) {
LOG.warning("Failed to close Logging client during unregister: %s", e.getMessage());
}
}
}

public static Logging createLoggingClient(
boolean enableGcpLogExporter,
OpenTelemetry customOpenTelemetry,
String effectiveCredentials,
String effectiveProjectId,
Credentials fallbackCredentials,
HeaderProvider headerProvider,
String universeDomain) {

if (!enableGcpLogExporter || customOpenTelemetry != null) {
return null;
}

try {
Credentials credentials;
if (effectiveCredentials != null) {
credentials =
TelemetryTransportConfig.resolveCredentialsFromString(
effectiveCredentials, Collections.emptyMap(), null);
} else {
credentials = fallbackCredentials;
}

LoggingOptions.Builder loggingOptionsBuilder =
LoggingOptions.newBuilder().setProjectId(effectiveProjectId);
if (credentials != null) {
loggingOptionsBuilder.setCredentials(credentials);
}
if (headerProvider != null) {
loggingOptionsBuilder.setHeaderProvider(headerProvider);
}
if (universeDomain != null) {
loggingOptionsBuilder.setUniverseDomain(universeDomain);
}
return loggingOptionsBuilder.build().getService();
} catch (Exception e) {
throw new BigQueryJdbcRuntimeException("Failed to initialize Logging client", e);
}
}

public static void releaseSdk(OpenTelemetry openTelemetry) {
if (openTelemetry == null || openTelemetry == OpenTelemetry.noop()) {
return;
Expand Down
Loading
Loading