Skip to content

Commit 6405ccd

Browse files
committed
test(bqjdbc): add e2e otel test and code refinements
1 parent 27ed8de commit 6405ccd

6 files changed

Lines changed: 343 additions & 2 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@
377377
<artifactId>opentelemetry-sdk-testing</artifactId>
378378
<scope>test</scope>
379379
</dependency>
380+
<dependency>
381+
<groupId>com.google.cloud</groupId>
382+
<artifactId>google-cloud-trace</artifactId>
383+
<version>2.92.0</version>
384+
<scope>test</scope>
385+
</dependency>
380386
</dependencies>
381387

382388
<profiles>

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ String getConnectionUrl() {
363363
return connectionUrl;
364364
}
365365

366-
String getConnectionId() {
366+
public String getConnectionId() {
367367
return this.connectionId;
368368
}
369369

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOpenTelemetry.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public class BigQueryJdbcOpenTelemetry {
6565
private static final String OTLP_ENDPOINT_VALUE = "https://telemetry.googleapis.com:443";
6666
private static final String EXPORTER_NONE = "none";
6767
private static final String EXPORTER_OTLP = "otlp";
68+
private static final String OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT =
69+
"otel.span.attribute.value.length.limit";
70+
private static final String OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT =
71+
"otel.attribute.value.length.limit";
72+
private static final String DEFAULT_ATTRIBUTE_LENGTH_LIMIT = "61440";
6873
private static final BigQueryJdbcCustomLogger LOG =
6974
new BigQueryJdbcCustomLogger("BigQueryJdbcOpenTelemetry");
7075

@@ -290,6 +295,17 @@ public static OpenTelemetry getOpenTelemetry(
290295
props.put(GOOGLE_CLOUD_PROJECT, gcpTelemetryProjectId);
291296
}
292297

298+
// Set safe, generous default limits on attribute value lengths (60KB) to protect
299+
// customers from GCP Cloud Trace 64KB span ingestion failures when logging massive
300+
// exception stack traces or database schema metadata.
301+
// Respect any existing user configuration overrides.
302+
if (!props.containsKey(OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT)) {
303+
props.put(OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT, DEFAULT_ATTRIBUTE_LENGTH_LIMIT);
304+
}
305+
if (!props.containsKey(OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT)) {
306+
props.put(OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, DEFAULT_ATTRIBUTE_LENGTH_LIMIT);
307+
}
308+
293309
AutoConfiguredOpenTelemetrySdk autoConfigured =
294310
AutoConfiguredOpenTelemetrySdk.builder().addPropertiesSupplier(() -> props).build();
295311

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
public class OpenTelemetryJulHandler extends Handler {
4343
private static final Pattern UNSAFE_LOG_CHARACTERS = Pattern.compile("[^a-zA-Z0-9./_-]");
4444

45-
public OpenTelemetryJulHandler() {}
45+
public OpenTelemetryJulHandler() {
46+
setLevel(Level.ALL);
47+
}
4648

4749
@Override
4850
public void publish(LogRecord record) {

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryConnectionTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,26 @@
2626
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
2727
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
2828
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
29+
import io.opentelemetry.api.trace.Span;
30+
import io.opentelemetry.api.trace.Tracer;
31+
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
32+
import io.opentelemetry.sdk.trace.data.SpanData;
2933
import java.io.IOException;
3034
import java.io.InputStream;
3135
import java.sql.SQLException;
36+
import java.util.List;
3237
import java.util.Properties;
3338
import org.junit.jupiter.api.BeforeEach;
3439
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.extension.RegisterExtension;
3541
import org.junit.jupiter.params.ParameterizedTest;
3642
import org.junit.jupiter.params.provider.CsvSource;
3743

3844
public class BigQueryConnectionTest {
3945

46+
@RegisterExtension
47+
static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();
48+
4049
private static final String DEFAULT_VERSION = "0.0.0";
4150
private static final String DEFAULT_JDBC_TOKEN_VALUE = "Google-BigQuery-JDBC-Driver";
4251
private static final String BASE_URL =
@@ -456,4 +465,25 @@ public void testIsReadOnlyTokenProvided(String readonlyProp, boolean expectedIsR
456465
assertEquals(expectedIsReadOnly, connection.isReadOnlyTokenUsed());
457466
}
458467
}
468+
469+
@Test
470+
public void testConnect_withCustomOpenTelemetry_usesCustomInstance() throws Exception {
471+
DataSource ds = DataSource.fromUrl(BASE_URL);
472+
ds.setCustomOpenTelemetry(otelTesting.getOpenTelemetry());
473+
474+
try (BigQueryConnection connection = new BigQueryConnection(BASE_URL, ds)) {
475+
assertNotNull(connection);
476+
assertFalse(connection.isClosed());
477+
478+
Tracer tracer = connection.getTracer();
479+
assertNotNull(tracer);
480+
481+
Span span = tracer.spanBuilder("custom-otel-span").startSpan();
482+
span.end();
483+
484+
List<SpanData> spans = otelTesting.getSpans();
485+
assertEquals(1, spans.size());
486+
assertEquals("custom-otel-span", spans.get(0).getName());
487+
}
488+
}
459489
}

0 commit comments

Comments
 (0)