From efb440097afafbdde3d7def73dac73338ca534c2 Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Fri, 5 Jul 2024 14:24:30 -0400 Subject: [PATCH] Moves gRPC tracing support into a new module. Creates a new module for testing gRPC. Signed-off-by: Santiago Pericas-Geertsen --- all/pom.xml | 4 + bom/pom.xml | 5 + microprofile/grpc/pom.xml | 2 + .../grpc/server/GrpcMpCdiExtension.java | 4 +- .../grpc/server/package-info.java | 2 +- .../server/src/main/java/module-info.java | 2 +- .../grpc/server/GrpcMpExtensionTest.java | 3 + microprofile/grpc/tests/pom.xml | 103 ++++++++++++++++++ .../microprofile/grpc/tests/package-info.java | 20 ++++ .../grpc/tests/src/main/java/module-info.java | 18 +++ .../grpc/tests}/BaseServiceTest.java | 3 +- .../grpc/tests}/EchoServiceTest.java | 10 +- .../grpc/tests}/HashServiceTest.java | 16 +-- .../grpc/tests}/RandomServiceTest.java | 20 ++-- .../grpc/tests}/StringServiceTest.java | 28 ++--- .../src/test/proto/echo.proto | 2 +- .../src/test/proto/hash.proto | 2 +- .../src/test/proto/random.proto | 2 +- .../src/test/proto/services.proto | 2 +- .../src/test/proto/strings.proto | 2 +- .../src/test/resources/META-INF/beans.xml | 0 .../io.helidon.grpc.core.MarshallerSupplier | 17 +++ .../src/test/resources/application.yaml | 4 - .../src/test/resources/client.p12 | Bin .../src/test/resources/logging.properties | 0 .../src/test/resources/server.p12 | Bin microprofile/grpc/tracing/pom.xml | 45 ++++++++ .../grpc/tracing/GrpcMpTracingExtension.java | 46 ++++++++ .../grpc/tracing/package-info.java | 20 ++++ .../tracing/src/main/java/module-info.java | 36 ++++++ .../helidon/webserver/grpc/GrpcRouting.java | 6 +- .../webserver/grpc/GrpcServiceRoute.java | 12 +- .../grpc/GrpcTracingConfigBlueprint.java | 2 +- .../grpc/GrpcTracingInterceptor.java | 2 +- 34 files changed, 373 insertions(+), 67 deletions(-) create mode 100644 microprofile/grpc/tests/pom.xml create mode 100644 microprofile/grpc/tests/src/main/java/io/helidon/microprofile/grpc/tests/package-info.java create mode 100644 microprofile/grpc/tests/src/main/java/module-info.java rename microprofile/grpc/{server/src/test/java/io/helidon/microprofile/grpc/server => tests/src/test/java/io/helidon/microprofile/grpc/tests}/BaseServiceTest.java (93%) rename microprofile/grpc/{server/src/test/java/io/helidon/microprofile/grpc/server => tests/src/test/java/io/helidon/microprofile/grpc/tests}/EchoServiceTest.java (93%) rename microprofile/grpc/{server/src/test/java/io/helidon/microprofile/grpc/server => tests/src/test/java/io/helidon/microprofile/grpc/tests}/HashServiceTest.java (79%) rename microprofile/grpc/{server/src/test/java/io/helidon/microprofile/grpc/server => tests/src/test/java/io/helidon/microprofile/grpc/tests}/RandomServiceTest.java (79%) rename microprofile/grpc/{server/src/test/java/io/helidon/microprofile/grpc/server => tests/src/test/java/io/helidon/microprofile/grpc/tests}/StringServiceTest.java (87%) rename microprofile/grpc/{server => tests}/src/test/proto/echo.proto (92%) rename microprofile/grpc/{server => tests}/src/test/proto/hash.proto (92%) rename microprofile/grpc/{server => tests}/src/test/proto/random.proto (92%) rename microprofile/grpc/{server => tests}/src/test/proto/services.proto (96%) rename microprofile/grpc/{server => tests}/src/test/proto/strings.proto (93%) rename microprofile/grpc/{server => tests}/src/test/resources/META-INF/beans.xml (100%) create mode 100644 microprofile/grpc/tests/src/test/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier rename microprofile/grpc/{server => tests}/src/test/resources/application.yaml (96%) rename microprofile/grpc/{server => tests}/src/test/resources/client.p12 (100%) rename microprofile/grpc/{server => tests}/src/test/resources/logging.properties (100%) rename microprofile/grpc/{server => tests}/src/test/resources/server.p12 (100%) create mode 100644 microprofile/grpc/tracing/pom.xml create mode 100644 microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/GrpcMpTracingExtension.java create mode 100644 microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/package-info.java create mode 100644 microprofile/grpc/tracing/src/main/java/module-info.java diff --git a/all/pom.xml b/all/pom.xml index 1e1b2eac1a8..96545976615 100644 --- a/all/pom.xml +++ b/all/pom.xml @@ -293,6 +293,10 @@ io.helidon.microprofile.grpc helidon-microprofile-grpc-server + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-tracing + io.helidon.metrics helidon-metrics diff --git a/bom/pom.xml b/bom/pom.xml index 8b405bff2bf..48b866d67b6 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -115,6 +115,11 @@ helidon-microprofile-grpc-server ${helidon.version} + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-tracing + ${helidon.version} + io.helidon.integrations.micronaut diff --git a/microprofile/grpc/pom.xml b/microprofile/grpc/pom.xml index b20900701df..71f807849d6 100644 --- a/microprofile/grpc/pom.xml +++ b/microprofile/grpc/pom.xml @@ -35,6 +35,8 @@ api core server + tracing + tests diff --git a/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/GrpcMpCdiExtension.java b/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/GrpcMpCdiExtension.java index bf92d0fa122..b4c557f9ede 100644 --- a/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/GrpcMpCdiExtension.java +++ b/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/GrpcMpCdiExtension.java @@ -28,7 +28,6 @@ import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.webserver.grpc.GrpcRouting; import io.helidon.webserver.grpc.GrpcService; -import io.helidon.webserver.grpc.GrpcTracingConfig; import io.grpc.BindableService; import jakarta.enterprise.context.ApplicationScoped; @@ -122,8 +121,7 @@ private boolean hasGrpcQualifier(Bean bean) { private void register(Object service, GrpcRouting.Builder builder, Class cls, BeanManager beanManager) { GrpcServiceBuilder serviceBuilder = GrpcServiceBuilder.create(cls, () -> service, beanManager); if (serviceBuilder.isAnnotatedService()) { - GrpcTracingConfig tracingConfig = GrpcTracingConfig.create(config.get("tracing.grpc")); - builder.service(serviceBuilder.build(), tracingConfig); + builder.service(serviceBuilder.build()); } else { LOGGER.log(Level.WARNING, () -> "Discovered type is not a properly annotated gRPC service " + service.getClass()); diff --git a/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/package-info.java b/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/package-info.java index fbcd798127a..746be971a1c 100644 --- a/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/package-info.java +++ b/microprofile/grpc/server/src/main/java/io/helidon/microprofile/grpc/server/package-info.java @@ -15,6 +15,6 @@ */ /** - * Microprofile 1.0 gRPC server implementation. + * Microprofile gRPC server implementation. */ package io.helidon.microprofile.grpc.server; diff --git a/microprofile/grpc/server/src/main/java/module-info.java b/microprofile/grpc/server/src/main/java/module-info.java index ae70c3c90c1..d52cc2e279e 100644 --- a/microprofile/grpc/server/src/main/java/module-info.java +++ b/microprofile/grpc/server/src/main/java/module-info.java @@ -22,7 +22,7 @@ * gRPC microprofile server module */ @Feature(value = "gRPC", - description = "Helidon MP gRPC implementation", + description = "Helidon gRPC MP server", in = HelidonFlavor.MP, path = "gRPC" ) diff --git a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/GrpcMpExtensionTest.java b/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/GrpcMpExtensionTest.java index a4995093904..6a352685041 100644 --- a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/GrpcMpExtensionTest.java +++ b/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/GrpcMpExtensionTest.java @@ -21,6 +21,7 @@ import io.helidon.microprofile.grpc.server.spi.GrpcMpContext; import io.helidon.microprofile.grpc.server.spi.GrpcMpExtension; +import io.helidon.microprofile.testing.junit5.AddBean; import io.helidon.microprofile.testing.junit5.AddExtension; import io.helidon.microprofile.testing.junit5.HelidonTest; @@ -32,6 +33,8 @@ @HelidonTest @AddExtension(GrpcMpCdiExtension.class) +@AddBean(GrpcMpExtensionTest.Extension1.class) +@AddBean(GrpcMpExtensionTest.Extension2.class) public class GrpcMpExtensionTest { private static final Set> EXTENSIONS_LOADED = new HashSet<>(); diff --git a/microprofile/grpc/tests/pom.xml b/microprofile/grpc/tests/pom.xml new file mode 100644 index 00000000000..54c8bc1e788 --- /dev/null +++ b/microprofile/grpc/tests/pom.xml @@ -0,0 +1,103 @@ + + + + + 4.0.0 + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-project + 4.1.0-SNAPSHOT + + + helidon-microprofile-grpc-tests + Helidon Microprofile gRPC Tests + + + + io.helidon.tracing + helidon-tracing + test + + + io.helidon.grpc + helidon-grpc-core + test + + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-core + test + + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-server + test + + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-tracing + test + + + io.helidon.microprofile.testing + helidon-microprofile-testing-junit5 + test + + + io.helidon.webserver.testing.junit5 + helidon-webserver-testing-junit5-grpc + test + + + + javax.annotation + javax.annotation-api + provided + true + + + + + + + kr.motd.maven + os-maven-plugin + ${version.plugin.os} + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + + + + test-compile + test-compile-custom + + + + + + + \ No newline at end of file diff --git a/microprofile/grpc/tests/src/main/java/io/helidon/microprofile/grpc/tests/package-info.java b/microprofile/grpc/tests/src/main/java/io/helidon/microprofile/grpc/tests/package-info.java new file mode 100644 index 00000000000..eae816f2a8e --- /dev/null +++ b/microprofile/grpc/tests/src/main/java/io/helidon/microprofile/grpc/tests/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * gRPC tests module. + */ +package io.helidon.microprofile.grpc.tests; diff --git a/microprofile/grpc/tests/src/main/java/module-info.java b/microprofile/grpc/tests/src/main/java/module-info.java new file mode 100644 index 00000000000..28831699287 --- /dev/null +++ b/microprofile/grpc/tests/src/main/java/module-info.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +module helidon.microprofile.grpc.tests { +} \ No newline at end of file diff --git a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/BaseServiceTest.java b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/BaseServiceTest.java similarity index 93% rename from microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/BaseServiceTest.java rename to microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/BaseServiceTest.java index 54dc7396838..164213e11a8 100644 --- a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/BaseServiceTest.java +++ b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/BaseServiceTest.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package io.helidon.microprofile.grpc.server; +package io.helidon.microprofile.grpc.tests; import io.helidon.common.configurable.Resource; import io.helidon.common.tls.Tls; +import io.helidon.microprofile.grpc.server.GrpcMpCdiExtension; import io.helidon.microprofile.testing.junit5.AddExtension; import io.helidon.microprofile.testing.junit5.HelidonTest; import io.helidon.webclient.grpc.GrpcClient; diff --git a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/EchoServiceTest.java b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/EchoServiceTest.java similarity index 93% rename from microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/EchoServiceTest.java rename to microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/EchoServiceTest.java index 8017cbfaca3..0d9bfff2959 100644 --- a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/EchoServiceTest.java +++ b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/EchoServiceTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.microprofile.grpc.server; +package io.helidon.microprofile.grpc.tests; import java.lang.annotation.Retention; import java.lang.annotation.Target; @@ -27,8 +27,8 @@ import io.helidon.microprofile.grpc.api.GrpcInterceptorBinding; import io.helidon.microprofile.grpc.api.GrpcInterceptors; import io.helidon.microprofile.grpc.api.Unary; -import io.helidon.microprofile.grpc.server.test.Echo; -import io.helidon.microprofile.grpc.server.test.EchoServiceGrpc; +import io.helidon.microprofile.grpc.tests.test.Echo; +import io.helidon.microprofile.grpc.tests.test.EchoServiceGrpc; import io.helidon.tracing.Tracer; import io.grpc.Context; @@ -79,8 +79,8 @@ private Echo.EchoRequest fromString(String value) { } /** - * A service that is annotated by {@link Grpc}. Should be discovered by - * {@link GrpcMpCdiExtension}. References two interceptors, one directly + * A service that is annotated by {@link io.helidon.microprofile.grpc.api.Grpc}. Should be discovered by + * {@link io.helidon.microprofile.grpc.server.GrpcMpCdiExtension}. References two interceptors, one directly * and one via an interceptor binding. */ @Grpc diff --git a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/HashServiceTest.java b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/HashServiceTest.java similarity index 79% rename from microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/HashServiceTest.java rename to microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/HashServiceTest.java index e318c8895e7..d7c8d6e89e3 100644 --- a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/HashServiceTest.java +++ b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/HashServiceTest.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package io.helidon.microprofile.grpc.server; +package io.helidon.microprofile.grpc.tests; -import io.helidon.microprofile.grpc.server.test.Hash; -import io.helidon.microprofile.grpc.server.test.HashServiceGrpc; +import io.helidon.grpc.core.ResponseHelper; +import io.helidon.microprofile.grpc.tests.test.Hash; +import io.helidon.microprofile.grpc.tests.test.HashServiceGrpc; import io.helidon.webserver.grpc.GrpcService; import com.google.protobuf.Descriptors; @@ -25,10 +26,9 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.client.WebTarget; +import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Test; -import static io.helidon.grpc.core.ResponseHelper.complete; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.is; class HashServiceTest extends BaseServiceTest { @@ -42,12 +42,12 @@ public HashServiceTest(WebTarget webTarget) { void testHash() { HashServiceGrpc.HashServiceBlockingStub service = HashServiceGrpc.newBlockingStub(grpcClient().channel()); Hash.Value res = service.hash(Hash.Message.newBuilder().setText("hello world").build()); - assertThat(res.getText(), is(String.valueOf("hello world".hashCode()))); + MatcherAssert.assertThat(res.getText(), is(String.valueOf("hello world".hashCode()))); } /** * A service that implements the {@link GrpcService} interface. Should be - * discovered by {@link GrpcMpCdiExtension}. + * discovered by {@link io.helidon.microprofile.grpc.server.GrpcMpCdiExtension}. */ @ApplicationScoped public static class HashService implements GrpcService { @@ -62,7 +62,7 @@ public void update(Routing routing) { } public void hash(Hash.Message request, StreamObserver observer) { - complete(observer, Hash.Value.newBuilder().setText( + ResponseHelper.complete(observer, Hash.Value.newBuilder().setText( String.valueOf(request.getText().hashCode())).build()); } } diff --git a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/RandomServiceTest.java b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/RandomServiceTest.java similarity index 79% rename from microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/RandomServiceTest.java rename to microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/RandomServiceTest.java index c5c3cacf417..d0f54846d49 100644 --- a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/RandomServiceTest.java +++ b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/RandomServiceTest.java @@ -14,22 +14,21 @@ * limitations under the License. */ -package io.helidon.microprofile.grpc.server; +package io.helidon.microprofile.grpc.tests; -import io.grpc.stub.StreamObserver; - -import io.helidon.microprofile.grpc.server.test.Random; -import io.helidon.microprofile.grpc.server.test.RandomServiceGrpc; +import io.helidon.grpc.core.ResponseHelper; +import io.helidon.microprofile.grpc.tests.test.Random; +import io.helidon.microprofile.grpc.tests.test.RandomServiceGrpc; +import io.grpc.stub.StreamObserver; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.client.WebTarget; +import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Test; -import static io.helidon.grpc.core.ResponseHelper.complete; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.lessThan; -import static org.hamcrest.MatcherAssert.assertThat; class RandomServiceTest extends BaseServiceTest { @@ -43,13 +42,12 @@ void testRandom() { RandomServiceGrpc.RandomServiceBlockingStub service = RandomServiceGrpc.newBlockingStub(grpcClient().channel()); int seed = (int) System.currentTimeMillis(); Random.IntValue res = service.random(Random.Seed.newBuilder().setN(seed).build()); - ; - assertThat(res.getN(), is(lessThan(1000))); + MatcherAssert.assertThat(res.getN(), is(lessThan(1000))); } /** * A service that implements the {@link io.grpc.BindableService} interface. Should be - * discovered by {@link GrpcMpCdiExtension}. + * discovered by {@link io.helidon.microprofile.grpc.server.GrpcMpCdiExtension}. */ @ApplicationScoped public static class RandomService implements io.grpc.BindableService, RandomServiceGrpc.AsyncService { @@ -64,7 +62,7 @@ public void random(Random.Seed request, StreamObserver observer int seed = request.getN(); java.util.Random random = new java.util.Random(); random.setSeed(seed); - complete(observer, Random.IntValue.newBuilder().setN(random.nextInt(1000)).build()); + ResponseHelper.complete(observer, Random.IntValue.newBuilder().setN(random.nextInt(1000)).build()); } } } diff --git a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/StringServiceTest.java b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/StringServiceTest.java similarity index 87% rename from microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/StringServiceTest.java rename to microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/StringServiceTest.java index 90925b8746a..1f7742aeaf7 100644 --- a/microprofile/grpc/server/src/test/java/io/helidon/microprofile/grpc/server/StringServiceTest.java +++ b/microprofile/grpc/tests/src/test/java/io/helidon/microprofile/grpc/tests/StringServiceTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.microprofile.grpc.server; +package io.helidon.microprofile.grpc.tests; import java.util.Iterator; import java.util.concurrent.CompletableFuture; @@ -25,22 +25,22 @@ import java.util.stream.Stream; import io.helidon.grpc.core.CollectingObserver; +import io.helidon.grpc.core.ResponseHelper; import io.helidon.microprofile.grpc.api.Bidirectional; import io.helidon.microprofile.grpc.api.ClientStreaming; import io.helidon.microprofile.grpc.api.Grpc; import io.helidon.microprofile.grpc.api.ServerStreaming; import io.helidon.microprofile.grpc.api.Unary; -import io.helidon.microprofile.grpc.server.test.StringServiceGrpc; -import io.helidon.microprofile.grpc.server.test.Strings; +import io.helidon.microprofile.grpc.tests.test.StringServiceGrpc; +import io.helidon.microprofile.grpc.tests.test.Strings; import io.grpc.stub.StreamObserver; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.client.WebTarget; +import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Test; -import static io.helidon.grpc.core.ResponseHelper.complete; -import static io.helidon.grpc.core.ResponseHelper.stream; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -56,22 +56,22 @@ public StringServiceTest(WebTarget webTarget) { void testUnaryUpper() { StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(grpcClient().channel()); Strings.StringMessage res = service.upper(newStringMessage("hello")); - assertThat(res.getText(), is("HELLO")); + MatcherAssert.assertThat(res.getText(), is("HELLO")); } @Test void testUnaryLower() { StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(grpcClient().channel()); Strings.StringMessage res = service.lower(newStringMessage("HELLO")); - assertThat(res.getText(), is("hello")); + MatcherAssert.assertThat(res.getText(), is("hello")); } @Test void testServerStreamingSplit() { StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(grpcClient().channel()); Iterator res = service.split(newStringMessage("hello world")); - assertThat(res.next().getText(), is("hello")); - assertThat(res.next().getText(), is("world")); + MatcherAssert.assertThat(res.next().getText(), is("hello")); + MatcherAssert.assertThat(res.next().getText(), is("world")); assertThat(res.hasNext(), is(false)); } @@ -84,7 +84,7 @@ void testClientStreamingJoinAsync() throws ExecutionException, InterruptedExcept req.onNext(newStringMessage("world")); req.onCompleted(); Strings.StringMessage res = future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); - assertThat(res.getText(), is("hello world")); + MatcherAssert.assertThat(res.getText(), is("hello world")); } @Test @@ -95,7 +95,7 @@ void testBidirectionalEcho() throws ExecutionException, InterruptedException, Ti req.onNext(newStringMessage("hello")); req.onCompleted(); Strings.StringMessage res = future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); - assertThat(res.getText(), is("hello")); + MatcherAssert.assertThat(res.getText(), is("hello")); } Strings.StringMessage newStringMessage(String data) { @@ -108,18 +108,18 @@ public static class StringService { @Unary(name = "Upper") public void upper(Strings.StringMessage request, StreamObserver observer) { - complete(observer, response(request.getText().toUpperCase())); + ResponseHelper.complete(observer, response(request.getText().toUpperCase())); } @Unary(name = "Lower") public void lower(Strings.StringMessage request, StreamObserver observer) { - complete(observer, response(request.getText().toLowerCase())); + ResponseHelper.complete(observer, response(request.getText().toLowerCase())); } @ServerStreaming(name = "Split") public void split(Strings.StringMessage request, StreamObserver observer) { String[] parts = request.getText().split(" "); - stream(observer, Stream.of(parts).map(this::response)); + ResponseHelper.stream(observer, Stream.of(parts).map(this::response)); } @ClientStreaming(name = "Join") diff --git a/microprofile/grpc/server/src/test/proto/echo.proto b/microprofile/grpc/tests/src/test/proto/echo.proto similarity index 92% rename from microprofile/grpc/server/src/test/proto/echo.proto rename to microprofile/grpc/tests/src/test/proto/echo.proto index af0d4c50d0d..c137657f5db 100644 --- a/microprofile/grpc/server/src/test/proto/echo.proto +++ b/microprofile/grpc/tests/src/test/proto/echo.proto @@ -15,7 +15,7 @@ */ syntax = "proto3"; -option java_package = "io.helidon.microprofile.grpc.server.test"; +option java_package = "io.helidon.microprofile.grpc.tests.test"; service EchoService { rpc Echo (EchoRequest) returns (EchoResponse) {} diff --git a/microprofile/grpc/server/src/test/proto/hash.proto b/microprofile/grpc/tests/src/test/proto/hash.proto similarity index 92% rename from microprofile/grpc/server/src/test/proto/hash.proto rename to microprofile/grpc/tests/src/test/proto/hash.proto index 8e823ec2948..e35fdacdad3 100644 --- a/microprofile/grpc/server/src/test/proto/hash.proto +++ b/microprofile/grpc/tests/src/test/proto/hash.proto @@ -16,7 +16,7 @@ syntax = "proto3"; -option java_package = "io.helidon.microprofile.grpc.server.test"; +option java_package = "io.helidon.microprofile.grpc.tests.test"; service HashService { rpc Hash (Message) returns (Value) {} diff --git a/microprofile/grpc/server/src/test/proto/random.proto b/microprofile/grpc/tests/src/test/proto/random.proto similarity index 92% rename from microprofile/grpc/server/src/test/proto/random.proto rename to microprofile/grpc/tests/src/test/proto/random.proto index e1cfb50eb65..c6067520599 100644 --- a/microprofile/grpc/server/src/test/proto/random.proto +++ b/microprofile/grpc/tests/src/test/proto/random.proto @@ -16,7 +16,7 @@ syntax = "proto3"; -option java_package = "io.helidon.microprofile.grpc.server.test"; +option java_package = "io.helidon.microprofile.grpc.tests.test"; service RandomService { rpc Random (Seed) returns (IntValue) {} diff --git a/microprofile/grpc/server/src/test/proto/services.proto b/microprofile/grpc/tests/src/test/proto/services.proto similarity index 96% rename from microprofile/grpc/server/src/test/proto/services.proto rename to microprofile/grpc/tests/src/test/proto/services.proto index 185dce004e3..8883df0692d 100644 --- a/microprofile/grpc/server/src/test/proto/services.proto +++ b/microprofile/grpc/tests/src/test/proto/services.proto @@ -15,7 +15,7 @@ */ syntax = "proto3"; -option java_package = "io.helidon.microprofile.grpc.server.test"; +option java_package = "io.helidon.microprofile.grpc.tests.test"; service UnaryService { rpc requestResponse (TestRequest) returns (TestResponse) {} diff --git a/microprofile/grpc/server/src/test/proto/strings.proto b/microprofile/grpc/tests/src/test/proto/strings.proto similarity index 93% rename from microprofile/grpc/server/src/test/proto/strings.proto rename to microprofile/grpc/tests/src/test/proto/strings.proto index 35caba68766..54d5148d18e 100644 --- a/microprofile/grpc/server/src/test/proto/strings.proto +++ b/microprofile/grpc/tests/src/test/proto/strings.proto @@ -16,7 +16,7 @@ syntax = "proto3"; -option java_package = "io.helidon.microprofile.grpc.server.test"; +option java_package = "io.helidon.microprofile.grpc.tests.test"; service StringService { rpc Upper (StringMessage) returns (StringMessage) {} diff --git a/microprofile/grpc/server/src/test/resources/META-INF/beans.xml b/microprofile/grpc/tests/src/test/resources/META-INF/beans.xml similarity index 100% rename from microprofile/grpc/server/src/test/resources/META-INF/beans.xml rename to microprofile/grpc/tests/src/test/resources/META-INF/beans.xml diff --git a/microprofile/grpc/tests/src/test/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier b/microprofile/grpc/tests/src/test/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier new file mode 100644 index 00000000000..d3f65bf900e --- /dev/null +++ b/microprofile/grpc/tests/src/test/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier @@ -0,0 +1,17 @@ +# +# Copyright (c) 2019, 2024 Oracle and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +io.helidon.microprofile.grpc.tests.StubMarshaller$Supplier diff --git a/microprofile/grpc/server/src/test/resources/application.yaml b/microprofile/grpc/tests/src/test/resources/application.yaml similarity index 96% rename from microprofile/grpc/server/src/test/resources/application.yaml rename to microprofile/grpc/tests/src/test/resources/application.yaml index 7cda6180804..00b3aef2e03 100644 --- a/microprofile/grpc/server/src/test/resources/application.yaml +++ b/microprofile/grpc/tests/src/test/resources/application.yaml @@ -28,7 +28,3 @@ server: passphrase: "password" resource: resource-path: "server.p12" - -tracing: - grpc: - enabled: true diff --git a/microprofile/grpc/server/src/test/resources/client.p12 b/microprofile/grpc/tests/src/test/resources/client.p12 similarity index 100% rename from microprofile/grpc/server/src/test/resources/client.p12 rename to microprofile/grpc/tests/src/test/resources/client.p12 diff --git a/microprofile/grpc/server/src/test/resources/logging.properties b/microprofile/grpc/tests/src/test/resources/logging.properties similarity index 100% rename from microprofile/grpc/server/src/test/resources/logging.properties rename to microprofile/grpc/tests/src/test/resources/logging.properties diff --git a/microprofile/grpc/server/src/test/resources/server.p12 b/microprofile/grpc/tests/src/test/resources/server.p12 similarity index 100% rename from microprofile/grpc/server/src/test/resources/server.p12 rename to microprofile/grpc/tests/src/test/resources/server.p12 diff --git a/microprofile/grpc/tracing/pom.xml b/microprofile/grpc/tracing/pom.xml new file mode 100644 index 00000000000..ecfc63cdb6d --- /dev/null +++ b/microprofile/grpc/tracing/pom.xml @@ -0,0 +1,45 @@ + + + + + 4.0.0 + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-project + 4.1.0-SNAPSHOT + + + helidon-microprofile-grpc-tracing + Helidon Microprofile gRPC Tracing + + + + io.helidon.config + helidon-config-mp + + + io.helidon.webserver + helidon-webserver-grpc + + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-server + + + \ No newline at end of file diff --git a/microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/GrpcMpTracingExtension.java b/microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/GrpcMpTracingExtension.java new file mode 100644 index 00000000000..2d9f9e80b84 --- /dev/null +++ b/microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/GrpcMpTracingExtension.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.microprofile.grpc.tracing; + +import io.helidon.config.Config; +import io.helidon.config.mp.MpConfig; +import io.helidon.microprofile.grpc.server.spi.GrpcMpContext; +import io.helidon.microprofile.grpc.server.spi.GrpcMpExtension; +import io.helidon.tracing.Tracer; +import io.helidon.webserver.grpc.GrpcTracingConfig; +import io.helidon.webserver.grpc.GrpcTracingInterceptor; + +import org.eclipse.microprofile.config.ConfigProvider; + +/** + * gRPC MP extension that adds the tracing interceptor if configured. + */ +public class GrpcMpTracingExtension implements GrpcMpExtension { + + private static final String GRPC_TRACING_ROOT = "tracing.grpc"; + + @Override + public void configure(GrpcMpContext context) { + Config config = MpConfig.toHelidonConfig(ConfigProvider.getConfig()); + GrpcTracingConfig tracingConfig = GrpcTracingConfig.create(config.get(GRPC_TRACING_ROOT)); + if (tracingConfig.enabled()) { + Tracer tracer = Tracer.global(); + GrpcTracingInterceptor interceptor = GrpcTracingInterceptor.create(tracer, tracingConfig); + context.routing().intercept(interceptor); + } + } +} diff --git a/microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/package-info.java b/microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/package-info.java new file mode 100644 index 00000000000..9215d0c52bc --- /dev/null +++ b/microprofile/grpc/tracing/src/main/java/io/helidon/microprofile/grpc/tracing/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Microprofile gRPC tracing implementation. + */ +package io.helidon.microprofile.grpc.tracing; diff --git a/microprofile/grpc/tracing/src/main/java/module-info.java b/microprofile/grpc/tracing/src/main/java/module-info.java new file mode 100644 index 00000000000..b44377ca2f2 --- /dev/null +++ b/microprofile/grpc/tracing/src/main/java/module-info.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import io.helidon.common.features.api.Feature; +import io.helidon.common.features.api.HelidonFlavor; + +@Feature(value = "gRPC Tracing", + description = "Helidon gRPC MP Tracing", + in = HelidonFlavor.MP, + path = {"gRPC", "Tracing"} +) +module helidon.microprofile.grpc.tracing { + + requires io.helidon.config; + requires io.helidon.config.mp; + requires io.helidon.webserver.grpc; + requires io.helidon.microprofile.grpc.server; + requires io.helidon.tracing; + requires io.helidon.common.features.api; + + provides io.helidon.microprofile.grpc.server.spi.GrpcMpExtension + with io.helidon.microprofile.grpc.tracing.GrpcMpTracingExtension; +} \ No newline at end of file diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java index fd5acc057ff..277075340d3 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcRouting.java @@ -133,6 +133,7 @@ private Builder() { @Override public GrpcRouting build() { + services.values().forEach(service -> route(GrpcServiceRoute.create(service, interceptors))); return new GrpcRouting(this); } @@ -160,16 +161,15 @@ public Builder service(BindableService service) { * Configure a service using a {@link io.grpc.ServiceDescriptor}. * * @param service service to add - * @param tracingConfig tracing configuration * @return updated builder */ - public Builder service(GrpcServiceDescriptor service, GrpcTracingConfig tracingConfig) { + public Builder service(GrpcServiceDescriptor service) { String name = service.name(); if (services.containsKey(name)) { throw new IllegalArgumentException("Attempted to register service name " + name + " multiple times"); } services.put(name, service); - return route(GrpcServiceRoute.create(service, tracingConfig)); + return this; } /** diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java index 0c6ca7b2c07..e7bef980539 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcServiceRoute.java @@ -22,7 +22,6 @@ import io.helidon.grpc.core.WeightedBag; import io.helidon.http.HttpPrologue; import io.helidon.http.PathMatchers; -import io.helidon.tracing.Tracer; import com.google.protobuf.Descriptors; import io.grpc.BindableService; @@ -71,19 +70,14 @@ static GrpcRoute create(BindableService service) { /** * Creates a gRPC route for an instance CDI bean annotated with {@link @Grpc}. - * Registers global interceptors for context and tracing on all the routes. + * Registers global interceptors for context on all the routes. * * @param service the service - * @param tracingConfig tracing configuration + * @param interceptors interceptor bag * @return the route */ - static GrpcRoute create(GrpcServiceDescriptor service, GrpcTracingConfig tracingConfig) { - WeightedBag interceptors = WeightedBag.create(); + static GrpcRoute create(GrpcServiceDescriptor service, WeightedBag interceptors) { interceptors.add(ContextSettingServerInterceptor.create()); - if (tracingConfig.enabled()) { - Tracer tracer = Tracer.global(); - interceptors.add(GrpcTracingInterceptor.create(tracer, tracingConfig)); - } return create(BindableServiceImpl.create(service, interceptors)); } diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingConfigBlueprint.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingConfigBlueprint.java index 81c93847888..0b41953f233 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingConfigBlueprint.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingConfigBlueprint.java @@ -30,7 +30,7 @@ interface GrpcTracingConfigBlueprint { * A flag indicating if tracing is enabled. */ @Option.Configured - @Option.DefaultBoolean(false) + @Option.DefaultBoolean(true) boolean enabled(); /** diff --git a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingInterceptor.java b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingInterceptor.java index 196e14ba0d5..a7301be511f 100644 --- a/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingInterceptor.java +++ b/webserver/grpc/src/main/java/io/helidon/webserver/grpc/GrpcTracingInterceptor.java @@ -88,7 +88,7 @@ private GrpcTracingInterceptor(Tracer tracer, GrpcTracingConfig tracingConfig) { * @param config the tracing configuration * @return a {@link GrpcTracingInterceptor} interceptor instance */ - static GrpcTracingInterceptor create(Tracer tracer, GrpcTracingConfig config) { + public static GrpcTracingInterceptor create(Tracer tracer, GrpcTracingConfig config) { return new GrpcTracingInterceptor(tracer, config); }