-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(jdbc): basic OpenTelemetry tracing integration for BigQuery JDBC Statement #12124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
d00ae37
01734d1
b743313
122c14e
9eeed5f
927599a
cb578ac
d9418fc
fc0174f
7b56606
27016c0
c457034
1204b1f
f1c14f0
855ab94
602b916
59706e6
88751ad
f177da6
c0b915a
ec25dcf
44a371b
483e073
cf8b180
494ad7a
aa5e9a0
437f748
adba483
50b9171
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,8 @@ | |
| import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient; | ||
| import com.google.cloud.bigquery.storage.v1.BigQueryWriteSettings; | ||
| import com.google.cloud.http.HttpTransportOptions; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.sql.CallableStatement; | ||
|
|
@@ -138,6 +140,9 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { | |
| Long connectionPoolSize; | ||
| Long listenerPoolSize; | ||
| String partnerToken; | ||
| boolean enableOpenTelemetry; | ||
| String openTelemetryExporter; | ||
| Tracer tracer = OpenTelemetry.noop().getTracer(""); | ||
|
keshavdandeva marked this conversation as resolved.
Outdated
|
||
|
|
||
| BigQueryConnection(String url) throws IOException { | ||
| this(url, DataSource.fromUrl(url)); | ||
|
|
@@ -242,6 +247,8 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { | |
| this.connectionPoolSize = ds.getConnectionPoolSize(); | ||
| this.listenerPoolSize = ds.getListenerPoolSize(); | ||
| this.partnerToken = ds.getPartnerToken(); | ||
| this.enableOpenTelemetry = ds.getEnableOpenTelemetry(); | ||
| this.openTelemetryExporter = ds.getOpenTelemetryExporter(); | ||
|
|
||
| this.headerProvider = createHeaderProvider(); | ||
| this.bigQuery = getBigQueryConnection(); | ||
|
|
@@ -935,6 +942,15 @@ private BigQuery getBigQueryConnection() { | |
| bigQueryOptions.setTransportOptions(this.httpTransportOptions); | ||
| } | ||
|
|
||
| OpenTelemetry openTelemetry = | ||
| BigQueryJdbcOpenTelemetry.getOpenTelemetry( | ||
| this.enableOpenTelemetry, this.openTelemetryExporter); | ||
| if (this.enableOpenTelemetry) { | ||
| this.tracer = BigQueryJdbcOpenTelemetry.getTracer(openTelemetry); | ||
|
keshavdandeva marked this conversation as resolved.
|
||
| bigQueryOptions.setOpenTelemetryTracer(this.tracer); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the other clients? Do they support OTel?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a write client as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I checked the source for |
||
| BigQueryJdbcOpenTelemetry.attachLoggingBridge(); | ||
| } | ||
|
|
||
| BigQueryOptions options = bigQueryOptions.setHeaderProvider(this.headerProvider).build(); | ||
| options.setDefaultJobCreationMode( | ||
| this.useStatelessQueryMode | ||
|
|
@@ -1083,4 +1099,8 @@ public CallableStatement prepareCall( | |
| } | ||
| return prepareCall(sql); | ||
| } | ||
|
|
||
| public Tracer getTracer() { | ||
| return this.tracer; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
|
keshavdandeva marked this conversation as resolved.
|
||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.bigquery.jdbc; | ||
|
|
||
| import com.google.cloud.opentelemetry.trace.TraceConfiguration; | ||
| import com.google.cloud.opentelemetry.trace.TraceExporter; | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
| import java.io.IOException; | ||
|
|
||
| public class BigQueryJdbcOpenTelemetry { | ||
|
keshavdandeva marked this conversation as resolved.
|
||
|
|
||
| private static final BigQueryJdbcCustomLogger LOG = | ||
| new BigQueryJdbcCustomLogger(BigQueryJdbcOpenTelemetry.class.getName()); | ||
| private static final Object lock = new Object(); | ||
| private static volatile OpenTelemetrySdk autoConfiguredOpenTelemetry; | ||
| private static volatile boolean initialized = false; | ||
| private static final String INSTRUMENTATION_SCOPE_NAME = "com.google.cloud.bigquery.jdbc"; | ||
|
keshavdandeva marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Initializes or returns the OpenTelemetry instance based on hybrid logic. Prefer | ||
| * GlobalOpenTelemetry; fallback to an auto-configured GCP exporter if requested. | ||
| */ | ||
| public static OpenTelemetry getOpenTelemetry(boolean enableOpenTelemetry, String exporterType) { | ||
| if (!enableOpenTelemetry) { | ||
| return OpenTelemetry.noop(); | ||
| } | ||
|
|
||
| OpenTelemetry globalOtel = GlobalOpenTelemetry.get(); | ||
| if ("gcp".equalsIgnoreCase(exporterType)) { | ||
| return getAutoConfiguredOpenTelemetry(); | ||
| } | ||
|
|
||
| return globalOtel; | ||
| } | ||
|
|
||
| /** Gets a Tracer for the JDBC driver instrumentation scope. */ | ||
| public static Tracer getTracer(OpenTelemetry openTelemetry) { | ||
| return openTelemetry.getTracer(INSTRUMENTATION_SCOPE_NAME); | ||
| } | ||
|
|
||
| /** | ||
| * TODO(b/491245568): Attaches the OpenTelemetry logging bridge to the root | ||
| * java.util.logging.Logger. This is currently a no-op due to shading issues with | ||
| * `opentelemetry-appender-jul`. | ||
| */ | ||
| public static void attachLoggingBridge() { | ||
| // No-op for now. | ||
|
keshavdandeva marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private static OpenTelemetry getAutoConfiguredOpenTelemetry() { | ||
| if (!initialized) { | ||
| synchronized (lock) { | ||
| if (!initialized) { | ||
| try { | ||
| autoConfiguredOpenTelemetry = initGcpOpenTelemetry(); | ||
| } catch (Exception e) { | ||
| LOG.warning("Failed to initialize OpenTelemetry with GCP exporter: " + e.getMessage()); | ||
|
keshavdandeva marked this conversation as resolved.
Outdated
|
||
| autoConfiguredOpenTelemetry = null; | ||
| } finally { | ||
| initialized = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return autoConfiguredOpenTelemetry != null ? autoConfiguredOpenTelemetry : OpenTelemetry.noop(); | ||
| } | ||
|
|
||
| private static OpenTelemetrySdk initGcpOpenTelemetry() throws IOException { | ||
| LOG.info("Initializing BigQuery JDBC standalone OpenTelemetry SDK with GCP exporter."); | ||
|
keshavdandeva marked this conversation as resolved.
Outdated
|
||
|
|
||
| SpanExporter traceExporter; | ||
| try { | ||
| TraceConfiguration configuration = TraceConfiguration.builder().build(); | ||
| traceExporter = TraceExporter.createWithConfiguration(configuration); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException( | ||
| "Could not create TraceExporter. Ensure exporter-trace is on classpath.", e); | ||
| } | ||
|
|
||
| SdkTracerProvider tracerProvider = | ||
| SdkTracerProvider.builder() | ||
| .addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build()) | ||
| .build(); | ||
|
|
||
| OpenTelemetrySdk sdk = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build(); | ||
|
|
||
| Runtime.getRuntime() | ||
| .addShutdownHook( | ||
| new Thread( | ||
| () -> { | ||
| try { | ||
| tracerProvider.close(); | ||
| } catch (Exception e) { | ||
| LOG.warning("Error closing tracer provider: " + e.getMessage()); | ||
| } | ||
| })); | ||
|
|
||
| return sdk; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.