Repository navigation
[java-bigquerystorage] Accept an OpenTelemetry instance per stream writer instead of the cached GlobalOpenTelemetry #14765
Description
Is your feature request related to a problem? Please describe.
The Storage Write client's OpenTelemetry metrics (StreamWriter.Builder.setEnableOpenTelemetry(true)) can only be sent to the process-wide GlobalOpenTelemetry.
TelemetryMetrics takes its MeterProvider from Singletons.getOpenTelemetry(), which caches GlobalOpenTelemetry.get() in a static field on first use and has no setter:
Lines 30 to 36 in 165f02e
| public static OpenTelemetry getOpenTelemetry() { | |
| if (openTelemetry == null) { | |
| openTelemetry = GlobalOpenTelemetry.get(); | |
| log.info("BigQueryStorage initialized Open Telemetry"); | |
| } | |
| return openTelemetry; | |
| } |
Lines 136 to 139 in 165f02e
| MeterProvider meterProvider = Singletons.getOpenTelemetry().getMeterProvider(); | |
| writeMeter = | |
| meterProvider | |
| .meterBuilder("com.google.cloud.bigquery.storage.v1.write") |
This makes the metrics hard to use from a framework that hosts many writers in one JVM.
We maintain an Apache Flink connector for BigQuery (flink-gcp/flink-connector-gcp) and want to forward each writer's metrics into Flink's own metric system, which then exports them to whatever reporter the deployment uses (Prometheus, Datadog, OTLP).
With only the global instance:
- several jobs or subtasks share one TaskManager JVM, so their writers cannot report to separate meter providers;
- a library cannot call
GlobalOpenTelemetry.set(...)without taking that global over from the application; - when the client is shaded into a connector jar,
io.opentelemetryis relocated, so the relocated global is not one the application or a Java agent can set at all.
Describe the solution you'd like
An optional per-writer OpenTelemetry (or MeterProvider) on the writer builders, for example StreamWriter.Builder.setOpenTelemetry(OpenTelemetry), with the same setter on JsonStreamWriter.Builder and SchemaAwareStreamWriter.Builder.
When it is unset, the current GlobalOpenTelemetry behaviour would stay as it is.
Other clients in this repository already offer this:
- Spanner:
SpannerOptions.Builder.setClientMetricsProvider(MetricsProvider)(feat(spanner): support user-provided OpenTelemetry for client metrics export #13741). - Bigtable:
BigtableDataSettings.Builder.setMetricsProvider(MetricsProvider), which accepts aCustomOpenTelemetryMetricsProvider.
Describe alternatives you've considered
GlobalOpenTelemetry.set(...)once per JVM: it cannot separate the writers of different jobs, and it does not reach a relocated copy of the client.- Reading the meters reflectively, or replacing
Singletons: neither is a supported surface.
Additional context
- Libraries-Bom: 26.90.0
- google-cloud-bigquerystorage 3.35.0
- The same code is on
mainat 165f02e.
Activity
laughingman7743 commentedon Oct 28, 2026
One addition, found after filing: a per-writer setter as proposed above would not reach a writer built with
setEnableConnectionPool(true).In pool mode the
ConnectionWorkerPoolis shared per (location, credentials) across everyStreamWriterin the JVM, and it is built from the builder of whichever writer arrives first:google-cloud-java/java-bigquerystorage/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java
Lines 346 to 362 in f01245a
Its connections are created with that pool-wide
enableOpenTelemetryflag, and eachConnectionWorkerowns oneTelemetryMetricskeyed by the worker's ownwriter_id, not by theStreamWriterthat issued a request:google-cloud-java/java-bigquerystorage/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/ConnectionWorkerPool.java
Lines 408 to 424 in f01245a
google-cloud-java/java-bigquerystorage/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/ConnectionWorker.java
Lines 677 to 678 in f01245a
So if the instance were captured the way
enableOpenTelemetryis today, the first writer'sOpenTelemetrywould receive the measurements of every writer sharing the pool, and the recording side could not tell them apart.For our connector the default-stream path uses the pool, so only its buffered-stream path (one connection per writer) would benefit from the setter alone.
For the pool case, it would help if measurements of a request (
append_requests_acked,append_rows_acked,append_request_bytes_acked,network_response_latency) were recorded against theOpenTelemetryof theStreamWriterthat appended it.Connection-level instruments (
connection_start_count,connection_end_count, the two gauges) belong to the pool, so a pool-level provider, or documenting that they go to the first writer's instance, would be enough there.The line references above are on
mainat f01245a.