2626import io .grpc .ManagedChannel ;
2727import io .grpc .ManagedChannelBuilder ;
2828import io .grpc .opentelemetry .GrpcOpenTelemetry ;
29+ import io .grpc .testing .GrpcCleanupRule ;
2930import io .grpc .testing .protobuf .SimpleRequest ;
3031import io .grpc .testing .protobuf .SimpleServiceGrpc ;
3132import io .opentelemetry .api .OpenTelemetry ;
4445
4546/**
4647 * xDS + OpenTelemetry E2E integration test using a fake control plane.
48+ * This class is skipped from Bazel builds because Bazel doesn't compile the
49+ * grpc-opentelemetry module.
4750 */
4851@ RunWith (Parameterized .class )
4952public class FakeControlPlaneXdsOtelIntegrationTest {
5053
5154 @ Rule (order = 0 )
52- public ControlPlaneRule controlPlane = new ControlPlaneRule ();
55+ public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule ();
5356 @ Rule (order = 1 )
54- public DataPlaneRule dataPlane = new DataPlaneRule ( controlPlane );
57+ public final ControlPlaneRule controlPlane = new ControlPlaneRule ( );
5558 @ Rule (order = 2 )
59+ public final DataPlaneRule dataPlane = new DataPlaneRule (controlPlane );
60+ @ Rule (order = 3 )
5661 public final FlagResetRule flagResetRule = new FlagResetRule ();
5762
5863 @ Parameters (name = "enableRfc3986UrisParam={0}" )
@@ -62,6 +67,21 @@ public static Iterable<Object[]> data() {
6267
6368 @ Parameter public boolean enableRfc3986UrisParam ;
6469
70+ @ Test
71+ public void testInMemoryMetricReader () {
72+ InMemoryMetricReader metricReader = InMemoryMetricReader .create ();
73+ SdkMeterProvider meterProvider = SdkMeterProvider .builder ()
74+ .registerMetricReader (metricReader )
75+ .build ();
76+ io .opentelemetry .api .metrics .LongCounter counter = meterProvider
77+ .meterBuilder ("test-scope" )
78+ .build ()
79+ .counterBuilder ("test-counter" )
80+ .build ();
81+ counter .add (10 );
82+ assertThat (metricReader .collectAllMetrics ()).isNotEmpty ();
83+ }
84+
6585 @ Before
6686 public void setupRfc3986UrisFeatureFlag () throws Exception {
6787 flagResetRule .setFlagForTest (
@@ -88,32 +108,32 @@ public void configureChannelBuilder(ManagedChannelBuilder<?> builder) {
88108 }
89109 };
90110
91- ManagedChannel channel = Grpc .newChannelBuilder ("test-xds:///test-server" ,
111+ ManagedChannel channel = grpcCleanupRule .register (
112+ Grpc .newChannelBuilder ("test-xds:///test-server" ,
92113 InsecureChannelCredentials .create ())
93114 .childChannelConfigurator (configurator )
94- .build ();
115+ .build ()) ;
95116
96- try {
97- SimpleServiceGrpc .SimpleServiceBlockingStub blockingStub = SimpleServiceGrpc .newBlockingStub (
98- channel );
99- blockingStub .unaryRpc (SimpleRequest .getDefaultInstance ());
117+ SimpleServiceGrpc .SimpleServiceBlockingStub blockingStub =
118+ SimpleServiceGrpc .newBlockingStub (channel );
119+ blockingStub .unaryRpc (SimpleRequest .getDefaultInstance ());
100120
101- boolean hasMetrics = false ;
102- for (int i = 0 ; i < 20 ; i ++) {
103- for (MetricData metric : metricReader .collectAllMetrics ()) {
104- if (metric .getName ().startsWith ("grpc.client." )) {
105- hasMetrics = true ;
106- break ;
107- }
108- }
109- if (hasMetrics ) {
121+ // Verify that OpenTelemetry metrics specifically from the xDS Control Plane ADS stream
122+ // successfully propagated, method name is recorded as 'other' because dynamic descriptors
123+ // are not sampled to local tracing by default.
124+ boolean foundXdsMetrics = false ;
125+ for (int i = 0 ; i < 50 ; i ++) {
126+ for (MetricData metric : metricReader .collectAllMetrics ()) {
127+ if (metric .toString ().contains ("grpc.client." ) && metric .toString ().contains ("other" )) {
128+ foundXdsMetrics = true ;
110129 break ;
111130 }
112- Thread .sleep (100 );
113131 }
114- assertThat (hasMetrics ).isTrue ();
115- } finally {
116- channel .shutdownNow ();
132+ if (foundXdsMetrics ) {
133+ break ;
134+ }
135+ Thread .sleep (100 );
117136 }
137+ assertThat (foundXdsMetrics ).isTrue ();
118138 }
119139}
0 commit comments