|
34 | 34 | import static org.junit.Assert.assertThrows; |
35 | 35 |
|
36 | 36 | import com.google.api.gax.core.FixedCredentialsProvider; |
| 37 | +import com.google.api.gax.core.NoCredentialsProvider; |
37 | 38 | import com.google.api.gax.httpjson.RestSerializationException; |
38 | 39 | import com.google.api.gax.rpc.DeadlineExceededException; |
39 | 40 | import com.google.api.gax.rpc.StatusCode.Code; |
| 41 | +import com.google.api.gax.rpc.TransportChannelProvider; |
40 | 42 | import com.google.api.gax.rpc.UnavailableException; |
41 | 43 | import com.google.api.gax.tracing.ObservabilityAttributes; |
42 | 44 | import com.google.api.gax.tracing.SpanTracerFactory; |
|
48 | 50 | import com.google.showcase.v1beta1.EchoSettings; |
49 | 51 | import com.google.showcase.v1beta1.it.util.TestClientInitializer; |
50 | 52 | import com.google.showcase.v1beta1.stub.EchoStubSettings; |
| 53 | +import com.google.api.client.http.HttpTransport; |
51 | 54 | import io.grpc.CallOptions; |
52 | 55 | import io.grpc.Channel; |
53 | 56 | import io.grpc.ClientCall; |
54 | 57 | import io.grpc.ClientInterceptor; |
55 | 58 | import io.grpc.ManagedChannelBuilder; |
56 | 59 | import io.grpc.MethodDescriptor; |
| 60 | +import io.grpc.Metadata; |
57 | 61 | import io.opentelemetry.api.GlobalOpenTelemetry; |
58 | 62 | import io.opentelemetry.api.common.AttributeKey; |
59 | 63 | import io.opentelemetry.sdk.OpenTelemetrySdk; |
60 | 64 | import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; |
61 | 65 | import io.opentelemetry.sdk.trace.SdkTracerProvider; |
62 | 66 | import io.opentelemetry.sdk.trace.data.SpanData; |
63 | 67 | import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
| 68 | +import java.io.ByteArrayInputStream; |
64 | 69 | import java.io.FileNotFoundException; |
65 | 70 | import java.io.IOException; |
| 71 | +import java.io.InputStream; |
66 | 72 | import java.net.BindException; |
67 | 73 | import java.net.NoRouteToHostException; |
68 | 74 | import java.net.ServerSocket; |
@@ -164,36 +170,157 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
164 | 170 | void testTracing_failedEcho_grpc_recordsErrorType() throws Exception { |
165 | 171 | SpanTracerFactory tracingFactory = new SpanTracerFactory(openTelemetrySdk); |
166 | 172 |
|
| 173 | + ClientInterceptor interceptor = |
| 174 | + new ClientInterceptor() { |
| 175 | + @Override |
| 176 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 177 | + MethodDescriptor<ReqT, RespT> method, |
| 178 | + CallOptions callOptions, |
| 179 | + Channel next) { |
| 180 | + return new ClientCall<ReqT, RespT>() { |
| 181 | + @Override |
| 182 | + public void start(Listener<RespT> responseListener, Metadata headers) { |
| 183 | + responseListener.onClose(io.grpc.Status.UNAVAILABLE, new Metadata()); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void request(int numMessages) {} |
| 188 | + |
| 189 | + @Override |
| 190 | + public void cancel(String message, Throwable cause) {} |
| 191 | + |
| 192 | + @Override |
| 193 | + public void halfClose() {} |
| 194 | + |
| 195 | + @Override |
| 196 | + public void sendMessage(ReqT message) {} |
| 197 | + }; |
| 198 | + } |
| 199 | + }; |
| 200 | + |
| 201 | + TransportChannelProvider transportChannelProvider = |
| 202 | + EchoSettings.defaultGrpcTransportProviderBuilder() |
| 203 | + .setChannelConfigurator(io.grpc.ManagedChannelBuilder::usePlaintext) |
| 204 | + .setInterceptorProvider(() -> ImmutableList.of(interceptor)) |
| 205 | + .build(); |
| 206 | + |
167 | 207 | try (EchoClient client = |
168 | | - TestClientInitializer.createGrpcEchoClientOpentelemetry(tracingFactory)) { |
| 208 | + TestClientInitializer.createGrpcEchoClientOpentelemetry( |
| 209 | + tracingFactory, transportChannelProvider)) { |
169 | 210 |
|
170 | 211 | EchoRequest echoRequest = |
171 | 212 | EchoRequest.newBuilder() |
172 | | - .setError(Status.newBuilder().setCode(Code.UNAVAILABLE.ordinal()).build()) |
| 213 | + .setError(Status.newBuilder().setCode(com.google.rpc.Code.UNAVAILABLE.ordinal()).build()) |
173 | 214 | .build(); |
174 | 215 |
|
175 | 216 | assertThrows(UnavailableException.class, () -> client.echo(echoRequest)); |
176 | 217 | verifyErrorTypeAttribute("UNAVAILABLE"); |
177 | 218 | } |
| 219 | + |
178 | 220 | } |
179 | 221 |
|
| 222 | + |
180 | 223 | @Test |
181 | 224 | void testTracing_failedEcho_httpjson_recordsErrorType() throws Exception { |
182 | 225 | SpanTracerFactory tracingFactory = new SpanTracerFactory(openTelemetrySdk); |
183 | 226 |
|
184 | | - try (EchoClient client = |
185 | | - TestClientInitializer.createHttpJsonEchoClientOpentelemetry(tracingFactory)) { |
| 227 | + HttpTransport mockTransport = |
| 228 | + new HttpTransport() { |
| 229 | + @Override |
| 230 | + protected com.google.api.client.http.LowLevelHttpRequest buildRequest( |
| 231 | + String method, String url) { |
| 232 | + return new com.google.api.client.http.LowLevelHttpRequest() { |
| 233 | + @Override |
| 234 | + public void addHeader(String name, String value) {} |
| 235 | + |
| 236 | + @Override |
| 237 | + public com.google.api.client.http.LowLevelHttpResponse execute() { |
| 238 | + return new com.google.api.client.http.LowLevelHttpResponse() { |
| 239 | + @Override |
| 240 | + public InputStream getContent() { |
| 241 | + return new ByteArrayInputStream("{}".getBytes()); |
| 242 | + } |
| 243 | + |
| 244 | + |
| 245 | + @Override |
| 246 | + public String getContentEncoding() { |
| 247 | + return null; |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public long getContentLength() { |
| 252 | + return 2; |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public String getContentType() { |
| 257 | + return "application/json"; |
| 258 | + } |
| 259 | + |
| 260 | + @Override |
| 261 | + public String getStatusLine() { |
| 262 | + return "HTTP/1.1 503 Service Unavailable"; |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + public int getStatusCode() { |
| 267 | + return 503; |
| 268 | + } |
| 269 | + |
| 270 | + @Override |
| 271 | + public String getReasonPhrase() { |
| 272 | + return "Service Unavailable"; |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public int getHeaderCount() { |
| 277 | + return 0; |
| 278 | + } |
| 279 | + |
| 280 | + @Override |
| 281 | + public String getHeaderName(int index) { |
| 282 | + return null; |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public String getHeaderValue(int index) { |
| 287 | + return null; |
| 288 | + } |
| 289 | + }; |
| 290 | + } |
| 291 | + }; |
| 292 | + } |
| 293 | + }; |
| 294 | + |
| 295 | + EchoSettings httpJsonEchoSettings = |
| 296 | + EchoSettings.newHttpJsonBuilder() |
| 297 | + .setCredentialsProvider(NoCredentialsProvider.create()) |
| 298 | + .setTransportChannelProvider( |
| 299 | + EchoSettings.defaultHttpJsonTransportProviderBuilder() |
| 300 | + .setHttpTransport(mockTransport) |
| 301 | + .setEndpoint(TestClientInitializer.DEFAULT_HTTPJSON_ENDPOINT) |
| 302 | + .build()) |
| 303 | + .build(); |
| 304 | + |
| 305 | + EchoStubSettings echoStubSettings = |
| 306 | + (EchoStubSettings) |
| 307 | + httpJsonEchoSettings.getStubSettings().toBuilder() |
| 308 | + .setTracerFactory(tracingFactory) |
| 309 | + .build(); |
186 | 310 |
|
| 311 | + try (EchoClient client = EchoClient.create(echoStubSettings.createStub())) { |
187 | 312 | EchoRequest echoRequest = |
188 | 313 | EchoRequest.newBuilder() |
189 | | - .setError(Status.newBuilder().setCode(Code.UNAVAILABLE.ordinal()).build()) |
| 314 | + .setError(Status.newBuilder().setCode(com.google.rpc.Code.UNAVAILABLE.ordinal()).build()) |
190 | 315 | .build(); |
191 | 316 |
|
| 317 | + |
192 | 318 | assertThrows(UnavailableException.class, () -> client.echo(echoRequest)); |
193 | 319 | verifyErrorTypeAttribute("503"); |
194 | 320 | } |
195 | 321 | } |
196 | 322 |
|
| 323 | + |
197 | 324 | @Test |
198 | 325 | void testTracing_clientConnectionError_ConnectException_grpc() throws Exception { |
199 | 326 | int port; |
@@ -363,54 +490,15 @@ void testTracing_clientTimeout_DeadlineExceededException_grpc() throws Exception |
363 | 490 |
|
364 | 491 | @Test |
365 | 492 | void testTracing_clientAuthenticationError_GeneralSecurityException_grpc() throws Exception { |
366 | | - Credentials credentials = |
367 | | - new Credentials() { |
368 | | - @Override |
369 | | - public String getAuthenticationType() { |
370 | | - return "mock"; |
371 | | - } |
372 | | - |
373 | | - @Override |
374 | | - public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException { |
375 | | - throw new IOException("Mock auth failure", new GeneralSecurityException("Root cause")); |
376 | | - } |
377 | | - |
378 | | - @Override |
379 | | - public boolean hasRequestMetadata() { |
380 | | - return true; |
381 | | - } |
382 | | - |
383 | | - @Override |
384 | | - public boolean hasRequestMetadataOnly() { |
385 | | - return true; |
386 | | - } |
387 | | - |
388 | | - @Override |
389 | | - public void refresh() throws IOException {} |
390 | | - }; |
391 | | - |
392 | | - SpanTracerFactory tracingFactory = new SpanTracerFactory(openTelemetrySdk); |
393 | | - EchoSettings grpcEchoSettings = |
394 | | - EchoSettings.newBuilder() |
395 | | - .setCredentialsProvider(FixedCredentialsProvider.create(credentials)) |
396 | | - .setTransportChannelProvider( |
397 | | - EchoSettings.defaultGrpcTransportProviderBuilder() |
398 | | - .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) |
399 | | - .build()) |
400 | | - .setEndpoint(TestClientInitializer.DEFAULT_GRPC_ENDPOINT) |
401 | | - .build(); |
402 | | - |
403 | | - EchoStubSettings.Builder echoStubSettingsBuilder = |
404 | | - (EchoStubSettings.Builder) grpcEchoSettings.getStubSettings().toBuilder(); |
405 | | - echoStubSettingsBuilder.setTracerFactory(tracingFactory); |
406 | | - |
407 | | - try (EchoClient client = EchoClient.create(echoStubSettingsBuilder.build().createStub())) { |
| 493 | + try (EchoClient client = createInterceptorClient(new GeneralSecurityException("Mock auth failure"))) { |
408 | 494 | assertThrows( |
409 | 495 | Exception.class, () -> client.echo(EchoRequest.newBuilder().setContent("test").build())); |
410 | 496 | verifyErrorTypeAttribute("CLIENT_AUTHENTICATION_ERROR"); |
411 | 497 | } |
412 | 498 | } |
413 | 499 |
|
| 500 | + |
| 501 | + |
414 | 502 | @Test |
415 | 503 | void testTracing_clientAuthenticationError_FileNotFoundException_grpc() throws Exception { |
416 | 504 | try (EchoClient client = createInterceptorClient(new FileNotFoundException("Key not found"))) { |
|
0 commit comments