|
40 | 40 | import io.grpc.ClientInterceptor; |
41 | 41 | import io.grpc.ClientInterceptors; |
42 | 42 | import io.grpc.ClientStreamTracer; |
| 43 | +import io.grpc.ConnectivityState; |
| 44 | +import io.grpc.EquivalentAddressGroup; |
43 | 45 | import io.grpc.Grpc; |
44 | 46 | import io.grpc.KnownLength; |
| 47 | +import io.grpc.LoadBalancer; |
| 48 | +import io.grpc.LoadBalancerProvider; |
| 49 | +import io.grpc.LoadBalancerRegistry; |
45 | 50 | import io.grpc.ManagedChannel; |
46 | 51 | import io.grpc.Metadata; |
47 | 52 | import io.grpc.MethodDescriptor; |
| 53 | +import io.grpc.NameResolver; |
| 54 | +import io.grpc.NameResolverProvider; |
| 55 | +import io.grpc.NameResolverRegistry; |
48 | 56 | import io.grpc.Server; |
49 | 57 | import io.grpc.ServerCall; |
50 | 58 | import io.grpc.ServerCallHandler; |
|
56 | 64 | import io.grpc.Status.Code; |
57 | 65 | import io.grpc.inprocess.InProcessChannelBuilder; |
58 | 66 | import io.grpc.inprocess.InProcessServerBuilder; |
| 67 | +import io.grpc.inprocess.InProcessSocketAddress; |
59 | 68 | import io.grpc.internal.FakeClock; |
60 | 69 | import io.grpc.internal.StatsTraceContext.ServerCallMethodListener; |
61 | 70 | import io.grpc.opentelemetry.GrpcOpenTelemetry.TargetFilter; |
|
79 | 88 | import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; |
80 | 89 | import java.io.IOException; |
81 | 90 | import java.io.InputStream; |
| 91 | +import java.net.SocketAddress; |
| 92 | +import java.net.URI; |
82 | 93 | import java.util.Arrays; |
| 94 | +import java.util.Collection; |
| 95 | +import java.util.Collections; |
83 | 96 | import java.util.List; |
84 | 97 | import java.util.Map; |
85 | 98 | import java.util.Optional; |
| 99 | +import java.util.concurrent.CountDownLatch; |
86 | 100 | import java.util.concurrent.TimeUnit; |
87 | 101 | import java.util.concurrent.atomic.AtomicReference; |
88 | 102 | import javax.annotation.Nullable; |
@@ -1648,6 +1662,134 @@ public void clientAttemptDelayDuration_recorded() { |
1648 | 1662 | }))); |
1649 | 1663 | } |
1650 | 1664 |
|
| 1665 | + @Test |
| 1666 | + public void clientAttemptDelayDuration_endToEnd_inProcessTransport() throws Exception { |
| 1667 | + final CountDownLatch latch = new CountDownLatch(1); |
| 1668 | + LoadBalancerProvider slowLbProvider = new LoadBalancerProvider() { |
| 1669 | + @Override |
| 1670 | + public boolean isAvailable() { |
| 1671 | + return true; |
| 1672 | + } |
| 1673 | + |
| 1674 | + @Override |
| 1675 | + public int getPriority() { |
| 1676 | + return 5; |
| 1677 | + } |
| 1678 | + |
| 1679 | + @Override |
| 1680 | + public String getPolicyName() { |
| 1681 | + return "slow_metrics_connecting_policy"; |
| 1682 | + } |
| 1683 | + |
| 1684 | + @Override |
| 1685 | + public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { |
| 1686 | + return new LoadBalancer() { |
| 1687 | + @Override |
| 1688 | + public Status acceptResolvedAddresses(LoadBalancer.ResolvedAddresses resolvedAddresses) { |
| 1689 | + helper.updateBalancingState(ConnectivityState.CONNECTING, new SubchannelPicker() { |
| 1690 | + @Override |
| 1691 | + public PickResult pickSubchannel(PickSubchannelArgs args) { |
| 1692 | + return PickResult.withNoResult("connecting", |
| 1693 | + "Simulated slow TLS handshake with backend"); |
| 1694 | + } |
| 1695 | + }); |
| 1696 | + latch.countDown(); |
| 1697 | + return Status.OK; |
| 1698 | + } |
| 1699 | + |
| 1700 | + @Override |
| 1701 | + public void handleNameResolutionError(Status error) {} |
| 1702 | + |
| 1703 | + @Override |
| 1704 | + public void shutdown() {} |
| 1705 | + }; |
| 1706 | + } |
| 1707 | + }; |
| 1708 | + LoadBalancerRegistry.getDefaultRegistry().register(slowLbProvider); |
| 1709 | + |
| 1710 | + NameResolverProvider customResolverProvider = new NameResolverProvider() { |
| 1711 | + @Override |
| 1712 | + protected boolean isAvailable() { |
| 1713 | + return true; |
| 1714 | + } |
| 1715 | + |
| 1716 | + @Override |
| 1717 | + protected int priority() { |
| 1718 | + return 5; |
| 1719 | + } |
| 1720 | + |
| 1721 | + @Override |
| 1722 | + public String getDefaultScheme() { |
| 1723 | + return "inprocmetricse2e"; |
| 1724 | + } |
| 1725 | + |
| 1726 | + @Override |
| 1727 | + public Collection<Class<? extends SocketAddress>> getProducedSocketAddressTypes() { |
| 1728 | + return Collections.singleton(InProcessSocketAddress.class); |
| 1729 | + } |
| 1730 | + |
| 1731 | + @Override |
| 1732 | + public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) { |
| 1733 | + return new NameResolver() { |
| 1734 | + @Override |
| 1735 | + public String getServiceAuthority() { |
| 1736 | + return "inprocmetricse2e"; |
| 1737 | + } |
| 1738 | + |
| 1739 | + @Override |
| 1740 | + public void start(Listener2 listener) { |
| 1741 | + listener.onResult(ResolutionResult.newBuilder() |
| 1742 | + .setAddresses(Collections.singletonList(new EquivalentAddressGroup( |
| 1743 | + new InProcessSocketAddress("test-metrics-e2e")))) |
| 1744 | + .build()); |
| 1745 | + } |
| 1746 | + |
| 1747 | + @Override |
| 1748 | + public void shutdown() {} |
| 1749 | + }; |
| 1750 | + } |
| 1751 | + }; |
| 1752 | + NameResolverRegistry.getDefaultRegistry().register(customResolverProvider); |
| 1753 | + |
| 1754 | + GrpcOpenTelemetry grpcOpenTelemetry = GrpcOpenTelemetry.newBuilder() |
| 1755 | + .sdk(openTelemetryTesting.getOpenTelemetry()) |
| 1756 | + .enableMetrics(Collections.singleton("grpc.client.attempt.delay.duration")) |
| 1757 | + .build(); |
| 1758 | + |
| 1759 | + InProcessChannelBuilder channelBuilder = |
| 1760 | + InProcessChannelBuilder.forTarget("inprocmetricse2e:///test-metrics-e2e") |
| 1761 | + .defaultLoadBalancingPolicy("slow_metrics_connecting_policy"); |
| 1762 | + grpcOpenTelemetry.configureChannelBuilder(channelBuilder); |
| 1763 | + ManagedChannel channel = channelBuilder.build(); |
| 1764 | + try { |
| 1765 | + ClientCall<String, String> call = channel.newCall(method, CallOptions.DEFAULT); |
| 1766 | + call.start(new ClientCall.Listener<String>() {}, new Metadata()); |
| 1767 | + call.request(1); |
| 1768 | + |
| 1769 | + latch.await(5, TimeUnit.SECONDS); |
| 1770 | + Thread.sleep(50); |
| 1771 | + call.cancel("End test delay segment", null); |
| 1772 | + } finally { |
| 1773 | + channel.shutdownNow(); |
| 1774 | + channel.awaitTermination(5, TimeUnit.SECONDS); |
| 1775 | + LoadBalancerRegistry.getDefaultRegistry().deregister(slowLbProvider); |
| 1776 | + NameResolverRegistry.getDefaultRegistry().deregister(customResolverProvider); |
| 1777 | + } |
| 1778 | + |
| 1779 | + assertThat(openTelemetryTesting.getMetrics()) |
| 1780 | + .anySatisfy( |
| 1781 | + metric -> assertThat(metric) |
| 1782 | + .hasName("grpc.client.attempt.delay.duration") |
| 1783 | + .hasHistogramSatisfying( |
| 1784 | + histogram -> histogram.hasPointsSatisfying( |
| 1785 | + point -> { |
| 1786 | + point.hasAttribute(METHOD_KEY, method.getFullMethodName()); |
| 1787 | + point.hasAttribute(TARGET_KEY, "inprocmetricse2e:///test-metrics-e2e"); |
| 1788 | + point.hasAttribute( |
| 1789 | + AttributeKey.stringKey("grpc.delay_type"), "connecting"); |
| 1790 | + }))); |
| 1791 | + } |
| 1792 | + |
1651 | 1793 | @Test |
1652 | 1794 | public void clientAttemptDelayStart_featureFlagDisabled_zeroMetrics() { |
1653 | 1795 | System.setProperty("GRPC_EXPERIMENTAL_ENABLE_DELAY_OBSERVABILITY", "false"); |
|
0 commit comments