|
42 | 42 | import io.grpc.ClientInterceptor; |
43 | 43 | import io.grpc.ClientInterceptors; |
44 | 44 | import io.grpc.ClientStreamTracer; |
| 45 | +import io.grpc.ConnectivityState; |
| 46 | +import io.grpc.EquivalentAddressGroup; |
45 | 47 | import io.grpc.KnownLength; |
| 48 | +import io.grpc.LoadBalancer; |
| 49 | +import io.grpc.LoadBalancerProvider; |
| 50 | +import io.grpc.LoadBalancerRegistry; |
46 | 51 | import io.grpc.ManagedChannel; |
47 | 52 | import io.grpc.Metadata; |
48 | 53 | import io.grpc.MethodDescriptor; |
| 54 | +import io.grpc.NameResolver; |
| 55 | +import io.grpc.NameResolverProvider; |
| 56 | +import io.grpc.NameResolverRegistry; |
49 | 57 | import io.grpc.NoopServerCall; |
50 | 58 | import io.grpc.Server; |
51 | 59 | import io.grpc.ServerCall; |
@@ -438,6 +446,146 @@ public void clientAttemptDelayTracing_reasonChangedInvariant() { |
438 | 446 | AttributeKey.stringKey("grpc.delay_reason"))); |
439 | 447 | } |
440 | 448 |
|
| 449 | + @Test |
| 450 | + public void clientAttemptDelayTracing_endToEnd_inProcessTransport() throws Exception { |
| 451 | + final java.util.concurrent.CountDownLatch latch = |
| 452 | + new java.util.concurrent.CountDownLatch(1); |
| 453 | + LoadBalancerProvider slowLbProvider = new LoadBalancerProvider() { |
| 454 | + @Override |
| 455 | + public boolean isAvailable() { |
| 456 | + return true; |
| 457 | + } |
| 458 | + |
| 459 | + @Override |
| 460 | + public int getPriority() { |
| 461 | + return 5; |
| 462 | + } |
| 463 | + |
| 464 | + @Override |
| 465 | + public String getPolicyName() { |
| 466 | + return "slow_connecting_policy"; |
| 467 | + } |
| 468 | + |
| 469 | + @Override |
| 470 | + public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { |
| 471 | + return new LoadBalancer() { |
| 472 | + @Override |
| 473 | + public Status acceptResolvedAddresses(LoadBalancer.ResolvedAddresses resolvedAddresses) { |
| 474 | + helper.updateBalancingState(ConnectivityState.CONNECTING, new SubchannelPicker() { |
| 475 | + @Override |
| 476 | + public PickResult pickSubchannel(PickSubchannelArgs args) { |
| 477 | + return PickResult.withNoResult("connecting", |
| 478 | + "Simulated slow TLS handshake with backend"); |
| 479 | + } |
| 480 | + }); |
| 481 | + latch.countDown(); |
| 482 | + return Status.OK; |
| 483 | + } |
| 484 | + |
| 485 | + @Override |
| 486 | + public void handleNameResolutionError(Status error) {} |
| 487 | + |
| 488 | + @Override |
| 489 | + public void shutdown() {} |
| 490 | + }; |
| 491 | + } |
| 492 | + }; |
| 493 | + LoadBalancerRegistry.getDefaultRegistry().register(slowLbProvider); |
| 494 | + |
| 495 | + NameResolverProvider customResolverProvider = new NameResolverProvider() { |
| 496 | + @Override |
| 497 | + protected boolean isAvailable() { |
| 498 | + return true; |
| 499 | + } |
| 500 | + |
| 501 | + @Override |
| 502 | + protected int priority() { |
| 503 | + return 5; |
| 504 | + } |
| 505 | + |
| 506 | + @Override |
| 507 | + public String getDefaultScheme() { |
| 508 | + return "inproce2e"; |
| 509 | + } |
| 510 | + |
| 511 | + @Override |
| 512 | + public java.util.Collection<Class<? extends java.net.SocketAddress>> |
| 513 | + getProducedSocketAddressTypes() { |
| 514 | + return java.util.Collections.singleton(io.grpc.inprocess.InProcessSocketAddress.class); |
| 515 | + } |
| 516 | + |
| 517 | + @Override |
| 518 | + public NameResolver newNameResolver(java.net.URI targetUri, NameResolver.Args args) { |
| 519 | + return new NameResolver() { |
| 520 | + @Override |
| 521 | + public String getServiceAuthority() { |
| 522 | + return "inproce2e"; |
| 523 | + } |
| 524 | + |
| 525 | + @Override |
| 526 | + public void start(Listener2 listener) { |
| 527 | + listener.onResult(ResolutionResult.newBuilder() |
| 528 | + .setAddresses(java.util.Collections.singletonList(new EquivalentAddressGroup( |
| 529 | + new io.grpc.inprocess.InProcessSocketAddress("test-e2e")))) |
| 530 | + .build()); |
| 531 | + } |
| 532 | + |
| 533 | + @Override |
| 534 | + public void shutdown() {} |
| 535 | + }; |
| 536 | + } |
| 537 | + }; |
| 538 | + NameResolverRegistry.getDefaultRegistry().register(customResolverProvider); |
| 539 | + |
| 540 | + GrpcOpenTelemetry grpcOpenTelemetry = GrpcOpenTelemetry.newBuilder() |
| 541 | + .sdk(openTelemetryRule.getOpenTelemetry()) |
| 542 | + .enableTracing(true) |
| 543 | + .build(); |
| 544 | + |
| 545 | + InProcessChannelBuilder channelBuilder = |
| 546 | + InProcessChannelBuilder.forTarget("inproce2e:///test-e2e") |
| 547 | + .defaultLoadBalancingPolicy("slow_connecting_policy"); |
| 548 | + grpcOpenTelemetry.configureChannelBuilder(channelBuilder); |
| 549 | + ManagedChannel channel = channelBuilder.build(); |
| 550 | + try { |
| 551 | + ClientCall<String, String> call = channel.newCall(method, CallOptions.DEFAULT); |
| 552 | + call.start(new ClientCall.Listener<String>() {}, new Metadata()); |
| 553 | + call.request(1); |
| 554 | + |
| 555 | + latch.await(5, java.util.concurrent.TimeUnit.SECONDS); |
| 556 | + Thread.sleep(50); |
| 557 | + call.cancel("End test delay segment", null); |
| 558 | + } finally { |
| 559 | + channel.shutdownNow(); |
| 560 | + channel.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS); |
| 561 | + LoadBalancerRegistry.getDefaultRegistry().deregister(slowLbProvider); |
| 562 | + NameResolverRegistry.getDefaultRegistry().deregister(customResolverProvider); |
| 563 | + } |
| 564 | + |
| 565 | + List<SpanData> spans = openTelemetryRule.getSpans(); |
| 566 | + SpanData delaySpanData = null; |
| 567 | + for (SpanData s : spans) { |
| 568 | + if ("Attempt Delay".equals(s.getName())) { |
| 569 | + delaySpanData = s; |
| 570 | + break; |
| 571 | + } |
| 572 | + } |
| 573 | + assertNotNull(delaySpanData); |
| 574 | + assertEquals("connecting", |
| 575 | + delaySpanData.getAttributes().get(AttributeKey.stringKey("grpc.delay_type"))); |
| 576 | + |
| 577 | + boolean foundTransition = false; |
| 578 | + for (io.opentelemetry.sdk.trace.data.EventData event : delaySpanData.getEvents()) { |
| 579 | + if ("Delay state transition".equals(event.getName()) |
| 580 | + && "Simulated slow TLS handshake with backend".equals( |
| 581 | + event.getAttributes().get(AttributeKey.stringKey("grpc.delay_reason")))) { |
| 582 | + foundTransition = true; |
| 583 | + break; |
| 584 | + } |
| 585 | + } |
| 586 | + assertTrue(foundTransition); |
| 587 | + } |
| 588 | + |
441 | 589 | @Test |
442 | 590 | public void clientAttemptDelayStart_featureFlagDisabled_zeroChildSpans() { |
443 | 591 | System.setProperty("GRPC_EXPERIMENTAL_ENABLE_DELAY_OBSERVABILITY", "false"); |
|
0 commit comments