|
| 1 | +/* |
| 2 | + * Copyright 2026 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.xds; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import io.grpc.ChannelConfigurator; |
| 22 | +import io.grpc.FlagResetRule; |
| 23 | +import io.grpc.Grpc; |
| 24 | +import io.grpc.InsecureChannelCredentials; |
| 25 | +import io.grpc.InternalFeatureFlags; |
| 26 | +import io.grpc.ManagedChannel; |
| 27 | +import io.grpc.ManagedChannelBuilder; |
| 28 | +import io.grpc.opentelemetry.GrpcOpenTelemetry; |
| 29 | +import io.grpc.testing.protobuf.SimpleRequest; |
| 30 | +import io.grpc.testing.protobuf.SimpleServiceGrpc; |
| 31 | +import io.opentelemetry.api.OpenTelemetry; |
| 32 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 33 | +import io.opentelemetry.sdk.metrics.SdkMeterProvider; |
| 34 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
| 35 | +import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; |
| 36 | +import java.util.Arrays; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Rule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | +import org.junit.runners.Parameterized; |
| 42 | +import org.junit.runners.Parameterized.Parameter; |
| 43 | +import org.junit.runners.Parameterized.Parameters; |
| 44 | + |
| 45 | +/** |
| 46 | + * xDS + OpenTelemetry E2E integration test using a fake control plane. |
| 47 | + * This class is skipped from Bazel builds because Bazel doesn't compile the |
| 48 | + * grpc-opentelemetry module. |
| 49 | + */ |
| 50 | +@RunWith(Parameterized.class) |
| 51 | +public class FakeControlPlaneXdsOtelIntegrationTest { |
| 52 | + |
| 53 | + @Rule(order = 0) |
| 54 | + public ControlPlaneRule controlPlane = new ControlPlaneRule(); |
| 55 | + @Rule(order = 1) |
| 56 | + public DataPlaneRule dataPlane = new DataPlaneRule(controlPlane); |
| 57 | + @Rule(order = 2) |
| 58 | + public final FlagResetRule flagResetRule = new FlagResetRule(); |
| 59 | + |
| 60 | + @Parameters(name = "enableRfc3986UrisParam={0}") |
| 61 | + public static Iterable<Object[]> data() { |
| 62 | + return Arrays.asList(new Object[][] {{true}, {false}}); |
| 63 | + } |
| 64 | + |
| 65 | + @Parameter public boolean enableRfc3986UrisParam; |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setupRfc3986UrisFeatureFlag() throws Exception { |
| 69 | + flagResetRule.setFlagForTest( |
| 70 | + InternalFeatureFlags::setRfc3986UrisEnabled, enableRfc3986UrisParam); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void childChannelConfigurator_passesOtelSdkToChannel_E2E() throws Exception { |
| 75 | + InMemoryMetricReader metricReader = InMemoryMetricReader.create(); |
| 76 | + SdkMeterProvider meterProvider = SdkMeterProvider.builder() |
| 77 | + .registerMetricReader(metricReader) |
| 78 | + .build(); |
| 79 | + OpenTelemetry openTelemetry = OpenTelemetrySdk.builder() |
| 80 | + .setMeterProvider(meterProvider) |
| 81 | + .build(); |
| 82 | + GrpcOpenTelemetry grpcOtel = GrpcOpenTelemetry.newBuilder() |
| 83 | + .sdk(openTelemetry) |
| 84 | + .build(); |
| 85 | + |
| 86 | + ChannelConfigurator configurator = new ChannelConfigurator() { |
| 87 | + @Override |
| 88 | + public void configureChannelBuilder(ManagedChannelBuilder<?> builder) { |
| 89 | + grpcOtel.configureChannelBuilder(builder); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + ManagedChannel channel = Grpc.newChannelBuilder("test-xds:///test-server", |
| 94 | + InsecureChannelCredentials.create()) |
| 95 | + .childChannelConfigurator(configurator) |
| 96 | + .build(); |
| 97 | + |
| 98 | + try { |
| 99 | + SimpleServiceGrpc.SimpleServiceBlockingStub blockingStub = SimpleServiceGrpc.newBlockingStub( |
| 100 | + channel); |
| 101 | + blockingStub.unaryRpc(SimpleRequest.getDefaultInstance()); |
| 102 | + |
| 103 | + boolean hasMetrics = false; |
| 104 | + for (int i = 0; i < 20; i++) { |
| 105 | + for (MetricData metric : metricReader.collectAllMetrics()) { |
| 106 | + if (metric.getName().startsWith("grpc.client.")) { |
| 107 | + hasMetrics = true; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + if (hasMetrics) { |
| 112 | + break; |
| 113 | + } |
| 114 | + Thread.sleep(100); |
| 115 | + } |
| 116 | + assertThat(hasMetrics).isTrue(); |
| 117 | + } finally { |
| 118 | + channel.shutdownNow(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments