6060import org .junit .runners .JUnit4 ;
6161
6262@ RunWith (JUnit4 .class )
63- public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractMockServerTest {
63+ public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractNettyMockServerTest {
6464
6565 private static final Statement SELECT_RANDOM = Statement .of ("SELECT * FROM random" );
6666
@@ -71,7 +71,8 @@ public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractMockServerTes
7171
7272 private static Map <String , String > attributes ;
7373
74- private static Attributes expectedBaseAttributes ;
74+ private static Attributes expectedCommonBaseAttributes ;
75+ private static Attributes expectedCommonRequestAttributes ;
7576
7677 private static final long MIN_LATENCY = 0 ;
7778
@@ -91,10 +92,11 @@ public static void setup() {
9192 String client_name = "spanner-java/" ;
9293 openTelemetry = OpenTelemetrySdk .builder ().setMeterProvider (meterProvider .build ()).build ();
9394 provider .reset ();
94- provider .initialize ("test-project" , client_name , null , null );
95+ // provider.getOpenTelemetry().getMeterProvider().
96+ provider .initialize (openTelemetry , "test-project" , client_name , null , null );
9597 attributes = provider .getClientAttributes ();
9698
97- expectedBaseAttributes =
99+ expectedCommonBaseAttributes =
98100 Attributes .builder ()
99101 .put (BuiltInMetricsConstant .PROJECT_ID_KEY , "test-project" )
100102 .put (BuiltInMetricsConstant .INSTANCE_CONFIG_ID_KEY , "unknown" )
@@ -105,6 +107,14 @@ public static void setup() {
105107 .put (BuiltInMetricsConstant .CLIENT_UID_KEY , attributes .get ("client_uid" ))
106108 .put (BuiltInMetricsConstant .CLIENT_HASH_KEY , attributes .get ("client_hash" ))
107109 .build ();
110+
111+ expectedCommonRequestAttributes =
112+ Attributes .builder ()
113+ .put (BuiltInMetricsConstant .INSTANCE_ID_KEY , "i" )
114+ .put (BuiltInMetricsConstant .DATABASE_KEY , "d" )
115+ .put (BuiltInMetricsConstant .DIRECT_PATH_ENABLED_KEY , "false" )
116+ .put (BuiltInMetricsConstant .DIRECT_PATH_USED_KEY , "false" )
117+ .build ();
108118 }
109119
110120 @ BeforeClass
@@ -139,19 +149,19 @@ public void createSpannerInstance() {
139149 .setRetryDelayMultiplier (1.0 )
140150 .setTotalTimeoutDuration (Duration .ofMinutes (10L ))
141151 .build ()));
152+ String endpoint = address .getHostString () + ":" + server .getPort ();
142153 spanner =
143- builder
154+ SpannerOptions . newBuilder ()
144155 .setProjectId ("test-project" )
145- .setChannelProvider (channelProvider )
156+ .setChannelConfigurator (ManagedChannelBuilder ::usePlaintext )
157+ .setHost ("http://" + endpoint )
146158 .setCredentials (NoCredentials .getInstance ())
147159 .setSessionPoolOption (
148160 SessionPoolOptions .newBuilder ()
149161 .setWaitForMinSessionsDuration (Duration .ofSeconds (5L ))
150162 .setFailOnSessionLeak ()
151163 .setSkipVerifyingBeginTransactionForMuxRW (true )
152164 .build ())
153- // Setting this to false so that Spanner Options does not register Metrics Tracer
154- // factory again.
155165 .setBuiltInMetricsEnabled (false )
156166 .setApiTracerFactory (metricsTracerFactory )
157167 .build ()
@@ -169,8 +179,9 @@ public void testMetricsSingleUseQuery() {
169179
170180 long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
171181 Attributes expectedAttributes =
172- expectedBaseAttributes
182+ expectedCommonBaseAttributes
173183 .toBuilder ()
184+ .putAll (expectedCommonRequestAttributes )
174185 .put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
175186 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.ExecuteStreamingSql" )
176187 .build ();
@@ -196,6 +207,11 @@ public void testMetricsSingleUseQuery() {
196207 getMetricData (metricReader , BuiltInMetricsConstant .ATTEMPT_COUNT_NAME );
197208 assertNotNull (attemptCountMetricData );
198209 assertThat (getAggregatedValue (attemptCountMetricData , expectedAttributes )).isEqualTo (1 );
210+
211+ MetricData gfeLatencyMetricData =
212+ getMetricData (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME );
213+ long gfeLatencyValue = getAggregatedValue (attemptLatencyMetricData , expectedAttributes );
214+ assertThat (gfeLatencyValue ).isEqualTo (gfeLatencyValue );
199215 }
200216
201217 @ Test
@@ -212,14 +228,15 @@ public void testMetricsWithGaxRetryUnaryRpc() {
212228 stopwatch .elapsed (TimeUnit .MILLISECONDS );
213229
214230 Attributes expectedAttributesBeginTransactionOK =
215- expectedBaseAttributes
231+ expectedCommonBaseAttributes
216232 .toBuilder ()
233+ .putAll (expectedCommonRequestAttributes )
217234 .put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
218235 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
219236 .build ();
220237
221238 Attributes expectedAttributesBeginTransactionFailed =
222- expectedBaseAttributes
239+ expectedCommonBaseAttributes
223240 .toBuilder ()
224241 .put (BuiltInMetricsConstant .STATUS_KEY , "UNAVAILABLE" )
225242 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
0 commit comments