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 @@ -1591,6 +1591,11 @@ private OpenTelemetry getOpenTelemetryInstance() {
.setUniverseDomain(this.universeDomain)
.setOverrideProperties(this.overrideProperties)
.setProxyProperties(this.proxyProperties)
.setSslTrustStore(
this.sslTrustStorePath,
this.sslTrustStorePassword,
this.sslTrustStoreType,
this.sslTrustStoreProvider)
.setHttpTransportFactory(this.httpTransportOptions.getHttpTransportFactory())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.common.export.ProxyOptions;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.export.SpanExporter;
Expand Down Expand Up @@ -81,6 +82,10 @@ class BigQueryJdbcOpenTelemetry {

private static final String OTEL_TRACES_EXPORTER = "otel.traces.exporter";
private static final String OTEL_EXPORTER_OTLP_ENDPOINT = "otel.exporter.otlp.endpoint";
private static final String OTEL_EXPORTER_OTLP_PROTOCOL = "otel.exporter.otlp.protocol";
private static final String OTEL_EXPORTER_OTLP_TRACES_PROTOCOL =
"otel.exporter.otlp.traces.protocol";
private static final String PROTOCOL_HTTP_PROTOBUF = "http/protobuf";
private static final String OTEL_LOGS_EXPORTER = "otel.logs.exporter";
private static final String OTEL_METRICS_EXPORTER = "otel.metrics.exporter";
private static final String GOOGLE_CLOUD_PROJECT = "google.cloud.project";
Expand All @@ -107,6 +112,10 @@ static final class TelemetryTransportConfig {
final String universeDomain;
final Map<String, String> overrideProperties;
final Map<String, String> proxyProperties;
final String sslTrustStorePath;
final String sslTrustStorePassword;
final String sslTrustStoreType;
final String sslTrustStoreProvider;
final HttpTransportFactory httpTransportFactory;

private TelemetryTransportConfig(Builder builder) {
Expand All @@ -115,6 +124,10 @@ private TelemetryTransportConfig(Builder builder) {
builder.overrideProperties != null ? builder.overrideProperties : Collections.emptyMap();
this.proxyProperties =
builder.proxyProperties != null ? builder.proxyProperties : Collections.emptyMap();
this.sslTrustStorePath = builder.sslTrustStorePath;
this.sslTrustStorePassword = builder.sslTrustStorePassword;
this.sslTrustStoreType = builder.sslTrustStoreType;
this.sslTrustStoreProvider = builder.sslTrustStoreProvider;
this.httpTransportFactory = builder.httpTransportFactory;
}

Expand All @@ -126,6 +139,10 @@ static final class Builder {
private String universeDomain;
private Map<String, String> overrideProperties;
private Map<String, String> proxyProperties;
private String sslTrustStorePath;
private String sslTrustStorePassword;
private String sslTrustStoreType;
private String sslTrustStoreProvider;
private HttpTransportFactory httpTransportFactory;

Builder setUniverseDomain(String universeDomain) {
Expand All @@ -143,6 +160,14 @@ Builder setProxyProperties(Map<String, String> proxyProperties) {
return this;
}

Builder setSslTrustStore(String path, String password, String type, String provider) {
this.sslTrustStorePath = path;
this.sslTrustStorePassword = password;
this.sslTrustStoreType = type;
this.sslTrustStoreProvider = provider;
return this;
}

Builder setHttpTransportFactory(HttpTransportFactory httpTransportFactory) {
this.httpTransportFactory = httpTransportFactory;
return this;
Expand Down Expand Up @@ -528,6 +553,18 @@ static OpenTelemetry getOpenTelemetry(
return OpenTelemetry.noop();
}

boolean hasProxy = BigQueryJdbcProxyUtility.hasProxy(transportConfig.proxyProperties);
boolean hasSsl = transportConfig.sslTrustStorePath != null;

if (enableGcpTraceExporter
&& hasProxy
&& BigQueryJdbcProxyUtility.hasProxyAuth(transportConfig.proxyProperties)) {
throw new BigQueryJdbcRuntimeException(
"Built-in GCP OpenTelemetry trace exporter does not support authenticated HTTP proxies"
+ " (ProxyUid/ProxyPwd). Disable enableGcpTraceExporter or provide a custom"
+ " OpenTelemetry instance via DataSource.setCustomOpenTelemetry(...).");
}

String validatedUniverse =
enableGcpTraceExporter
? validateNormalizedUniverseDomain(transportConfig.universeDomain)
Expand Down Expand Up @@ -556,6 +593,7 @@ static OpenTelemetry getOpenTelemetry(

final Credentials resolvedExporterCredentials;
final ProxyOptions proxyOptions;
final BigQueryJdbcProxyUtility.SslConfig sslConfig;
if (enableGcpTraceExporter) {
resolvedExporterCredentials =
resolveTelemetryCredentials(
Expand All @@ -564,10 +602,19 @@ static OpenTelemetry getOpenTelemetry(
transportConfig,
targetUniverseDomain,
CLOUD_PLATFORM_SCOPE);
proxyOptions = createProxyOptions(transportConfig.proxyProperties);
proxyOptions = hasProxy ? createProxyOptions(transportConfig.proxyProperties) : null;
sslConfig =
hasSsl
? BigQueryJdbcProxyUtility.createSslConfig(
transportConfig.sslTrustStorePath,
transportConfig.sslTrustStorePassword,
transportConfig.sslTrustStoreType,
transportConfig.sslTrustStoreProvider)
: null;
} else {
resolvedExporterCredentials = null;
proxyOptions = null;
sslConfig = null;
}

Map<String, String> props = new HashMap<>();
Expand Down Expand Up @@ -595,7 +642,7 @@ static OpenTelemetry getOpenTelemetry(
props.put(OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT, DEFAULT_ATTRIBUTE_LENGTH_LIMIT);
props.put(OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, DEFAULT_ATTRIBUTE_LENGTH_LIMIT);

AutoConfiguredOpenTelemetrySdk autoConfigured =
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder =
AutoConfiguredOpenTelemetrySdk.builder()
.addPropertiesSupplier(() -> props)
.addResourceCustomizer(
Expand All @@ -612,10 +659,20 @@ static OpenTelemetry getOpenTelemetry(
spanExporter,
gcpTelemetryProjectId,
resolvedExporterCredentials,
proxyOptions))
.build();
proxyOptions,
sslConfig));

if (enableGcpTraceExporter && hasProxy) {
sdkBuilder.addPropertiesCustomizer(
configProperties -> {
Map<String, String> protocolOverrides = new HashMap<>();
protocolOverrides.put(OTEL_EXPORTER_OTLP_PROTOCOL, PROTOCOL_HTTP_PROTOBUF);
protocolOverrides.put(OTEL_EXPORTER_OTLP_TRACES_PROTOCOL, PROTOCOL_HTTP_PROTOBUF);
return protocolOverrides;
});
}

OpenTelemetrySdk newSdk = autoConfigured.getOpenTelemetrySdk();
OpenTelemetrySdk newSdk = sdkBuilder.build().getOpenTelemetrySdk();

return sdkCache.compute(
key,
Expand All @@ -635,7 +692,8 @@ static SpanExporter customizeSpanExporter(
SpanExporter spanExporter,
String gcpTelemetryProjectId,
Credentials credentials,
ProxyOptions proxyOptions) {
ProxyOptions proxyOptions,
BigQueryJdbcProxyUtility.SslConfig sslConfig) {
Supplier<Map<String, String>> headersSupplier =
credentials != null ? () -> getAuthHeaders(credentials, gcpTelemetryProjectId) : null;

Expand All @@ -647,6 +705,9 @@ static SpanExporter customizeSpanExporter(
if (proxyOptions != null) {
httpBuilder.setProxy(proxyOptions);
}
if (sslConfig != null) {
httpBuilder.setSslContext(sslConfig.sslContext, sslConfig.trustManager);
}
SpanExporter customized = httpBuilder.build();
spanExporter.shutdown();
return customized;
Expand All @@ -657,6 +718,9 @@ static SpanExporter customizeSpanExporter(
if (headersSupplier != null) {
grpcBuilder.setHeaders(headersSupplier);
}
if (sslConfig != null) {
grpcBuilder.setSslContext(sslConfig.sslContext, sslConfig.trustManager);
}
SpanExporter customized = grpcBuilder.build();
spanExporter.shutdown();
return customized;
Expand Down Expand Up @@ -720,23 +784,9 @@ public static <T> T withTracing(
}

private static ProxyOptions createProxyOptions(Map<String, String> proxyProperties) {
if (proxyProperties == null) {
return null;
}

final String host = proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
final String portStr = proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME);
if (host == null || host.isEmpty() || portStr == null || portStr.isEmpty()) {
return null;
}

int port;
try {
port = Integer.parseInt(portStr);
} catch (NumberFormatException e) {
throw new BigQueryJdbcRuntimeException("Invalid proxy port number: " + portStr, e);
}

String host = proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
int port =
Integer.parseInt(proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME));
return ProxyOptions.create(InetSocketAddress.createUnresolved(host, port));
}
Comment on lines 786 to 791

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

The previous implementation of createProxyOptions safely handled missing or invalid proxy host/port configurations by returning null or throwing a descriptive BigQueryJdbcRuntimeException with a clear error message. The new implementation removes these guards and the try-catch block, which can lead to raw NumberFormatExceptions or NullPointerExceptions if the proxy properties are malformed or incomplete.

We should restore the defensive checks and wrap any NumberFormatException in a BigQueryJdbcRuntimeException to maintain robust error handling and clear diagnostics.

  private static ProxyOptions createProxyOptions(Map<String, String> proxyProperties) {
    String host = proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
    String portStr = proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME);
    if (host == null || host.isEmpty() || portStr == null || portStr.isEmpty()) {
      throw new BigQueryJdbcRuntimeException("Proxy host and port must be specified");
    }
    int port;
    try {
      port = Integer.parseInt(portStr);
    } catch (NumberFormatException e) {
      throw new BigQueryJdbcRuntimeException("Invalid proxy port number: " + portStr, e);
    }
    return ProxyOptions.create(InetSocketAddress.createUnresolved(host, port));
  }

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryPayload;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryTestUtils;
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
import com.google.cloud.bigquery.jdbc.utils.URIBuilder;
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
import com.google.cloud.bigquery.storage.v1.BigQueryReadSettings;
Expand Down Expand Up @@ -2089,14 +2090,21 @@ public void testConcurrentGetWarningsAndReset_neverThrowsIndexOutOfBounds() thro
}

@Test
public void testConnectionPropagatesUniverseAndOverridesToTelemetryClient() throws Exception {
public void testConnectionPropagatesUniverseProxyAndTrustStoreToTelemetryClient()
throws Exception {
String trustStoreUrlPath = TestUtilities.getTestResourcePath("test_truststore_withpass.jks");
String url =
getUriOAuthServiceAccount()
.append("ProjectId", "telemetry-project")
.append("universeDomain", CUSTOM_UNIVERSE)
.append("EndpointOverrides", "OAUTH2=https://oauth2.custom.internal/token")
.append("ProxyHost", "127.0.0.1")
.append("ProxyPort", "3128")
.append("ProxyUid", "proxy-user")
.append("ProxyPwd", "proxy-pass")
.append("SSLTrustStore", trustStoreUrlPath)
.append("SSLTrustStorePwd", "testpassword")
.append("SSLTrustStoreType", "JKS")
.append("enableGcpTraceExporter", 1)
.append("EnableDiagnosticTelemetry", 0)
.toString();
Expand Down Expand Up @@ -2140,6 +2148,15 @@ public void testConnectionPropagatesUniverseAndOverridesToTelemetryClient() thro
captured.proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME));
assertEquals(
"3128", captured.proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME));
assertEquals(
"proxy-user",
captured.proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME));
assertEquals(
"proxy-pass",
captured.proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME));
assertEquals(trustStoreUrlPath, captured.sslTrustStorePath);
assertEquals("testpassword", captured.sslTrustStorePassword);
assertEquals("JKS", captured.sslTrustStoreType);
assertNotNull(captured.httpTransportFactory);
}
}
Expand Down
Loading
Loading