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 @@ -1597,6 +1596,7 @@ private OpenTelemetry getOpenTelemetryInstance() {
this.sslTrustStoreType,
this.sslTrustStoreProvider)
.setHttpTransportFactory(this.httpTransportOptions.getHttpTransportFactory())
.setTransportChannelProvider(this.transportChannelProvider)
.build();

OpenTelemetry openTelemetry =
Expand All @@ -1612,24 +1612,22 @@ private OpenTelemetry getOpenTelemetryInstance() {
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,
localLogWriter =
GcpLogWriter.create(
this.gcpTelemetryCredentials,
effectiveProjectId,
this.credentials,
this.headerProvider,
this.universeDomain);
telemetryTransportConfig);
}

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,16 +16,14 @@

package com.google.cloud.bigquery.jdbc;

import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.auth.Credentials;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ImpersonatedCredentials;
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 com.google.common.hash.Hashing;
import io.opentelemetry.api.GlobalOpenTelemetry;
Expand Down Expand Up @@ -117,6 +115,7 @@ static final class TelemetryTransportConfig {
final String sslTrustStoreType;
final String sslTrustStoreProvider;
final HttpTransportFactory httpTransportFactory;
final TransportChannelProvider transportChannelProvider;

private TelemetryTransportConfig(Builder builder) {
this.universeDomain = builder.universeDomain;
Expand All @@ -129,6 +128,7 @@ private TelemetryTransportConfig(Builder builder) {
this.sslTrustStoreType = builder.sslTrustStoreType;
this.sslTrustStoreProvider = builder.sslTrustStoreProvider;
this.httpTransportFactory = builder.httpTransportFactory;
this.transportChannelProvider = builder.transportChannelProvider;
}

static Builder builder() {
Expand All @@ -144,6 +144,7 @@ static final class Builder {
private String sslTrustStoreType;
private String sslTrustStoreProvider;
private HttpTransportFactory httpTransportFactory;
private TransportChannelProvider transportChannelProvider;

Builder setUniverseDomain(String universeDomain) {
this.universeDomain = universeDomain;
Expand Down Expand Up @@ -173,6 +174,11 @@ Builder setHttpTransportFactory(HttpTransportFactory httpTransportFactory) {
return this;
}

Builder setTransportChannelProvider(TransportChannelProvider transportChannelProvider) {
this.transportChannelProvider = transportChannelProvider;
return this;
}

TelemetryTransportConfig build() {
return new TelemetryTransportConfig(this);
}
Expand Down Expand Up @@ -236,13 +242,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 @@ -273,78 +279,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) {
return createLoggingClient(
enableGcpLogExporter,
customOpenTelemetry,
effectiveCredentials,
effectiveProjectId,
fallbackCredentials,
headerProvider,
null);
}

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 = resolveCredentialsFromString(effectiveCredentials, null, 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