1616
1717package com .google .cloud .bigquery .jdbc .it ;
1818
19- import static org .junit .Assert .assertEquals ;
20- import static org .junit .Assert .assertFalse ;
21- import static org .junit .Assert .assertNotNull ;
22- import static org .junit .Assert .assertTrue ;
19+ import static org .junit .jupiter .api .Assertions .assertEquals ;
20+ import static org .junit .jupiter .api .Assertions .assertFalse ;
21+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
22+ import static org .junit .jupiter .api .Assertions .assertTrue ;
23+ import static org .junit .jupiter .api .Assumptions .assumeTrue ;
2324
2425import com .google .api .gax .paging .Page ;
2526import com .google .cloud .ServiceOptions ;
3839import java .util .ArrayList ;
3940import java .util .List ;
4041import java .util .Properties ;
41- import org .junit .Test ;
42+ import org .junit .jupiter . api . Test ;
4243
4344public class ITOpenTelemetryTest {
4445
45- private static final String projectId = ServiceOptions .getDefaultProjectId ();
46- private static final String connectionUrl =
46+ private static final String PROJECT_ID = ServiceOptions .getDefaultProjectId ();
47+ private static final String CONNECTION_URL =
4748 String .format (
4849 "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=%s;OAuthType=3;Timeout=3600;" ,
49- projectId );
50+ PROJECT_ID );
5051
5152 @ Test
5253 public void testExecute_withOpenTelemetryGcpExporter () throws Exception {
54+ assumeTrue (
55+ PROJECT_ID != null && !PROJECT_ID .trim ().isEmpty (),
56+ "Skipping OTel E2E tests because no default Project ID is configured." );
5357
5458 // Step 1: Connect with GCP Exporters enabled
5559 Properties props = new Properties ();
5660 props .setProperty ("enableGcpTraceExporter" , "true" );
5761 props .setProperty ("enableGcpLogExporter" , "true" );
5862 props .setProperty ("LogLevel" , "3" ); // Triggers FINE log generation
59- props .setProperty ("gcpTelemetryProjectId" , projectId );
63+ props .setProperty ("gcpTelemetryProjectId" , PROJECT_ID );
6064 props .setProperty ("EnableHighThroughputAPI" , "0" );
6165 props .setProperty ("MaxResults" , "50" ); // Forces small page size (50) to trigger pagination
6266
6367 String connectionUuid = null ;
6468
65- try (Connection connection = DriverManager .getConnection (connectionUrl , props );
69+ try (Connection connection = DriverManager .getConnection (CONNECTION_URL , props );
6670 Statement statement = connection .createStatement ()) {
6771
6872 // Retrieve the Connection UUID programmatically
6973 BigQueryConnection bqConnection = connection .unwrap (BigQueryConnection .class );
7074 connectionUuid = bqConnection .getConnectionId ();
71- assertNotNull ("Connection UUID should be generated" , connectionUuid );
75+ assertNotNull (connectionUuid , "Connection UUID should be generated" );
7276
7377 // Execute an in-memory array query (scans 0 bytes, extremely fast) and force pagination
7478 String paginationQuery = "SELECT * FROM UNNEST(GENERATE_ARRAY(1, 1000)) AS id;" ;
@@ -85,23 +89,23 @@ public void testExecute_withOpenTelemetryGcpExporter() throws Exception {
8589 String hexSpanId = null ;
8690
8791 try (Logging logging =
88- LoggingOptions .newBuilder ().setProjectId (projectId ).build ().getService ()) {
92+ LoggingOptions .newBuilder ().setProjectId (PROJECT_ID ).build ().getService ()) {
8993 String filter =
9094 "logName:\" projects/"
91- + projectId
95+ + PROJECT_ID
9296 + "/logs/com.google.cloud.bigquery\" AND labels.\" jdbc.connection_id\" =\" "
9397 + connectionUuid
9498 + "\" " ;
9599
96100 List <LogEntry > entries = fetchLogsWithRetry (logging , filter );
97- assertFalse ("Telemetry logs should be exported to GCP" , entries . isEmpty () );
101+ assertFalse (entries . isEmpty (), "Telemetry logs should be exported to GCP" );
98102
99103 LogEntry sampleEntry = entries .get (0 );
100104 traceId = sampleEntry .getTrace ();
101105 hexSpanId = sampleEntry .getSpanId ();
102106
103- assertNotNull ("Log entry must contain TraceId" , traceId );
104- assertNotNull ("Log entry must contain SpanId" , hexSpanId );
107+ assertNotNull (traceId , "Log entry must contain TraceId" );
108+ assertNotNull (hexSpanId , "Log entry must contain SpanId" );
105109
106110 // Verify Connection UUID label correlation on all entries
107111 for (LogEntry entry : entries ) {
@@ -116,8 +120,8 @@ public void testExecute_withOpenTelemetryGcpExporter() throws Exception {
116120 }
117121
118122 try (TraceServiceClient traceClient = TraceServiceClient .create ()) {
119- Trace trace = fetchTraceWithRetry (traceClient , projectId , hexTraceId );
120- assertNotNull ("Trace must be found in Cloud Trace API: " + hexTraceId , trace );
123+ Trace trace = fetchTraceWithRetry (traceClient , PROJECT_ID , hexTraceId );
124+ assertNotNull (trace , "Trace must be found in Cloud Trace API: " + hexTraceId );
121125
122126 boolean foundParentExecuteQuery = false ;
123127 boolean foundChildSdkSpans = false ;
@@ -133,8 +137,8 @@ public void testExecute_withOpenTelemetryGcpExporter() throws Exception {
133137 }
134138
135139 assertTrue (
136- "Traces must contain JDBC parent span 'BigQueryStatement.executeQuery'" ,
137- foundParentExecuteQuery );
140+ foundParentExecuteQuery ,
141+ "Traces must contain JDBC parent span 'BigQueryStatement.executeQuery'" );
138142
139143 // Verify that we captured child spans or linked pagination spans
140144 for (TraceSpan span : trace .getSpansList ()) {
@@ -146,32 +150,35 @@ public void testExecute_withOpenTelemetryGcpExporter() throws Exception {
146150 }
147151 }
148152
149- assertTrue ("OTel pagination must generate pagination spans" , foundPaginationSpans );
153+ assertTrue (foundPaginationSpans , "OTel pagination must generate pagination spans" );
150154 assertTrue (
151- "OTel context must propagate parent to downstream pagination child spans" ,
152- foundChildSdkSpans );
155+ foundChildSdkSpans ,
156+ "OTel context must propagate parent to downstream pagination child spans" );
153157 }
154158 }
155159
156160 @ Test
157161 public void testExecute_withErrorCorrelation () throws Exception {
162+ assumeTrue (
163+ PROJECT_ID != null && !PROJECT_ID .trim ().isEmpty (),
164+ "Skipping OTel E2E tests because no default Project ID is configured." );
158165
159166 // Step 1: Connect with GCP Exporters enabled
160167 Properties props = new Properties ();
161168 props .setProperty ("enableGcpTraceExporter" , "true" );
162169 props .setProperty ("enableGcpLogExporter" , "true" );
163170 props .setProperty ("LogLevel" , "3" ); // Triggers FINE log generation
164- props .setProperty ("gcpTelemetryProjectId" , projectId );
171+ props .setProperty ("gcpTelemetryProjectId" , PROJECT_ID );
165172
166173 String connectionUuid = null ;
167174
168- try (Connection connection = DriverManager .getConnection (connectionUrl , props );
175+ try (Connection connection = DriverManager .getConnection (CONNECTION_URL , props );
169176 Statement statement = connection .createStatement ()) {
170177
171178 // Retrieve the Connection UUID programmatically
172179 BigQueryConnection bqConnection = connection .unwrap (BigQueryConnection .class );
173180 connectionUuid = bqConnection .getConnectionId ();
174- assertNotNull ("Connection UUID should be generated" , connectionUuid );
181+ assertNotNull (connectionUuid , "Connection UUID should be generated" );
175182
176183 // Execute a query designed to fail due to non-existent table
177184 boolean caughtException = false ;
@@ -180,31 +187,31 @@ public void testExecute_withErrorCorrelation() throws Exception {
180187 } catch (SQLException e ) {
181188 caughtException = true ;
182189 }
183- assertTrue ("Expected SQLException to be thrown" , caughtException );
190+ assertTrue (caughtException , "Expected SQLException to be thrown" );
184191 }
185192
186193 // Step 2: Retrieve logs from Cloud Logging and assert error logs
187194 String traceId = null ;
188195 String hexSpanId = null ;
189196
190197 try (Logging logging =
191- LoggingOptions .newBuilder ().setProjectId (projectId ).build ().getService ()) {
198+ LoggingOptions .newBuilder ().setProjectId (PROJECT_ID ).build ().getService ()) {
192199 String filter =
193200 "logName:\" projects/"
194- + projectId
201+ + PROJECT_ID
195202 + "/logs/com.google.cloud.bigquery\" AND labels.\" jdbc.connection_id\" =\" "
196203 + connectionUuid
197204 + "\" " ;
198205
199206 List <LogEntry > entries = fetchLogsWithRetry (logging , filter );
200- assertFalse ("Telemetry logs should be exported to GCP" , entries . isEmpty () );
207+ assertFalse (entries . isEmpty (), "Telemetry logs should be exported to GCP" );
201208
202209 LogEntry sampleEntry = entries .get (0 );
203210 traceId = sampleEntry .getTrace ();
204211 hexSpanId = sampleEntry .getSpanId ();
205212
206- assertNotNull ("Log entry must contain TraceId" , traceId );
207- assertNotNull ("Log entry must contain SpanId" , hexSpanId );
213+ assertNotNull (traceId , "Log entry must contain TraceId" );
214+ assertNotNull (hexSpanId , "Log entry must contain SpanId" );
208215 }
209216
210217 // Step 3: Query Cloud Trace using TraceId and assert span status is ERROR
@@ -214,8 +221,8 @@ public void testExecute_withErrorCorrelation() throws Exception {
214221 }
215222
216223 try (TraceServiceClient traceClient = TraceServiceClient .create ()) {
217- Trace trace = fetchTraceWithRetry (traceClient , projectId , hexTraceId );
218- assertNotNull ("Trace must be found in Cloud Trace API: " + hexTraceId , trace );
224+ Trace trace = fetchTraceWithRetry (traceClient , PROJECT_ID , hexTraceId );
225+ assertNotNull (trace , "Trace must be found in Cloud Trace API: " + hexTraceId );
219226
220227 boolean foundParentExecuteQuery = false ;
221228
@@ -227,8 +234,8 @@ public void testExecute_withErrorCorrelation() throws Exception {
227234 }
228235
229236 assertTrue (
230- "Traces must contain JDBC parent span 'BigQueryStatement.executeQuery'" ,
231- foundParentExecuteQuery );
237+ foundParentExecuteQuery ,
238+ "Traces must contain JDBC parent span 'BigQueryStatement.executeQuery'" );
232239 }
233240 }
234241
0 commit comments