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,19 +148,19 @@ 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 .setSkipVerifyingBeginTransactionForMuxRW (true )
150163 .build ())
151- // Setting this to false so that Spanner Options does not register Metrics Tracer
152- // factory again.
153164 .setBuiltInMetricsEnabled (false )
154165 .setApiTracerFactory (metricsTracerFactory )
155166 .build ()
@@ -167,8 +178,9 @@ public void testMetricsSingleUseQuery() {
167178
168179 long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
169180 Attributes expectedAttributes =
170- expectedBaseAttributes
181+ expectedCommonBaseAttributes
171182 .toBuilder ()
183+ .putAll (expectedCommonRequestAttributes )
172184 .put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
173185 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.ExecuteStreamingSql" )
174186 .build ();
@@ -190,6 +202,11 @@ public void testMetricsSingleUseQuery() {
190202 MetricData attemptCountMetricData =
191203 getMetricData (metricReader , BuiltInMetricsConstant .ATTEMPT_COUNT_NAME );
192204 assertThat (getAggregatedValue (attemptCountMetricData , expectedAttributes )).isEqualTo (1 );
205+
206+ MetricData gfeLatencyMetricData =
207+ getMetricData (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME );
208+ long gfeLatencyValue = getAggregatedValue (attemptLatencyMetricData , expectedAttributes );
209+ assertThat (gfeLatencyValue ).isEqualTo (gfeLatencyValue );
193210 }
194211
195212 @ Test
@@ -206,14 +223,15 @@ public void testMetricsWithGaxRetryUnaryRpc() {
206223 stopwatch .elapsed (TimeUnit .MILLISECONDS );
207224
208225 Attributes expectedAttributesBeginTransactionOK =
209- expectedBaseAttributes
226+ expectedCommonBaseAttributes
210227 .toBuilder ()
228+ .putAll (expectedCommonRequestAttributes )
211229 .put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
212230 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
213231 .build ();
214232
215233 Attributes expectedAttributesBeginTransactionFailed =
216- expectedBaseAttributes
234+ expectedCommonBaseAttributes
217235 .toBuilder ()
218236 .put (BuiltInMetricsConstant .STATUS_KEY , "UNAVAILABLE" )
219237 .put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
0 commit comments