diff --git a/src/main/java/io/opentracing/contrib/grpc/ActiveSpanContextSource.java b/src/main/java/io/opentracing/contrib/grpc/ActiveSpanContextSource.java index 24aa27d..50837ea 100644 --- a/src/main/java/io/opentracing/contrib/grpc/ActiveSpanContextSource.java +++ b/src/main/java/io/opentracing/contrib/grpc/ActiveSpanContextSource.java @@ -11,17 +11,18 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import io.opentracing.SpanContext; /** - * An interface that defines how to get the current active span context + * An interface that defines how to get the current active span context. */ public interface ActiveSpanContextSource { /** - * ActiveSpanContextSource implementation that always returns null as the active span context + * ActiveSpanContextSource implementation that always returns null as the active span context. */ ActiveSpanContextSource NONE = new ActiveSpanContextSource() { @Override @@ -32,7 +33,7 @@ public SpanContext getActiveSpanContext() { /** * ActiveSpanContextSource implementation that returns the current span context stored in the GRPC - * context under {@link OpenTracingContextKey} + * context under {@link OpenTracingContextKey}. */ ActiveSpanContextSource GRPC_CONTEXT = new ActiveSpanContextSource() { @Override @@ -42,6 +43,8 @@ public SpanContext getActiveSpanContext() { }; /** + * Retrieves the active {@link SpanContext}. + * * @return the active span context */ SpanContext getActiveSpanContext(); diff --git a/src/main/java/io/opentracing/contrib/grpc/ActiveSpanSource.java b/src/main/java/io/opentracing/contrib/grpc/ActiveSpanSource.java index e673768..1bbe73f 100644 --- a/src/main/java/io/opentracing/contrib/grpc/ActiveSpanSource.java +++ b/src/main/java/io/opentracing/contrib/grpc/ActiveSpanSource.java @@ -11,17 +11,18 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import io.opentracing.Span; /** - * An interface that defines how to get the current active span + * An interface that defines how to get the current active span. */ public interface ActiveSpanSource { /** - * ActiveSpanSource implementation that always returns null as the active span + * ActiveSpanSource implementation that always returns null as the active span. */ ActiveSpanSource NONE = new ActiveSpanSource() { @Override @@ -32,7 +33,7 @@ public Span getActiveSpan() { /** * ActiveSpanSource implementation that returns the current span stored in the GRPC context under - * {@link OpenTracingContextKey} + * {@link OpenTracingContextKey}. */ ActiveSpanSource GRPC_CONTEXT = new ActiveSpanSource() { @Override @@ -42,7 +43,9 @@ public Span getActiveSpan() { }; /** + * Retieves the active {@link Span}. + * * @return the active span */ Span getActiveSpan(); -} \ No newline at end of file +} diff --git a/src/main/java/io/opentracing/contrib/grpc/ClientCloseDecorator.java b/src/main/java/io/opentracing/contrib/grpc/ClientCloseDecorator.java index 5cba051..a45b36b 100644 --- a/src/main/java/io/opentracing/contrib/grpc/ClientCloseDecorator.java +++ b/src/main/java/io/opentracing/contrib/grpc/ClientCloseDecorator.java @@ -22,14 +22,12 @@ public interface ClientCloseDecorator { /** - * The method of the implementation is executed inside {@link ForwardingClientCallListener#onClose(Status, - * Metadata)}. + * The method of the implementation is executed inside {@link + * ForwardingClientCallListener#onClose}. * * @param span The span created by {@link TracingClientInterceptor} - * @param status The status passed to {@link ForwardingClientCallListener#onClose(Status, - * Metadata)}. - * @param trailers The trailing headers passed to {@link ForwardingClientCallListener#onClose(Status, - * Metadata)}. + * @param status The status passed to {@link ForwardingClientCallListener#onClose}. + * @param trailers The trailing headers passed to {@link ForwardingClientCallListener#onClose}. */ void close(Span span, Status status, Metadata trailers); } diff --git a/src/main/java/io/opentracing/contrib/grpc/ClientSpanDecorator.java b/src/main/java/io/opentracing/contrib/grpc/ClientSpanDecorator.java index 9ba06a1..8d2aac2 100644 --- a/src/main/java/io/opentracing/contrib/grpc/ClientSpanDecorator.java +++ b/src/main/java/io/opentracing/contrib/grpc/ClientSpanDecorator.java @@ -15,7 +15,6 @@ package io.opentracing.contrib.grpc; import io.grpc.CallOptions; -import io.grpc.Channel; import io.grpc.MethodDescriptor; import io.grpc.ServerCall; import io.opentracing.Span; @@ -27,14 +26,14 @@ public interface ClientSpanDecorator { /** - * The method of the implementation is executed inside {@link TracingClientInterceptor#interceptCall(MethodDescriptor, - * CallOptions, Channel)}. + * The method of the implementation is executed inside {@link + * TracingClientInterceptor#interceptCall}. * * @param span The span created by {@link TracingClientInterceptor} - * @param callOptions The {@link ServerCall} parameter of {@link TracingClientInterceptor#interceptCall(MethodDescriptor, - * CallOptions, Channel)} - * @param method The {@link MethodDescriptor} parameter of {@link TracingClientInterceptor#interceptCall(MethodDescriptor, - * CallOptions, Channel)} + * @param callOptions The {@link ServerCall} parameter of {@link + * TracingClientInterceptor#interceptCall} + * @param method The {@link MethodDescriptor} parameter of {@link + * TracingClientInterceptor#interceptCall} */ void interceptCall(Span span, MethodDescriptor method, CallOptions callOptions); } diff --git a/src/main/java/io/opentracing/contrib/grpc/GrpcFields.java b/src/main/java/io/opentracing/contrib/grpc/GrpcFields.java index ee78e61..2bc869e 100644 --- a/src/main/java/io/opentracing/contrib/grpc/GrpcFields.java +++ b/src/main/java/io/opentracing/contrib/grpc/GrpcFields.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import com.google.common.collect.ImmutableMap; @@ -19,50 +20,50 @@ final class GrpcFields { - static final String ERROR = "error"; - static final String HEADERS = "headers"; + static final String ERROR = "error"; + static final String HEADERS = "headers"; - static final String CLIENT_CALL_START = "client-call-start"; - static final String CLIENT_CALL_CANCEL = "client-call-cancel"; - static final String CLIENT_CALL_HALF_CLOSE = "client-call-half-close"; - static final String CLIENT_CALL_SEND_MESSAGE = "client-call-send-message"; + static final String CLIENT_CALL_START = "client-call-start"; + static final String CLIENT_CALL_CANCEL = "client-call-cancel"; + static final String CLIENT_CALL_HALF_CLOSE = "client-call-half-close"; + static final String CLIENT_CALL_SEND_MESSAGE = "client-call-send-message"; - static final String CLIENT_CALL_LISTENER_ON_HEADERS = "client-call-listener-on-headers"; - static final String CLIENT_CALL_LISTENER_ON_MESSAGE = "client-call-listener-on-message"; - static final String CLIENT_CALL_LISTENER_ON_CLOSE = "client-call-listener-on-close"; + static final String CLIENT_CALL_LISTENER_ON_HEADERS = "client-call-listener-on-headers"; + static final String CLIENT_CALL_LISTENER_ON_MESSAGE = "client-call-listener-on-message"; + static final String CLIENT_CALL_LISTENER_ON_CLOSE = "client-call-listener-on-close"; - static final String SERVER_CALL_SEND_HEADERS = "server-call-send-headers"; - static final String SERVER_CALL_SEND_MESSAGE = "server-call-send-message"; - static final String SERVER_CALL_CLOSE = "server-call-close"; + static final String SERVER_CALL_SEND_HEADERS = "server-call-send-headers"; + static final String SERVER_CALL_SEND_MESSAGE = "server-call-send-message"; + static final String SERVER_CALL_CLOSE = "server-call-close"; - static final String SERVER_CALL_LISTENER_ON_MESSAGE = "server-call-listener-on-message"; - static final String SERVER_CALL_LISTENER_ON_HALF_CLOSE = "server-call-listener-on-half-close"; - static final String SERVER_CALL_LISTENER_ON_CANCEL = "server-call-listener-on-cancel"; - static final String SERVER_CALL_LISTENER_ON_COMPLETE = "server-call-listener-on-complete"; + static final String SERVER_CALL_LISTENER_ON_MESSAGE = "server-call-listener-on-message"; + static final String SERVER_CALL_LISTENER_ON_HALF_CLOSE = "server-call-listener-on-half-close"; + static final String SERVER_CALL_LISTENER_ON_CANCEL = "server-call-listener-on-cancel"; + static final String SERVER_CALL_LISTENER_ON_COMPLETE = "server-call-listener-on-complete"; - static void logClientCallError(Span span, String message, Throwable cause) { - logCallError(span, message, cause, "Client"); - } + static void logClientCallError(Span span, String message, Throwable cause) { + logCallError(span, message, cause, "Client"); + } - static void logServerCallError(Span span, String message, Throwable cause) { - logCallError(span, message, cause, "Server"); - } + static void logServerCallError(Span span, String message, Throwable cause) { + logCallError(span, message, cause, "Server"); + } - private static void logCallError(Span span, String message, Throwable cause, String name) { - ImmutableMap.Builder builder = ImmutableMap.builder() - .put(Fields.EVENT, GrpcFields.ERROR); - String causeMessage = null; - if (cause != null) { - builder.put(Fields.ERROR_OBJECT, cause); - causeMessage = cause.getMessage(); - } - if (message != null) { - builder.put(Fields.MESSAGE, message); - } else if (causeMessage != null) { - builder.put(Fields.MESSAGE, causeMessage); - } else { - builder.put(Fields.MESSAGE, name + " call failed"); - } - span.log(builder.build()); + private static void logCallError(Span span, String message, Throwable cause, String name) { + ImmutableMap.Builder builder = ImmutableMap.builder() + .put(Fields.EVENT, GrpcFields.ERROR); + String causeMessage = null; + if (cause != null) { + builder.put(Fields.ERROR_OBJECT, cause); + causeMessage = cause.getMessage(); + } + if (message != null) { + builder.put(Fields.MESSAGE, message); + } else if (causeMessage != null) { + builder.put(Fields.MESSAGE, causeMessage); + } else { + builder.put(Fields.MESSAGE, name + " call failed"); } + span.log(builder.build()); + } } diff --git a/src/main/java/io/opentracing/contrib/grpc/GrpcTags.java b/src/main/java/io/opentracing/contrib/grpc/GrpcTags.java index 5b271a3..2285cad 100644 --- a/src/main/java/io/opentracing/contrib/grpc/GrpcTags.java +++ b/src/main/java/io/opentracing/contrib/grpc/GrpcTags.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import io.grpc.Attributes; @@ -42,7 +43,8 @@ final class GrpcTags { /** * grpc.call_attributes tag. */ - static final NullableTag GRPC_CALL_ATTRIBUTES = new NullableTag<>("grpc.call_attributes"); + static final NullableTag GRPC_CALL_ATTRIBUTES = new NullableTag<>( + "grpc.call_attributes"); /** * grpc.call_options tag. @@ -74,7 +76,8 @@ public void set(Span span, Deadline deadline) { /** * grpc.method_name tag. */ - static final Tag GRPC_METHOD_NAME = new AbstractTag("grpc.method_name") { + static final Tag GRPC_METHOD_NAME = new AbstractTag( + "grpc.method_name") { @Override public void set(Span span, MethodDescriptor method) { if (method != null) { @@ -86,7 +89,8 @@ public void set(Span span, MethodDescriptor method) { /** * grpc.method_type tag. */ - static final Tag GRPC_METHOD_TYPE = new AbstractTag("grpc.method_type") { + static final Tag GRPC_METHOD_TYPE = new AbstractTag( + "grpc.method_type") { @Override public void set(Span span, MethodDescriptor method) { if (method != null) { diff --git a/src/main/java/io/opentracing/contrib/grpc/OpenTracingContextKey.java b/src/main/java/io/opentracing/contrib/grpc/OpenTracingContextKey.java index 47b244b..0dd2b0a 100644 --- a/src/main/java/io/opentracing/contrib/grpc/OpenTracingContextKey.java +++ b/src/main/java/io/opentracing/contrib/grpc/OpenTracingContextKey.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import io.grpc.Context; @@ -20,8 +21,8 @@ /** * A {@link io.grpc.Context} key for the current OpenTracing trace state. * - * Can be used to get the active span, or to set the active span for a scoped unit of work. See the - * grpc-java OpenTracing docs for use cases and + *

Can be used to get the active span, or to set the active span for a scoped unit of work. See + * the grpc-java OpenTracing docs for use cases and * examples. */ public class OpenTracingContextKey { @@ -32,6 +33,8 @@ public class OpenTracingContextKey { private static final Context.Key keyContext = Context.key(KEY_CONTEXT_NAME); /** + * Retrieves the active span. + * * @return the active span for the current request */ public static Span activeSpan() { @@ -39,6 +42,8 @@ public static Span activeSpan() { } /** + * Retrieves the span key. + * * @return the OpenTracing context key */ public static Context.Key getKey() { @@ -46,6 +51,8 @@ public static Context.Key getKey() { } /** + * Retrieves the span context key. + * * @return the OpenTracing context key for span context */ public static Context.Key getSpanContextKey() { @@ -55,4 +62,4 @@ public static Context.Key getSpanContextKey() { public static SpanContext activeSpanContext() { return keyContext.get(); } -} \ No newline at end of file +} diff --git a/src/main/java/io/opentracing/contrib/grpc/OperationNameConstructor.java b/src/main/java/io/opentracing/contrib/grpc/OperationNameConstructor.java index bd7289e..218d8f2 100644 --- a/src/main/java/io/opentracing/contrib/grpc/OperationNameConstructor.java +++ b/src/main/java/io/opentracing/contrib/grpc/OperationNameConstructor.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import io.grpc.MethodDescriptor; @@ -34,10 +35,10 @@ public String constructOperationName(MethodDescriptor /** * Constructs a span's operation name from the RPC's method. * - * @param method the rpc method to extract a name from - * @param the rpc request type + * @param method the rpc method to extract a name from + * @param the rpc request type * @param the rpc response type * @return the operation name */ String constructOperationName(MethodDescriptor method); -} \ No newline at end of file +} diff --git a/src/main/java/io/opentracing/contrib/grpc/ServerSpanDecorator.java b/src/main/java/io/opentracing/contrib/grpc/ServerSpanDecorator.java index f46ddbf..1b534c8 100644 --- a/src/main/java/io/opentracing/contrib/grpc/ServerSpanDecorator.java +++ b/src/main/java/io/opentracing/contrib/grpc/ServerSpanDecorator.java @@ -16,7 +16,6 @@ import io.grpc.Metadata; import io.grpc.ServerCall; -import io.grpc.ServerCallHandler; import io.opentracing.Span; /** @@ -26,14 +25,13 @@ public interface ServerSpanDecorator { /** - * The method of the implementation is executed inside {@link TracingServerInterceptor#interceptCall(ServerCall, - * Metadata, ServerCallHandler)}. + * The method of the implementation is executed inside {@link + * TracingServerInterceptor#interceptCall}. * * @param span The span created by {@link TracingServerInterceptor} - * @param call The {@link ServerCall} parameter of {@link TracingServerInterceptor#interceptCall(ServerCall, - * Metadata, ServerCallHandler)} - * @param headers The {@link Metadata} parameter of {@link TracingServerInterceptor#interceptCall(ServerCall, - * Metadata, ServerCallHandler)} + * @param call The {@link ServerCall} parameter of {@link + * TracingServerInterceptor#interceptCall} + * @param headers The {@link Metadata} parameter of {@link TracingServerInterceptor#interceptCall} */ void interceptCall(Span span, ServerCall call, Metadata headers); } diff --git a/src/test/java/io/opentracing/contrib/grpc/ActiveSpanContextSourceTest.java b/src/test/java/io/opentracing/contrib/grpc/ActiveSpanContextSourceTest.java index 1d2e9a1..6cc935c 100644 --- a/src/test/java/io/opentracing/contrib/grpc/ActiveSpanContextSourceTest.java +++ b/src/test/java/io/opentracing/contrib/grpc/ActiveSpanContextSourceTest.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.junit.Assert.assertEquals; @@ -32,7 +33,7 @@ public void before() { } @Test - public void TestDefaultNone() { + public void testDefaultNone() { ActiveSpanContextSource ss = ActiveSpanContextSource.NONE; assertNull("active span context should always be null", ss.getActiveSpanContext()); @@ -47,7 +48,7 @@ public void TestDefaultNone() { } @Test - public void TestDefaultGrpc() { + public void testDefaultGrpc() { ActiveSpanContextSource ss = ActiveSpanContextSource.GRPC_CONTEXT; assertNull("active span context should be null, no span context in OpenTracingContextKey", ss.getActiveSpanContext()); @@ -67,4 +68,4 @@ public void TestDefaultGrpc() { ss.getActiveSpanContext()); } -} \ No newline at end of file +} diff --git a/src/test/java/io/opentracing/contrib/grpc/ActiveSpanSourceTest.java b/src/test/java/io/opentracing/contrib/grpc/ActiveSpanSourceTest.java index f38ae04..e63c525 100644 --- a/src/test/java/io/opentracing/contrib/grpc/ActiveSpanSourceTest.java +++ b/src/test/java/io/opentracing/contrib/grpc/ActiveSpanSourceTest.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.junit.Assert.assertEquals; @@ -24,10 +25,10 @@ public class ActiveSpanSourceTest { - Tracer tracer = new MockTracer(); + private Tracer tracer = new MockTracer(); @Test - public void TestDefaultNone() { + public void testDefaultNone() { ActiveSpanSource ss = ActiveSpanSource.NONE; assertNull("active span should always be null", ss.getActiveSpan()); @@ -42,7 +43,7 @@ public void TestDefaultNone() { } @Test - public void TestDefaultGrpc() { + public void testDefaultGrpc() { ActiveSpanSource ss = ActiveSpanSource.GRPC_CONTEXT; assertNull("active span should be null, no span in OpenTracingContextKey", ss.getActiveSpan()); diff --git a/src/test/java/io/opentracing/contrib/grpc/GrpcTagsTest.java b/src/test/java/io/opentracing/contrib/grpc/GrpcTagsTest.java index 554e1bc..f8f3ac4 100644 --- a/src/test/java/io/opentracing/contrib/grpc/GrpcTagsTest.java +++ b/src/test/java/io/opentracing/contrib/grpc/GrpcTagsTest.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.assertj.core.api.Assertions.assertThat; @@ -28,6 +29,7 @@ import org.junit.Test; public class GrpcTagsTest { + @Test public void testStatusOk() { final Status status = Status.OK; @@ -43,9 +45,7 @@ public void testStatusError() { MockSpan span = new MockTracer().buildSpan("").start(); GrpcTags.GRPC_STATUS.set(span, status); assertThat(span.tags()) - .containsOnly( - MapEntry.entry(GrpcTags.GRPC_STATUS.getKey(), status.getCode().name()) - ); + .containsOnly(MapEntry.entry(GrpcTags.GRPC_STATUS.getKey(), status.getCode().name())); } @Test diff --git a/src/test/java/io/opentracing/contrib/grpc/OpenTracingContextKeyTest.java b/src/test/java/io/opentracing/contrib/grpc/OpenTracingContextKeyTest.java index a03d9b7..b091d3a 100644 --- a/src/test/java/io/opentracing/contrib/grpc/OpenTracingContextKeyTest.java +++ b/src/test/java/io/opentracing/contrib/grpc/OpenTracingContextKeyTest.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.junit.Assert.assertEquals; @@ -96,17 +97,14 @@ public void testMultipleContextLayers() { Span childSpan = tracer.buildSpan("s1").start(); Context childCtx = Context.current().withValue(OpenTracingContextKey.getKey(), childSpan); parentCtx = childCtx.attach(); - assertEquals(OpenTracingContextKey.activeSpan(), childSpan); childCtx.detach(parentCtx); childSpan.finish(); - assertEquals(OpenTracingContextKey.activeSpan(), parentSpan); parentCtx.detach(previousCtx); parentSpan.finish(); - assertNull(OpenTracingContextKey.activeSpan()); } @@ -121,17 +119,14 @@ public void testMultipleContextLayersForSpanContext() { Context childCtx = Context.current().withValue(OpenTracingContextKey.getSpanContextKey(), childSpan.context()); parentCtx = childCtx.attach(); - assertEquals(OpenTracingContextKey.activeSpanContext(), childSpan.context()); childCtx.detach(parentCtx); childSpan.finish(); - assertEquals(OpenTracingContextKey.activeSpanContext(), parentSpan.context()); parentCtx.detach(previousCtx); parentSpan.finish(); - assertNull(OpenTracingContextKey.activeSpanContext()); } } diff --git a/src/test/java/io/opentracing/contrib/grpc/SecondClientInterceptor.java b/src/test/java/io/opentracing/contrib/grpc/SecondClientInterceptor.java index ce2d282..88273b5 100644 --- a/src/test/java/io/opentracing/contrib/grpc/SecondClientInterceptor.java +++ b/src/test/java/io/opentracing/contrib/grpc/SecondClientInterceptor.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.junit.Assert.assertNotNull; @@ -29,71 +30,72 @@ public class SecondClientInterceptor implements ClientInterceptor { - private final MockTracer tracer; + private final MockTracer tracer; + + SecondClientInterceptor(MockTracer tracer) { + this.tracer = tracer; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, + CallOptions callOptions, + Channel next) { + + assertNotNull(tracer.activeSpan()); + + return new ForwardingClientCall.SimpleForwardingClientCall( + next.newCall(method, callOptions)) { + + @Override + public void start(Listener responseListener, Metadata headers) { + assertNotNull(tracer.activeSpan()); + delegate().start(new ForwardingClientCallListener + .SimpleForwardingClientCallListener(responseListener) { + }, headers); + } + + @Override + public void request(int numMessages) { + assertNotNull(tracer.activeSpan()); + delegate().request(numMessages); + } + + @Override + public void cancel(@Nullable String message, @Nullable Throwable cause) { + assertNotNull(tracer.activeSpan()); + delegate().cancel(message, cause); + } + + @Override + public void halfClose() { + assertNotNull(tracer.activeSpan()); + delegate().halfClose(); + } - public SecondClientInterceptor(MockTracer tracer) { - this.tracer = tracer; - } + @Override + public void sendMessage(ReqT message) { + assertNotNull(tracer.activeSpan()); + delegate().sendMessage(message); + } - @Override - public ClientCall interceptCall( - MethodDescriptor method, - CallOptions callOptions, - Channel next) { + @Override + public boolean isReady() { + assertNotNull(tracer.activeSpan()); + return delegate().isReady(); + } + @Override + public void setMessageCompression(boolean enabled) { assertNotNull(tracer.activeSpan()); + delegate().setMessageCompression(enabled); + } - return new ForwardingClientCall.SimpleForwardingClientCall( - next.newCall(method, callOptions)) { - - @Override - public void start(Listener responseListener, Metadata headers) { - assertNotNull(tracer.activeSpan()); - delegate().start(new ForwardingClientCallListener - .SimpleForwardingClientCallListener(responseListener) {}, headers); - } - - @Override - public void request(int numMessages) { - assertNotNull(tracer.activeSpan()); - delegate().request(numMessages); - } - - @Override - public void cancel(@Nullable String message, @Nullable Throwable cause) { - assertNotNull(tracer.activeSpan()); - delegate().cancel(message, cause); - } - - @Override - public void halfClose() { - assertNotNull(tracer.activeSpan()); - delegate().halfClose(); - } - - @Override - public void sendMessage(ReqT message) { - assertNotNull(tracer.activeSpan()); - delegate().sendMessage(message); - } - - @Override - public boolean isReady() { - assertNotNull(tracer.activeSpan()); - return delegate().isReady(); - } - - @Override - public void setMessageCompression(boolean enabled) { - assertNotNull(tracer.activeSpan()); - delegate().setMessageCompression(enabled); - } - - @Override - public Attributes getAttributes() { - assertNotNull(tracer.activeSpan()); - return delegate().getAttributes(); - } - }; - } + @Override + public Attributes getAttributes() { + assertNotNull(tracer.activeSpan()); + return delegate().getAttributes(); + } + }; + } } diff --git a/src/test/java/io/opentracing/contrib/grpc/SecondServerInterceptor.java b/src/test/java/io/opentracing/contrib/grpc/SecondServerInterceptor.java index a3e96c6..98b97ce 100644 --- a/src/test/java/io/opentracing/contrib/grpc/SecondServerInterceptor.java +++ b/src/test/java/io/opentracing/contrib/grpc/SecondServerInterceptor.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.junit.Assert.assertNotNull; @@ -27,9 +28,10 @@ import io.opentracing.mock.MockTracer; public class SecondServerInterceptor implements ServerInterceptor { + private final MockTracer tracer; - public SecondServerInterceptor(MockTracer tracer) { + SecondServerInterceptor(MockTracer tracer) { this.tracer = tracer; } @@ -107,35 +109,35 @@ public String getAuthority() { return new ForwardingServerCallListener.SimpleForwardingServerCallListener( next.startCall(call, headers)) { - @Override - public void onMessage(ReqT message) { - assertNotNull(tracer.activeSpan()); - delegate().onMessage(message); - } - - @Override - public void onHalfClose() { - assertNotNull(tracer.activeSpan()); - delegate().onHalfClose(); - } - - @Override - public void onCancel() { - assertNotNull(tracer.activeSpan()); - delegate().onCancel(); - } - - @Override - public void onComplete() { - assertNotNull(tracer.activeSpan()); - delegate().onComplete(); - } - - @Override - public void onReady() { - assertNotNull(tracer.activeSpan()); - delegate().onReady(); - } - }; + @Override + public void onMessage(ReqT message) { + assertNotNull(tracer.activeSpan()); + delegate().onMessage(message); + } + + @Override + public void onHalfClose() { + assertNotNull(tracer.activeSpan()); + delegate().onHalfClose(); + } + + @Override + public void onCancel() { + assertNotNull(tracer.activeSpan()); + delegate().onCancel(); + } + + @Override + public void onComplete() { + assertNotNull(tracer.activeSpan()); + delegate().onComplete(); + } + + @Override + public void onReady() { + assertNotNull(tracer.activeSpan()); + delegate().onReady(); + } + }; } } diff --git a/src/test/java/io/opentracing/contrib/grpc/TracedClient.java b/src/test/java/io/opentracing/contrib/grpc/TracedClient.java index 6a498d8..01fdda2 100644 --- a/src/test/java/io/opentracing/contrib/grpc/TracedClient.java +++ b/src/test/java/io/opentracing/contrib/grpc/TracedClient.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import io.grpc.ClientInterceptor; @@ -29,14 +30,6 @@ class TracedClient { blockingStub = GreeterGrpc.newBlockingStub(ClientInterceptors.intercept(channel, interceptors)); } - TracedClient( - ManagedChannel channel, - long deadline, - ClientInterceptor... interceptors) { - blockingStub = GreeterGrpc.newBlockingStub(ClientInterceptors.intercept(channel, interceptors)) - .withDeadlineAfter(deadline, TimeUnit.MILLISECONDS); - } - TracedClient( ManagedChannel channel, long deadline, @@ -47,9 +40,9 @@ class TracedClient { .withCompression(compression); } - HelloReply greet(String name) { + HelloReply greet() { try { - return blockingStub.sayHello(HelloRequest.newBuilder().setName(name).build()); + return blockingStub.sayHello(HelloRequest.newBuilder().setName("world").build()); } catch (Exception ignored) { return null; } diff --git a/src/test/java/io/opentracing/contrib/grpc/TracedService.java b/src/test/java/io/opentracing/contrib/grpc/TracedService.java index 8f76591..bd7f497 100644 --- a/src/test/java/io/opentracing/contrib/grpc/TracedService.java +++ b/src/test/java/io/opentracing/contrib/grpc/TracedService.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import io.grpc.ServerInterceptor; diff --git a/src/test/java/io/opentracing/contrib/grpc/TracingClientInterceptorTest.java b/src/test/java/io/opentracing/contrib/grpc/TracingClientInterceptorTest.java index dfd8426..d10f3d8 100644 --- a/src/test/java/io/opentracing/contrib/grpc/TracingClientInterceptorTest.java +++ b/src/test/java/io/opentracing/contrib/grpc/TracingClientInterceptorTest.java @@ -74,7 +74,7 @@ public class TracingClientInterceptorTest { public GrpcServerRule grpcServer = new GrpcServerRule(); @Before - public void before() { + public void setUp() { GlobalTracerTestUtil.resetGlobalTracer(); clientTracer.reset(); TracedService.addGeeterService(grpcServer.getServiceRegistry()); @@ -86,7 +86,7 @@ public void testTracedClientBasic() { TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -109,7 +109,7 @@ public void testTracedClientTwoInterceptors() { tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -133,7 +133,7 @@ public void testTracedClientWithVerbosity() { TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -170,7 +170,7 @@ public void testTracedClientWithStreaming() { TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -208,7 +208,7 @@ public String constructOperationName(MethodDescriptor TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -233,7 +233,7 @@ public void testTracedClientWithTracedAttributes() { TracedClient client = new TracedClient(grpcServer.getChannel(), 50, "gzip", tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -267,7 +267,7 @@ public Span getActiveSpan() { TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -298,7 +298,7 @@ public SpanContext getActiveSpanContext() { TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -338,7 +338,7 @@ public void interceptCall(Span span, MethodDescriptor method, CallOptions callOp TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); @@ -380,7 +380,7 @@ public void close(Span span, Status status, Metadata trailers) { TracedClient client = new TracedClient(grpcServer.getChannel(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", clientTracer.finishedSpans().size(), 1); diff --git a/src/test/java/io/opentracing/contrib/grpc/TracingInterceptorsTest.java b/src/test/java/io/opentracing/contrib/grpc/TracingInterceptorsTest.java index 9ee9a51..fc4fb45 100644 --- a/src/test/java/io/opentracing/contrib/grpc/TracingInterceptorsTest.java +++ b/src/test/java/io/opentracing/contrib/grpc/TracingInterceptorsTest.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.awaitility.Awaitility.await; @@ -80,7 +81,7 @@ public void testTracedClientAndServerSuccess() { TracedService.addGeeterService(grpcServer.getServiceRegistry(), serverInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); @@ -168,7 +169,7 @@ public void sendMessage(ReqT message) { .build(); TracedService.addGeeterService(grpcServer.getServiceRegistry(), serverInterceptor); - assertNull("call should return null", client.greet("world")); + assertNull("call should return null", client.greet()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); @@ -246,7 +247,7 @@ public void onMessage(RespT message) { .build(); TracedService.addGeeterService(grpcServer.getServiceRegistry(), serverInterceptor); - assertNull("call should return null", client.greet("world")); + assertNull("call should return null", client.greet()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); @@ -323,7 +324,7 @@ public void onMessage(ReqT message) { TracedService .addGeeterService(grpcServer.getServiceRegistry(), serverReceiveError, serverInterceptor); - assertNull("call should return null", client.greet("world")); + assertNull("call should return null", client.greet()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); @@ -402,7 +403,7 @@ public void sendMessage(RespT message) { TracedService .addGeeterService(grpcServer.getServiceRegistry(), serverSendError, serverInterceptor); - assertNull("call should return null", client.greet("world")); + assertNull("call should return null", client.greet()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(clientTracer), equalTo(1)); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); diff --git a/src/test/java/io/opentracing/contrib/grpc/TracingServerInterceptorTest.java b/src/test/java/io/opentracing/contrib/grpc/TracingServerInterceptorTest.java index 6391917..b4d398b 100644 --- a/src/test/java/io/opentracing/contrib/grpc/TracingServerInterceptorTest.java +++ b/src/test/java/io/opentracing/contrib/grpc/TracingServerInterceptorTest.java @@ -11,6 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package io.opentracing.contrib.grpc; import static org.awaitility.Awaitility.await; @@ -95,7 +96,7 @@ public void testTracedServerBasic() { TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1); @@ -119,7 +120,7 @@ public void testTracedServerTwoInterceptors() { grpcServer.getServiceRegistry(), secondServerInterceptor, tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1); @@ -144,7 +145,7 @@ public void testTracedServerWithVerbosity() { TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1); @@ -181,7 +182,7 @@ public void testTracedServerWithStreaming() { TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1); @@ -220,7 +221,7 @@ public String constructOperationName(MethodDescriptor TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1); @@ -245,7 +246,7 @@ public void testTracedServerWithTracedAttributes() { TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1); @@ -288,7 +289,7 @@ public void interceptCall(Span span, ServerCall call, Metadata headers) { TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1); @@ -329,7 +330,7 @@ public void close(Span span, Status status, Metadata trailers) { TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor); assertEquals("call should complete successfully", "Hello world", - client.greet("world").getMessage()); + client.greet().getMessage()); await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1)); assertEquals("one span should have been created and finished for one client request", serverTracer.finishedSpans().size(), 1);