2424import static org .junit .Assert .assertThrows ;
2525import static org .junit .Assert .assertTrue ;
2626import static org .junit .Assert .fail ;
27- import static org .junit .Assume .assumeFalse ;
28- import static org .junit .Assume .assumeTrue ;
2927
3028import com .google .api .gax .longrunning .OperationTimedPollAlgorithm ;
3129import com .google .api .gax .retrying .RetrySettings ;
3432import com .google .cloud .spanner .MockSpannerServiceImpl .SimulatedExecutionTime ;
3533import com .google .cloud .spanner .MockSpannerServiceImpl .StatementResult ;
3634import com .google .cloud .spanner .connection .RandomResultSetGenerator ;
37- import com .google .cloud .spanner .spi .v1 .GapicSpannerRpc ;
3835import com .google .common .base .Stopwatch ;
3936import com .google .common .collect .ImmutableList ;
4037import com .google .common .collect .Range ;
5148import io .opentelemetry .sdk .metrics .data .MetricData ;
5249import io .opentelemetry .sdk .testing .exporter .InMemoryMetricReader ;
5350import java .io .IOException ;
54- import java .lang .reflect .Field ;
5551import java .net .InetSocketAddress ;
5652import java .time .Duration ;
5753import java .util .Collection ;
@@ -195,19 +191,11 @@ public void testMetricsSingleUseQuery() {
195191 checkIfMetricExists (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME ));
196192 assertFalse (
197193 checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME ));
198- if (GapicSpannerRpc .isEnableDirectPathXdsEnv ()) {
199- // AFE metrics are enabled for DirectPath.
200- MetricData afeLatencyMetricData =
201- getMetricData (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME );
202- double afeLatencyValue = getAggregatedValue (afeLatencyMetricData , expectedAttributes );
203- assertEquals (fakeAFEServerTiming .get (), afeLatencyValue , 1e-6 );
204- } else {
205- MetricData gfeLatencyMetricData =
206- getMetricData (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME );
207- double gfeLatencyValue = getAggregatedValue (gfeLatencyMetricData , expectedAttributes );
208- assertEquals (fakeServerTiming .get (), gfeLatencyValue , 1e-6 );
209- assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME ));
210- }
194+ // AFE metrics are enabled for DirectPath.
195+ MetricData afeLatencyMetricData =
196+ getMetricData (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME );
197+ double afeLatencyValue = getAggregatedValue (afeLatencyMetricData , expectedAttributes );
198+ assertEquals (fakeAFEServerTiming .get (), afeLatencyValue , 1e-6 );
211199 }
212200
213201 private boolean isJava8 () {
@@ -218,75 +206,6 @@ private boolean isWindows() {
218206 return System .getProperty ("os.name" ).toLowerCase ().contains ("windows" );
219207 }
220208
221- @ Test
222- public void testMetricsSingleUseQueryWithAfeEnabled () throws Exception {
223- assumeTrue (isJava8 () && !isWindows ());
224- assumeFalse (System .getenv ().containsKey ("SPANNER_DISABLE_AFE_SERVER_TIMING" ));
225-
226- Class <?> classOfMap = System .getenv ().getClass ();
227- Field field = classOfMap .getDeclaredField ("m" );
228- field .setAccessible (true );
229- Map <String , String > writeableEnvironmentVariables =
230- (Map <String , String >) field .get (System .getenv ());
231-
232- try {
233- writeableEnvironmentVariables .put ("SPANNER_DISABLE_AFE_SERVER_TIMING" , "false" );
234-
235- Stopwatch stopwatch = Stopwatch .createStarted ();
236- try (ResultSet resultSet = client .singleUse ().executeQuery (SELECT_RANDOM )) {
237- assertTrue (resultSet .next ());
238- assertFalse (resultSet .next ());
239- }
240-
241- double elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
242- Attributes expectedAttributes =
243- expectedCommonBaseAttributes .toBuilder ()
244- .putAll (expectedCommonRequestAttributes )
245- .put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
246- .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.ExecuteStreamingSql" )
247- .build ();
248-
249- MetricData operationLatencyMetricData =
250- getMetricData (metricReader , BuiltInMetricsConstant .OPERATION_LATENCIES_NAME );
251- assertNotNull (operationLatencyMetricData );
252- double operationLatencyValue =
253- getAggregatedValue (operationLatencyMetricData , expectedAttributes );
254- assertThat (operationLatencyValue ).isIn (Range .closed (MIN_LATENCY , elapsed ));
255-
256- MetricData attemptLatencyMetricData =
257- getMetricData (metricReader , BuiltInMetricsConstant .ATTEMPT_LATENCIES_NAME );
258- assertNotNull (attemptLatencyMetricData );
259- double attemptLatencyValue = getAggregatedValue (attemptLatencyMetricData , expectedAttributes );
260- assertThat (attemptLatencyValue ).isIn (Range .closed (MIN_LATENCY , elapsed ));
261-
262- MetricData operationCountMetricData =
263- getMetricData (metricReader , BuiltInMetricsConstant .OPERATION_COUNT_NAME );
264- assertNotNull (operationCountMetricData );
265- assertThat (getAggregatedValue (operationCountMetricData , expectedAttributes )).isEqualTo (1 );
266-
267- MetricData attemptCountMetricData =
268- getMetricData (metricReader , BuiltInMetricsConstant .ATTEMPT_COUNT_NAME );
269- assertNotNull (attemptCountMetricData );
270- assertThat (getAggregatedValue (attemptCountMetricData , expectedAttributes )).isEqualTo (1 );
271-
272- assertFalse (
273- checkIfMetricExists (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME ));
274- assertFalse (
275- checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME ));
276- MetricData afeLatencyMetricData =
277- getMetricData (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME );
278- double afeLatencyValue = getAggregatedValue (afeLatencyMetricData , expectedAttributes );
279- assertEquals (fakeAFEServerTiming .get (), afeLatencyValue , 1e-6 );
280-
281- MetricData gfeLatencyMetricData =
282- getMetricData (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME );
283- double gfeLatencyValue = getAggregatedValue (gfeLatencyMetricData , expectedAttributes );
284- assertEquals (fakeServerTiming .get (), gfeLatencyValue , 1e-6 );
285- } finally {
286- writeableEnvironmentVariables .remove ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" );
287- }
288- }
289-
290209 @ Test
291210 public void testMetricsWithGaxRetryUnaryRpc () {
292211 Stopwatch stopwatch = Stopwatch .createStarted ();
@@ -454,17 +373,9 @@ public void testNoServerTimingHeader() throws IOException, InterruptedException
454373
455374 assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME ));
456375 assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME ));
457- if (GapicSpannerRpc .isEnableDirectPathXdsEnv ()) {
458- MetricData afeConnectivityMetricData =
459- getMetricData (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME );
460- assertThat (getAggregatedValue (afeConnectivityMetricData , expectedAttributes )).isEqualTo (1 );
461- } else {
462- MetricData gfeConnectivityMetricData =
463- getMetricData (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME );
464- assertThat (getAggregatedValue (gfeConnectivityMetricData , expectedAttributes )).isEqualTo (1 );
465- assertFalse (
466- checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME ));
467- }
376+ MetricData afeConnectivityMetricData =
377+ getMetricData (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME );
378+ assertThat (getAggregatedValue (afeConnectivityMetricData , expectedAttributes )).isEqualTo (1 );
468379
469380 spannerNoHeader .close ();
470381 serverNoHeader .shutdown ();
0 commit comments