3333import com .google .common .base .Stopwatch ;
3434import com .google .common .collect .ImmutableList ;
3535import com .google .common .collect .Range ;
36+ import io .grpc .ManagedChannelBuilder ;
3637import io .grpc .Status ;
3738import io .opentelemetry .api .OpenTelemetry ;
3839import io .opentelemetry .api .common .Attributes ;
5859import org .junit .runners .JUnit4 ;
5960
6061@ RunWith (JUnit4 .class )
61- public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractMockServerTest {
62+ public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractNettyMockServerTest {
6263
6364 private static final Statement SELECT_RANDOM = Statement .of ("SELECT * FROM random" );
6465
@@ -69,7 +70,8 @@ public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractMockServerTes
6970
7071 private static Map <String , String > attributes ;
7172
72- private static Attributes expectedBaseAttributes ;
73+ private static Attributes expectedCommonBaseAttributes ;
74+ private static Attributes expectedCommonRequestAttributes ;
7375
7476 private static final long MIN_LATENCY = 0 ;
7577
@@ -89,10 +91,11 @@ public static void setup() {
8991 String client_name = "spanner-java/" ;
9092 openTelemetry = OpenTelemetrySdk .builder ().setMeterProvider (meterProvider .build ()).build ();
9193 provider .reset ();
92- provider .initialize ("test-project" , client_name , null , null );
94+ // provider.getOpenTelemetry().getMeterProvider().
95+ provider .initialize (openTelemetry , "test-project" , client_name , null , null );
9396 attributes = provider .getClientAttributes ();
9497
95- expectedBaseAttributes =
98+ expectedCommonBaseAttributes =
9699 Attributes .builder ()
97100 .put (BuiltInMetricsConstant .PROJECT_ID_KEY , "test-project" )
98101 .put (BuiltInMetricsConstant .INSTANCE_CONFIG_ID_KEY , "unknown" )
@@ -103,6 +106,14 @@ public static void setup() {
103106 .put (BuiltInMetricsConstant .CLIENT_UID_KEY , attributes .get ("client_uid" ))
104107 .put (BuiltInMetricsConstant .CLIENT_HASH_KEY , attributes .get ("client_hash" ))
105108 .build ();
109+
110+ expectedCommonRequestAttributes =
111+ Attributes .builder ()
112+ .put (BuiltInMetricsConstant .INSTANCE_ID_KEY , "i" )
113+ .put (BuiltInMetricsConstant .DATABASE_KEY , "d" )
114+ .put (BuiltInMetricsConstant .DIRECT_PATH_ENABLED_KEY , "false" )
115+ .put (BuiltInMetricsConstant .DIRECT_PATH_USED_KEY , "false" )
116+ .build ();
106117 }
107118
108119 @ BeforeClass
@@ -137,18 +148,18 @@ public void createSpannerInstance() {
137148 .setRetryDelayMultiplier (1.0 )
138149 .setTotalTimeoutDuration (Duration .ofMinutes (10L ))
139150 .build ()));
151+ String endpoint = address .getHostString () + ":" + server .getPort ();
140152 spanner =
141- builder
153+ SpannerOptions . newBuilder ()
142154 .setProjectId ("test-project" )
143- .setChannelProvider (channelProvider )
155+ .setChannelConfigurator (ManagedChannelBuilder ::usePlaintext )
156+ .setHost ("http://" + endpoint )
144157 .setCredentials (NoCredentials .getInstance ())
145158 .setSessionPoolOption (
146159 SessionPoolOptions .newBuilder ()
147160 .setWaitForMinSessionsDuration (Duration .ofSeconds (5L ))
148161 .setFailOnSessionLeak ()
149162 .build ())
150- // Setting this to false so that Spanner Options does not register Metrics Tracer
151- // factory again.
152163 .setBuiltInMetricsEnabled (false )
153164 .setApiTracerFactory (metricsTracerFactory )
154165 .build ()
@@ -166,8 +177,9 @@ public void testMetricsSingleUseQuery() {
166177
167178 long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
168179 Attributes expectedAttributes =
169- expectedBaseAttributes
180+ expectedCommonBaseAttributes
170181 .toBuilder ()
182+ .putAll (expectedCommonRequestAttributes )
171183 .put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
172184 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.ExecuteStreamingSql" )
173185 .build ();
@@ -189,6 +201,11 @@ public void testMetricsSingleUseQuery() {
189201 MetricData attemptCountMetricData =
190202 getMetricData (metricReader , BuiltInMetricsConstant .ATTEMPT_COUNT_NAME );
191203 assertThat (getAggregatedValue (attemptCountMetricData , expectedAttributes )).isEqualTo (1 );
204+
205+ MetricData gfeLatencyMetricData =
206+ getMetricData (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME );
207+ long gfeLatencyValue = getAggregatedValue (attemptLatencyMetricData , expectedAttributes );
208+ assertThat (gfeLatencyValue ).isEqualTo (gfeLatencyValue );
192209 }
193210
194211 @ Test
@@ -205,14 +222,15 @@ public void testMetricsWithGaxRetryUnaryRpc() {
205222 stopwatch .elapsed (TimeUnit .MILLISECONDS );
206223
207224 Attributes expectedAttributesBeginTransactionOK =
208- expectedBaseAttributes
225+ expectedCommonBaseAttributes
209226 .toBuilder ()
227+ .putAll (expectedCommonRequestAttributes )
210228 .put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
211229 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
212230 .build ();
213231
214232 Attributes expectedAttributesBeginTransactionFailed =
215- expectedBaseAttributes
233+ expectedCommonBaseAttributes
216234 .toBuilder ()
217235 .put (BuiltInMetricsConstant .STATUS_KEY , "UNAVAILABLE" )
218236 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
0 commit comments