-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBigQueryJdbcOpenTelemetry.java
More file actions
409 lines (359 loc) · 15.3 KB
/
BigQueryJdbcOpenTelemetry.java
File metadata and controls
409 lines (359 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright 2026 Google LLC
*
* 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.auth.Credentials;
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.LoggingOptions;
import com.google.common.hash.Hashing;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Handler;
import java.util.logging.Logger;
public class BigQueryJdbcOpenTelemetry {
static final String INSTRUMENTATION_SCOPE_NAME = "com.google.cloud.bigquery.jdbc";
static final String BIGQUERY_NAMESPACE = "com.google.cloud.bigquery";
public static final String CONNECTION_ID_BAGGAGE_KEY = "jdbc.connection_id";
public static final String DB_SYSTEM_KEY = "db.system";
public static final String DB_SYSTEM_VALUE = "bigquery";
public static final String DB_CONNECTION_ID_KEY = "db.connection_id";
public static final String DB_APPLICATION_KEY = "db.application";
public static final String DEFAULT_APPLICATION_NAME = "Google-BigQuery-JDBC-Driver";
public static final String DB_STATEMENT_KEY = "db.statement";
public static final String DB_STATEMENT_COUNT_KEY = "db.statement.count";
public static final String DB_BATCH_STATEMENTS_KEY = "db.batch.statements";
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_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";
private static final String OTLP_ENDPOINT_VALUE = "https://telemetry.googleapis.com:443";
private static final String EXPORTER_NONE = "none";
private static final String EXPORTER_OTLP = "otlp";
private static final String OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT =
"otel.span.attribute.value.length.limit";
private static final String OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT =
"otel.attribute.value.length.limit";
private static final String DEFAULT_ATTRIBUTE_LENGTH_LIMIT = "32768";
private static final BigQueryJdbcCustomLogger LOG =
new BigQueryJdbcCustomLogger("BigQueryJdbcOpenTelemetry");
private static final class SdkCacheKey {
private final String projectId;
private final String credentialsHashOrPath;
private final boolean enableTrace;
SdkCacheKey(String projectId, String credentialsHashOrPath, boolean enableTrace) {
this.projectId = projectId;
this.credentialsHashOrPath = credentialsHashOrPath;
this.enableTrace = enableTrace;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SdkCacheKey that = (SdkCacheKey) o;
return enableTrace == that.enableTrace
&& Objects.equals(projectId, that.projectId)
&& Objects.equals(credentialsHashOrPath, that.credentialsHashOrPath);
}
@Override
public int hashCode() {
return Objects.hash(projectId, credentialsHashOrPath, enableTrace);
}
}
private static final ConcurrentHashMap<SdkCacheKey, OpenTelemetrySdk> sdkCache =
new ConcurrentHashMap<>();
static class TelemetryConfig {
final OpenTelemetry openTelemetry;
final Logging loggingClient;
final boolean useDirectGcpLogging;
TelemetryConfig(
OpenTelemetry openTelemetry, Logging loggingClient, Boolean useDirectGcpLogging) {
this.openTelemetry = openTelemetry;
this.loggingClient = loggingClient;
this.useDirectGcpLogging = useDirectGcpLogging != null ? useDirectGcpLogging : false;
}
}
private static final ConcurrentHashMap<String, TelemetryConfig> connectionConfigs =
new ConcurrentHashMap<>();
private BigQueryJdbcOpenTelemetry() {}
static {
ensureGlobalHandlerAttached();
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
for (OpenTelemetrySdk sdk : sdkCache.values()) {
try {
sdk.close();
} catch (Exception e) {
// Ignore failures during shutdown to ensure all SDKs are attempted to be
// closed. Logging is avoided here because the logging system might have
// already been shut down by the JVM.
}
}
}));
}
public static synchronized void ensureGlobalHandlerAttached() {
Logger logger = Logger.getLogger(BIGQUERY_NAMESPACE);
boolean present = false;
for (Handler h : logger.getHandlers()) {
if (h instanceof OpenTelemetryJulHandler) {
present = true;
break;
}
}
if (!present) {
logger.addHandler(new OpenTelemetryJulHandler());
}
}
public static void registerConnection(
String connectionId,
OpenTelemetry openTelemetry,
Logging loggingClient,
Boolean useDirectGcpLogging) {
connectionConfigs.put(
connectionId, new TelemetryConfig(openTelemetry, loggingClient, useDirectGcpLogging));
}
public static void unregisterConnection(String connectionId) {
TelemetryConfig config = connectionConfigs.remove(connectionId);
if (config != null && config.loggingClient != null) {
try {
config.loggingClient.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) {
if (!enableGcpLogExporter || customOpenTelemetry != null) {
return null;
}
try {
Credentials credentials;
if (effectiveCredentials != null) {
credentials = resolveCredentialsFromString(effectiveCredentials);
} else {
credentials = fallbackCredentials;
}
LoggingOptions.Builder loggingOptionsBuilder =
LoggingOptions.newBuilder().setProjectId(effectiveProjectId);
if (credentials != null) {
loggingOptionsBuilder.setCredentials(credentials);
}
return loggingOptionsBuilder.build().getService();
} catch (Exception e) {
throw new BigQueryJdbcRuntimeException("Failed to initialize Logging client", e);
}
}
private static Credentials resolveCredentialsFromString(String credsString) {
Map<String, String> authProperties = new java.util.HashMap<>();
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_TYPE_PROPERTY_NAME, "0"); // Service Account
byte[] credsBytes = credsString.getBytes(StandardCharsets.UTF_8);
if (BigQueryJdbcOAuthUtility.isJson(credsBytes)) {
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, credsString);
} else {
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PATH_PROPERTY_NAME, credsString);
}
return BigQueryJdbcOAuthUtility.getCredentials(
authProperties,
new java.util.HashMap<>(),
false,
BigQueryJdbcOpenTelemetry.class.getName());
}
public static TelemetryConfig getConnectionConfig(String connectionId) {
return connectionConfigs.get(connectionId);
}
public static Collection<TelemetryConfig> getRegisteredConfigs() {
return connectionConfigs.values();
}
private static Map<String, String> getAuthHeaders(Credentials credentials) {
try {
Map<String, List<String>> metadata =
credentials.getRequestMetadata(URI.create(OTLP_ENDPOINT_VALUE));
Map<String, String> headers = new HashMap<>();
metadata.forEach(
(headerKey, headerValues) -> {
if (!headerValues.isEmpty()) {
headers.put(headerKey, headerValues.get(0));
}
});
return headers;
} catch (IOException e) {
// We log the warning and return an empty map, allowing the exporter to fail gracefully
// with a standard OTLP response code (e.g., 401 Unauthorized) handled by OTel.
LOG.warning("Failed to get auth headers: %s", e.getMessage());
return new HashMap<>();
}
}
private static String getCredentialsIdentifier(String credentials) {
if (credentials == null) {
return "";
}
byte[] credsBytes = credentials.getBytes(StandardCharsets.UTF_8);
if (BigQueryJdbcOAuthUtility.isJson(credsBytes)) {
return Hashing.sha256().hashString(credentials, StandardCharsets.UTF_8).toString();
}
return credentials;
}
/**
* Initializes or returns the OpenTelemetry instance based on hybrid logic. Prefer
* customOpenTelemetry if provided; fallback to an auto-configured GCP exporter if requested.
*/
public static OpenTelemetry getOpenTelemetry(
boolean useGlobalOpenTelemetry,
boolean enableGcpTraceExporter,
boolean enableGcpLogExporter,
OpenTelemetry customOpenTelemetry,
String gcpTelemetryCredentials,
String gcpTelemetryProjectId) {
if (customOpenTelemetry != null) {
return customOpenTelemetry;
}
if (useGlobalOpenTelemetry) {
return GlobalOpenTelemetry.get();
}
if (!enableGcpTraceExporter && !enableGcpLogExporter) {
return OpenTelemetry.noop();
}
SdkCacheKey key =
new SdkCacheKey(
gcpTelemetryProjectId,
getCredentialsIdentifier(gcpTelemetryCredentials),
enableGcpTraceExporter);
return sdkCache.computeIfAbsent(
key,
k -> {
Map<String, String> props = new HashMap<>();
if (enableGcpTraceExporter) {
props.put(OTEL_TRACES_EXPORTER, EXPORTER_OTLP);
props.put(OTEL_EXPORTER_OTLP_ENDPOINT, OTLP_ENDPOINT_VALUE);
} else {
props.put(OTEL_TRACES_EXPORTER, EXPORTER_NONE);
}
// Logs are handled directly via GCP logging
props.put(OTEL_LOGS_EXPORTER, EXPORTER_NONE);
// Metrics are deferred to a future phase
props.put(OTEL_METRICS_EXPORTER, EXPORTER_NONE);
if (gcpTelemetryProjectId != null) {
props.put(GOOGLE_CLOUD_PROJECT, gcpTelemetryProjectId);
}
// Set safe, generous default limits on attribute value lengths (32KB) to protect
// customers from GCP Cloud Trace 64KB span ingestion failures when logging massive
// exception stack traces or database schema metadata.
// Respect any existing user configuration overrides.
if (!props.containsKey(OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT)) {
props.put(OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT, DEFAULT_ATTRIBUTE_LENGTH_LIMIT);
}
if (!props.containsKey(OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT)) {
props.put(OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, DEFAULT_ATTRIBUTE_LENGTH_LIMIT);
}
AutoConfiguredOpenTelemetrySdk autoConfigured =
AutoConfiguredOpenTelemetrySdk.builder()
.addPropertiesSupplier(() -> props)
.addSpanExporterCustomizer(
(spanExporter, configProperties) -> {
if (gcpTelemetryCredentials != null) {
Credentials credentials =
resolveCredentialsFromString(gcpTelemetryCredentials);
if (spanExporter instanceof OtlpHttpSpanExporter) {
return ((OtlpHttpSpanExporter) spanExporter)
.toBuilder().setHeaders(() -> getAuthHeaders(credentials)).build();
}
if (spanExporter instanceof OtlpGrpcSpanExporter) {
return ((OtlpGrpcSpanExporter) spanExporter)
.toBuilder().setHeaders(() -> getAuthHeaders(credentials)).build();
}
}
return spanExporter;
})
.build();
OpenTelemetrySdk sdk = autoConfigured.getOpenTelemetrySdk();
return sdk;
});
}
/** Gets a Tracer for the JDBC driver instrumentation scope. */
public static Tracer getTracer(OpenTelemetry openTelemetry) {
return openTelemetry.getTracer(INSTRUMENTATION_SCOPE_NAME);
}
public static <T> T withTracing(
String spanName, BigQueryConnection connection, String sql, Callable<T> operation)
throws SQLException {
Tracer tracer = connection.getTracer();
Span span = tracer.spanBuilder(spanName).setSpanKind(SpanKind.CLIENT).startSpan();
span.setAttribute(DB_SYSTEM_KEY, DB_SYSTEM_VALUE);
span.setAttribute(DB_CONNECTION_ID_KEY, connection.getConnectionId());
String appName = connection.getPartnerToken();
if (appName == null || appName.isEmpty()) {
appName = DEFAULT_APPLICATION_NAME;
}
span.setAttribute(DB_APPLICATION_KEY, appName);
if (sql != null) {
span.setAttribute(DB_STATEMENT_KEY, sql);
}
Baggage updatedBaggage =
Baggage.fromContext(Context.current()).toBuilder()
.put(CONNECTION_ID_BAGGAGE_KEY, connection.getConnectionId())
.build();
// Create full context with new span and updated baggage
Context fullContext = Context.current().with(span).with(updatedBaggage);
try (Scope scope = fullContext.makeCurrent()) {
return operation.call();
} catch (Exception ex) {
span.recordException(ex);
span.setStatus(StatusCode.ERROR, ex.getMessage());
if (ex instanceof SQLException) {
throw (SQLException) ex;
}
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
if (ex instanceof InterruptedException) {
Thread.currentThread().interrupt();
throw new BigQueryJdbcRuntimeException("Operation interrupted", ex);
}
throw new BigQueryJdbcRuntimeException(ex);
} finally {
span.end();
}
}
}