6262import org .junit .jupiter .api .BeforeEach ;
6363import org .junit .jupiter .api .Nested ;
6464import org .junit .jupiter .api .Test ;
65+ import org .junit .jupiter .api .parallel .Execution ;
66+ import org .junit .jupiter .api .parallel .ExecutionMode ;
6567
68+ @ Execution (ExecutionMode .SAME_THREAD )
6669public class HttpBigQueryRpcTest {
6770
6871 private static final String PROJECT_ID = "test-project" ;
@@ -73,6 +76,8 @@ public class HttpBigQueryRpcTest {
7376 private static final String JOB_ID = "test-job" ;
7477 private static final String LOCATION = "test-location" ;
7578
79+ private static final Object TEST_LOCK = new Object ();
80+
7681 private InMemorySpanExporter spanExporter ;
7782 private MockLowLevelHttpResponse mockResponse ;
7883 private String lastRequestMethod ;
@@ -196,6 +201,8 @@ class TelemetryEnabled {
196201 @ BeforeEach
197202 public void setUp () {
198203 setUpServer ();
204+ spanExporter .reset (); // Clear spans from previous tests
205+ System .setProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" , "true" );
199206 rpc = createRpc (true );
200207 }
201208
@@ -911,10 +918,7 @@ public void testOtelAttributesFromOptionsGetAddedtoSpan() throws Exception {
911918
912919 @ Test
913920 public void testHttpTracingEnabledAddsAdditionalAttributes () throws Exception {
914- try {
915- System .setProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" , "true" );
916- HttpBigQueryRpc customRpc = createRpc (true );
917-
921+ synchronized (TEST_LOCK ) {
918922 setMockResponse (
919923 "{\" kind\" :\" bigquery#dataset\" ,\" id\" :\" "
920924 + PROJECT_ID
@@ -926,7 +930,7 @@ public void testHttpTracingEnabledAddsAdditionalAttributes() throws Exception {
926930 + DATASET_ID
927931 + "\" }}" );
928932
929- customRpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
933+ rpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
930934
931935 verifyRequest ("GET" , "/projects/" + PROJECT_ID + "/datasets/" + DATASET_ID );
932936 verifySpan (
@@ -936,6 +940,7 @@ public void testHttpTracingEnabledAddsAdditionalAttributes() throws Exception {
936940 Collections .singletonMap ("bq.rpc.response.dataset.id" , PROJECT_ID + ":" + DATASET_ID ));
937941
938942 List <SpanData > spans = spanExporter .getFinishedSpanItems ();
943+ System .out .println ("total number of spans " + spans .size ());
939944 assertThat (spans ).isNotEmpty ();
940945 SpanData rpcSpan =
941946 spans .stream ()
@@ -947,17 +952,89 @@ public void testHttpTracingEnabledAddsAdditionalAttributes() throws Exception {
947952 assertNotNull (rpcSpan );
948953 assertEquals ("http" , rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .RPC_SYSTEM_NAME ));
949954 assertNotNull (rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .GCP_CLIENT_SERVICE ));
950- } finally {
951- System .clearProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" );
952955 }
953956 }
954957
955958 @ Test
956- public void testHttpTracingDisabledDoesNotAddAdditionalAttributes () throws Exception {
957- try {
958- System .setProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" , "false" );
959- HttpBigQueryRpc customRpc = createRpc (true );
959+ public void testHttpTracingEnabled_GenericException_SetsAttributes () throws Exception {
960+ synchronized (TEST_LOCK ) {
961+ assertThrows (
962+ IOException .class ,
963+ () -> {
964+ rpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
965+ });
966+
967+ List <SpanData > spans = spanExporter .getFinishedSpanItems ();
968+ assertThat (spans ).isNotEmpty ();
969+ SpanData rpcSpan =
970+ spans .stream ()
971+ .filter (
972+ span ->
973+ span .getName ().equals ("com.google.cloud.bigquery.BigQueryRpc.getDataset" ))
974+ .findFirst ()
975+ .orElse (null );
976+ assertNotNull (rpcSpan );
977+ assertEquals (
978+ "java.io.IOException" ,
979+ rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .EXCEPTION_TYPE ));
980+ assertEquals (
981+ "CLIENT_UNKNOWN_ERROR" ,
982+ rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .ERROR_TYPE ));
983+ }
984+ }
985+
986+ @ Test
987+ public void testHttpTracingEnabled_JsonResponseException_SetsAttributes () throws Exception {
988+ synchronized (TEST_LOCK ) {
989+ mockResponse .setStatusCode (400 );
990+ mockResponse .setContentType (Json .MEDIA_TYPE );
991+ mockResponse .setContent (
992+ "{\" error\" :{\" code\" :400,\" message\" :\" Invalid request\" ,\" errors\" :[{\" message\" :\" Invalid request\" ,\" domain\" :\" global\" ,\" reason\" :\" invalid\" }]}}" );
993+
994+ assertThrows (
995+ IOException .class ,
996+ () -> {
997+ rpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
998+ });
999+
1000+ List <SpanData > spans = spanExporter .getFinishedSpanItems ();
1001+ assertThat (spans ).isNotEmpty ();
1002+ SpanData rpcSpan =
1003+ spans .stream ()
1004+ .filter (
1005+ span ->
1006+ span .getName ().equals ("com.google.cloud.bigquery.BigQueryRpc.getDataset" ))
1007+ .findFirst ()
1008+ .orElse (null );
1009+ assertNotNull (rpcSpan );
1010+ assertEquals ("invalid" , rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .ERROR_TYPE ));
1011+ assertEquals (
1012+ "Invalid request" , rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .STATUS_MESSAGE ));
1013+ }
1014+ }
1015+ }
9601016
1017+ @ Nested
1018+ class TelemetryEnabledDevDisabled {
1019+ private HttpBigQueryRpc rpc ;
1020+
1021+ @ BeforeEach
1022+ public void setUp () {
1023+ setUpServer ();
1024+ spanExporter .reset (); // Clear spans from previous tests
1025+ System .clearProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" );
1026+ rpc = createRpc (true );
1027+ }
1028+
1029+ @ org .junit .jupiter .api .AfterEach
1030+ public void tearDown () {
1031+ // Ensure property is cleared for this test class
1032+ System .clearProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" );
1033+ }
1034+
1035+ @ Test
1036+ public void testHttpTracingDisabledDoesNotAddAdditionalAttributes () throws Exception {
1037+ synchronized (TEST_LOCK ) {
9611038 setMockResponse (
9621039 "{\" kind\" :\" bigquery#dataset\" ,\" id\" :\" "
9631040 + PROJECT_ID
@@ -969,7 +1046,7 @@ public void testHttpTracingDisabledDoesNotAddAdditionalAttributes() throws Excep
9691046 + DATASET_ID
9701047 + "\" }}" );
9711048
972- customRpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
1049+ rpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
9731050
9741051 verifyRequest ("GET" , "/projects/" + PROJECT_ID + "/datasets/" + DATASET_ID );
9751052 verifySpan (
@@ -990,8 +1067,33 @@ public void testHttpTracingDisabledDoesNotAddAdditionalAttributes() throws Excep
9901067 assertNotNull (rpcSpan );
9911068 assertNull (rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .RPC_SYSTEM_NAME ));
9921069 assertNull (rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .GCP_CLIENT_SERVICE ));
993- } finally {
994- System .clearProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" );
1070+ }
1071+ }
1072+
1073+ @ Test
1074+ public void testHttpTracingDisabled_GenericException_DoesNotSetAttributes () throws Exception {
1075+ synchronized (TEST_LOCK ) {
1076+ spanExporter .reset (); // Clear any accumulated spans
1077+
1078+ assertThrows (
1079+ IOException .class ,
1080+ () -> {
1081+ rpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
1082+ });
1083+
1084+ List <io .opentelemetry .sdk .trace .data .SpanData > spans = spanExporter .getFinishedSpanItems ();
1085+ assertThat (spans ).isNotEmpty ();
1086+ io .opentelemetry .sdk .trace .data .SpanData rpcSpan =
1087+ spans .stream ()
1088+ .filter (
1089+ span ->
1090+ span .getName ().equals ("com.google.cloud.bigquery.BigQueryRpc.getDataset" ))
1091+ .findFirst ()
1092+ .orElse (null );
1093+ assertNotNull (rpcSpan );
1094+ assertNull (rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .EXCEPTION_TYPE ));
1095+ assertNull (rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .ERROR_TYPE ));
1096+ assertNull (rpcSpan .getAttributes ().get (BigQueryTelemetryTracer .STATUS_MESSAGE ));
9951097 }
9961098 }
9971099 }
@@ -1003,9 +1105,17 @@ class TelemetryDisabled {
10031105 @ BeforeEach
10041106 public void setUp () {
10051107 setUpServer ();
1108+ spanExporter .reset (); // Clear spans from previous tests
1109+ System .clearProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" );
10061110 rpc = createRpc (false );
10071111 }
10081112
1113+ @ org .junit .jupiter .api .AfterEach
1114+ public void tearDown () {
1115+ // Ensure property is cleared for this test class
1116+ System .clearProperty ("com.google.cloud.bigquery.http.tracing.dev.enabled" );
1117+ }
1118+
10091119 @ Test
10101120 public void testGetDatasetNoTelemetry () throws Exception {
10111121 setMockResponse (
@@ -1544,5 +1654,20 @@ public void testTestIamPermissionsNoTelemetry() throws Exception {
15441654 + ":testIamPermissions" );
15451655 verifyNoSpans ();
15461656 }
1657+
1658+ @ Test
1659+ public void testExecuteWithSpan_IgnoresHttpResponseException () throws Exception {
1660+ setMockResponse ("" );
1661+ mockResponse .setStatusCode (404 );
1662+
1663+ try {
1664+ rpc .getDatasetSkipExceptionTranslation (PROJECT_ID , DATASET_ID , new HashMap <>());
1665+ org .junit .jupiter .api .Assertions .fail ("Expected HttpResponseException was not thrown" );
1666+ } catch (com .google .api .client .http .HttpResponseException e ) {
1667+ assertEquals (404 , e .getStatusCode ());
1668+ }
1669+
1670+ verifyNoSpans ();
1671+ }
15471672 }
15481673}
0 commit comments