add comments

This commit is contained in:
ascrutae 2018-02-05 22:30:21 +08:00
parent 9c332e864a
commit 60638f7ceb
17 changed files with 74 additions and 614 deletions

View File

@ -1,39 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ForwardingClientCall;
import io.grpc.MethodDescriptor;
import static org.apache.skywalking.apm.plugin.grpc.v1.OperationNameFormatUtil.formatOperationName;
public abstract class AbstractCallClientInterceptor extends ForwardingClientCall.SimpleForwardingClientCall {
protected final String serviceName;
protected final String remotePeer;
protected AbstractCallClientInterceptor(ClientCall delegate, MethodDescriptor method, Channel channel) {
super(delegate);
this.serviceName = formatOperationName(method);
this.remotePeer = channel.authority();
}
}

View File

@ -25,6 +25,12 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedI
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
/**
* {@link AbstractServerImplBuilderInterceptor} add the {@link CallServerInterceptor} interceptor for each the
* ServerService.
*
* @author zhang xin
*/
public class AbstractServerImplBuilderInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,

View File

@ -26,6 +26,11 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceC
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
/**
* {@link AbstractStubInterceptor} add the interceptor for each ClientCall.
*
* @author zhang xin
*/
public class AbstractStubInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {

View File

@ -20,6 +20,7 @@ package org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ForwardingClientCall;
import io.grpc.ForwardingClientCallListener;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
@ -32,10 +33,20 @@ import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
public class BlockingCallClientInterceptor extends AbstractCallClientInterceptor {
import static org.apache.skywalking.apm.plugin.grpc.v1.OperationNameFormatUtil.formatOperationName;
/**
* @author zhang xin
*/
public class BlockingCallClientInterceptor extends ForwardingClientCall.SimpleForwardingClientCall {
private final String serviceName;
private final String remotePeer;
public BlockingCallClientInterceptor(ClientCall delegate, MethodDescriptor method, Channel channel) {
super(delegate, method, channel);
super(delegate);
this.serviceName = formatOperationName(method);
this.remotePeer = channel.authority();
}
@Override public void start(Listener responseListener, Metadata headers) {

View File

@ -36,6 +36,9 @@ import org.apache.skywalking.apm.util.StringUtil;
import static org.apache.skywalking.apm.plugin.grpc.v1.Constants.STREAM_ON_NEXT_OPERATION_NAME;
/**
* @author zhang xin
*/
public class CallServerInterceptor implements ServerInterceptor {
@Override
public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler handler) {

View File

@ -18,6 +18,11 @@
package org.apache.skywalking.apm.plugin.grpc.v1;
/**
* Constant variables
*
* @author zhang xin
*/
public class Constants {
public static final String STREAM_ON_READY_OPERATION_NAME = "RequestStreamObserver/onReady";

View File

@ -24,9 +24,17 @@ import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.MethodDescriptor;
/**
* {@link GRPCClientInterceptor} determines the returned Interceptor based on the method type. If the method type is
* UNARY, {@link GRPCClientInterceptor} returns BlockingCallClientInterceptor, or it returns
* StreamCallClientInterceptor.
*
* @author zhang xin
*/
public class GRPCClientInterceptor implements ClientInterceptor {
@Override public ClientCall interceptCall(MethodDescriptor method,
@Override
public ClientCall interceptCall(MethodDescriptor method,
CallOptions callOptions, Channel channel) {
if (method.getType() != MethodDescriptor.MethodType.UNARY) {
return new StreamCallClientInterceptor(channel.newCall(method, callOptions), method, channel);

View File

@ -20,6 +20,11 @@ package org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.MethodDescriptor;
/**
* Operation Name utility
*
* @author zhangxin
*/
public class OperationNameFormatUtil {
public static String formatOperationName(MethodDescriptor methodDescriptor) {

View File

@ -20,6 +20,7 @@ package org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ForwardingClientCall;
import io.grpc.ForwardingClientCallListener;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
@ -35,11 +36,20 @@ import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import static org.apache.skywalking.apm.plugin.grpc.v1.Constants.STREAM_ON_NEXT_OPERATION_NAME;
import static org.apache.skywalking.apm.plugin.grpc.v1.Constants.STREAM_ON_READY_OPERATION_NAME;
import static org.apache.skywalking.apm.plugin.grpc.v1.OperationNameFormatUtil.formatOperationName;
public class StreamCallClientInterceptor extends AbstractCallClientInterceptor {
/**
* @author zhangxin
*/
public class StreamCallClientInterceptor extends ForwardingClientCall.SimpleForwardingClientCall {
private final String serviceName;
private final String remotePeer;
protected StreamCallClientInterceptor(ClientCall delegate, MethodDescriptor method, Channel channel) {
super(delegate, method, channel);
super(delegate);
this.serviceName = formatOperationName(method);
this.remotePeer = channel.authority();
}
@Override

View File

@ -29,11 +29,19 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link AbstractServerImplBuilderInstrumentation} present that the GRPC plugin intercept the method
* <code>addService</code> in the {@link io.grpc.internal.AbstractServerImplBuilder} class by using the {@link
* org.apache.skywalking.apm.plugin.grpc.v1.AbstractServerImplBuilderInterceptor} class.
*
* @author zhang xin
*/
public class AbstractServerImplBuilderInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String ENHANCE_CLASS = "io.grpc.internal.AbstractServerImplBuilder";
public static final String ENHANCE_METHOD = "addService";
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.grpc.v1.AbstractServerImplBuilderInterceptor";
public static final String ARGUMENT_TYPE = "io.grpc.ServerServiceDefinition";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
@ -43,7 +51,7 @@ public class AbstractServerImplBuilderInstrumentation extends ClassInstanceMetho
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD).and(takesArgumentWithType(0, "io.grpc.ServerServiceDefinition"));
return named(ENHANCE_METHOD).and(takesArgumentWithType(0, ARGUMENT_TYPE));
}
@Override public String getMethodsInterceptor() {

View File

@ -29,6 +29,13 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
/**
* {@link AbstractStubInstrumentation} present that the GRPC plugin intercept the method <code>getChannel</code> in the
* {@link io.grpc.stub.AbstractStub} class by using the {@link org.apache.skywalking.apm.plugin.grpc.v1.AbstractStubInterceptor}
* class.
*
* @author zhang xin
*/
public class AbstractStubInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.grpc.v1.AbstractStubInterceptor";

View File

@ -1,65 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.MethodDescriptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest(MethodDescriptor.class)
public class ClientCallIConstructorInterceptorTest {
private ClientCallIConstructorInterceptor constructorInterceptor;
@Mock
private EnhancedInstance enhancedInstance;
private Object[] arguments;
@Before
public void setUp() {
constructorInterceptor = new ClientCallIConstructorInterceptor();
MethodDescriptor methodDescriptor = mock(MethodDescriptor.class);
when(methodDescriptor.getType()).thenReturn(MethodDescriptor.MethodType.UNARY);
when(methodDescriptor.getFullMethodName()).thenReturn("test/testMethod");
arguments = new Object[] {methodDescriptor};
}
@Test
public void testOnConstructor() {
constructorInterceptor.onConstruct(enhancedInstance, arguments);
verify(enhancedInstance, times(1)).setSkyWalkingDynamicField(Matchers.any());
}
}

View File

@ -1,85 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.grpc.v1;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class ClientCallOnNextInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule agentServiceRule = new AgentServiceRule();
@Mock
private EnhancedInstance clientCall;
@Mock
private GRPCDynamicFields cachedObjects;
private ClientCallOnNextInterceptor callOnNextInterceptor;
@Before
public void setUp() {
when(cachedObjects.getRequestMethodName()).thenReturn("org.skywalking.test.grpc.GreetService.sayHello");
when(clientCall.getSkyWalkingDynamicField()).thenReturn(cachedObjects);
callOnNextInterceptor = new ClientCallOnNextInterceptor();
}
@Test
public void testCallOnNext() throws Throwable {
callOnNextInterceptor.beforeMethod(clientCall, null, null, null, null);
callOnNextInterceptor.afterMethod(clientCall, null, null, null, null);
verify(cachedObjects, times(1)).incrementOnNextCount();
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
assertThat(SegmentHelper.getSpans(traceSegment).size(), is(1));
AbstractTracingSpan span = SegmentHelper.getSpans(traceSegment).get(0);
assertThat(span.getOperationName(), is("org.skywalking.test.grpc.GreetService.sayHello/ResponseStreamObserver/OnNext"));
assertThat(span.isEntry(), is(false));
assertThat(span.isExit(), is(false));
}
}

View File

@ -1,122 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.MethodDescriptor;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.test.tools.SpanAssert;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class ClientCallsMethodInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule agentServiceRule = new AgentServiceRule();
private ClientCallsMethodInterceptor clientCallStartInterceptor;
@Mock
private EnhancedInstance clientCallImpl;
@Mock
private EnhancedInstance clientCallListener;
@Mock
private GRPCDynamicFields unaryCachedObjects;
@Mock
private GRPCDynamicFields streamCachedObjects;
private Object[] arguments;
private Class[] argumentTypes;
@Before
public void setUp() {
when(unaryCachedObjects.getRequestMethodName()).thenReturn("org.skywalking.test.grpc.GreetService.sayHello");
when(unaryCachedObjects.getAuthority()).thenReturn("localhost:500051");
when(unaryCachedObjects.getMethodType()).thenReturn(MethodDescriptor.MethodType.UNARY);
when(streamCachedObjects.getRequestMethodName()).thenReturn("org.skywalking.test.grpc.GreetService.sayHello");
when(streamCachedObjects.getAuthority()).thenReturn("localhost:500051");
when(streamCachedObjects.getMethodType()).thenReturn(MethodDescriptor.MethodType.SERVER_STREAMING);
arguments = new Object[] {clientCallImpl, clientCallListener};
argumentTypes = new Class[] {clientCallImpl.getClass(), clientCallListener.getClass()};
clientCallStartInterceptor = new ClientCallsMethodInterceptor();
}
@Test
public void testNormalUnaryCallStart() throws Throwable {
when(clientCallImpl.getSkyWalkingDynamicField()).thenReturn(unaryCachedObjects);
clientCallStartInterceptor.beforeMethod(null, null, arguments, argumentTypes, null);
clientCallStartInterceptor.afterMethod(null, null, arguments, argumentTypes, null);
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
assertThat(SegmentHelper.getSpans(traceSegment).size(), is(1));
AbstractTracingSpan abstractTracingSpan = SegmentHelper.getSpans(traceSegment).get(0);
SpanAssert.assertComponent(abstractTracingSpan, ComponentsDefine.GRPC);
SpanAssert.assertLayer(abstractTracingSpan, SpanLayer.RPC_FRAMEWORK);
SpanAssert.assertOccurException(abstractTracingSpan, false);
}
@Test
public void testNormalStreamCallStart() throws Throwable {
when(clientCallImpl.getSkyWalkingDynamicField()).thenReturn(streamCachedObjects);
clientCallStartInterceptor.beforeMethod(null, null, arguments, argumentTypes, null);
clientCallStartInterceptor.afterMethod(null, null, arguments, argumentTypes, null);
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
assertThat(SegmentHelper.getSpans(traceSegment).size(), is(1));
AbstractTracingSpan abstractTracingSpan = SegmentHelper.getSpans(traceSegment).get(0);
SpanAssert.assertComponent(abstractTracingSpan, ComponentsDefine.GRPC);
SpanAssert.assertLayer(abstractTracingSpan, SpanLayer.RPC_FRAMEWORK);
SpanAssert.assertOccurException(abstractTracingSpan, false);
}
}

View File

@ -1,72 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest(MethodDescriptor.class)
public class ServerCallHandlerInterceptorTest {
@Mock
private EnhancedInstance enhancedInstance;
private ServerCallHandlerInterceptor callHandlerInterceptor;
@Mock
private ServerCall serverCall;
@Mock
private MethodDescriptor methodDescriptor;
private Metadata metadata;
private Object[] arguments;
private Class[] argumentTypes;
@Before
public void setUp() {
when(methodDescriptor.getFullMethodName()).thenReturn("org.skywalking.test.GreetService/SayHello");
when(serverCall.getMethodDescriptor()).thenReturn(methodDescriptor);
callHandlerInterceptor = new ServerCallHandlerInterceptor();
metadata = new Metadata();
arguments = new Object[] {serverCall, metadata};
argumentTypes = new Class[] {serverCall.getClass(), metadata.getClass()};
}
@Test
public void testSetCachedObjects() throws Throwable {
callHandlerInterceptor.afterMethod(null, null, arguments, argumentTypes, enhancedInstance);
verify(enhancedInstance, times(1)).setSkyWalkingDynamicField(Matchers.any());
}
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.grpc.v1;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class ServerCallOnMessageInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule agentServiceRule = new AgentServiceRule();
@Mock
private EnhancedInstance clientCall;
@Mock
private GRPCDynamicFields cachedObjects;
private ServerCallOnMessageInterceptor serverCallOnMessageInterceptor;
@Before
public void setUp() {
when(cachedObjects.getRequestMethodName()).thenReturn("org.skywalking.test.grpc.GreetService.sayHello");
when(clientCall.getSkyWalkingDynamicField()).thenReturn(cachedObjects);
serverCallOnMessageInterceptor = new ServerCallOnMessageInterceptor();
}
@Test
public void testCallOnNext() throws Throwable {
serverCallOnMessageInterceptor.beforeMethod(clientCall, null, null, null, null);
serverCallOnMessageInterceptor.afterMethod(clientCall, null, null, null, null);
verify(cachedObjects, times(1)).incrementOnNextCount();
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
assertThat(SegmentHelper.getSpans(traceSegment).size(), is(1));
AbstractTracingSpan span = SegmentHelper.getSpans(traceSegment).get(0);
assertThat(span.getOperationName(), is("org.skywalking.test.grpc.GreetService.sayHello/ResponseStreamObserver/OnNext"));
assertThat(span.isEntry(), is(false));
assertThat(span.isExit(), is(false));
}
}

View File

@ -1,139 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.grpc.v1;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import org.apache.skywalking.apm.agent.test.tools.SegmentRefAssert;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.helper.SpanHelper;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.plugin.grpc.v1.vo.GRPCDynamicFields;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class ServerCallOnReadyInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule agentServiceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedInstance;
private GRPCDynamicFields cachedObjects;
@Mock
private MethodDescriptor.Marshaller requestMarshaller;
@Mock
private MethodDescriptor.Marshaller responseMarshaller;
private ServerCallOnReadyInterceptor serverCallOnReadyInterceptor;
private ServerCallOnCloseInterceptor serverCallOnCloseInterceptor;
private ServerCallOnMessageInterceptor serverCallOnMessageInterceptor;
@Before
public void setUp() {
cachedObjects = new GRPCDynamicFields();
cachedObjects.setDescriptor(MethodDescriptor.create(MethodDescriptor.MethodType.SERVER_STREAMING, "org.skywalking.test.grpc.GreetService/SayHello", requestMarshaller, responseMarshaller));
when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(cachedObjects);
serverCallOnReadyInterceptor = new ServerCallOnReadyInterceptor();
serverCallOnCloseInterceptor = new ServerCallOnCloseInterceptor();
serverCallOnMessageInterceptor = new ServerCallOnMessageInterceptor();
}
@Test
public void testOnReadyWithoutContextCarrier() throws Throwable {
cachedObjects.setMetadata(new Metadata());
serverCallOnReadyInterceptor.beforeMethod(enhancedInstance, null, null, null, null);
serverCallOnMessageInterceptor.beforeMethod(enhancedInstance, null, null, null, null);
serverCallOnMessageInterceptor.afterMethod(enhancedInstance, null, null, null, null);
serverCallOnCloseInterceptor.afterMethod(enhancedInstance, null, null, null, null);
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment segment = segmentStorage.getTraceSegments().get(0);
assertThat(segment.getRefs() == null, is(true));
assertThat(SegmentHelper.getSpans(segment).size(), is(2));
AbstractTracingSpan abstractTracingSpan = SegmentHelper.getSpans(segment).get(0);
assertThat(abstractTracingSpan.getOperationName(), is("org.skywalking.test.grpc.GreetService.sayHello/ResponseStreamObserver/OnNext"));
abstractTracingSpan = SegmentHelper.getSpans(segment).get(1);
assertThat(abstractTracingSpan.getOperationName(), is("org.skywalking.test.grpc.GreetService.sayHello/StreamCall"));
assertThat(abstractTracingSpan.isEntry(), is(true));
assertThat(SpanHelper.getTags(abstractTracingSpan).size(), is(1));
assertThat(SpanHelper.getTags(abstractTracingSpan).get(0).getKey(), is("onNext.count"));
assertThat(SpanHelper.getTags(abstractTracingSpan).get(0).getValue(), is("1"));
}
@Test
public void testOnReadyWithContextCarrier() throws Throwable {
Metadata metadata = new Metadata();
metadata.put(Metadata.Key.of("sw3", Metadata.ASCII_STRING_MARSHALLER), "1.234.111|3|1|1|#192.168.1.100:50051|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*");
cachedObjects.setMetadata(metadata);
serverCallOnReadyInterceptor.beforeMethod(enhancedInstance, null, null, null, null);
serverCallOnMessageInterceptor.beforeMethod(enhancedInstance, null, null, null, null);
serverCallOnMessageInterceptor.afterMethod(enhancedInstance, null, null, null, null);
serverCallOnCloseInterceptor.afterMethod(enhancedInstance, null, null, null, null);
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment segment = segmentStorage.getTraceSegments().get(0);
assertThat(segment.getRefs() != null, is(true));
SegmentRefAssert.assertPeerHost(segment.getRefs().get(0), "192.168.1.100:50051");
SegmentRefAssert.assertEntryApplicationInstanceId(segment.getRefs().get(0), 1);
SegmentRefAssert.assertSpanId(segment.getRefs().get(0), 3);
SegmentRefAssert.assertSegmentId(segment.getRefs().get(0), "1.234.111");
assertThat(SegmentHelper.getSpans(segment).size(), is(2));
AbstractTracingSpan abstractTracingSpan = SegmentHelper.getSpans(segment).get(0);
assertThat(abstractTracingSpan.getOperationName(), is("org.skywalking.test.grpc.GreetService.sayHello/ResponseStreamObserver/OnNext"));
abstractTracingSpan = SegmentHelper.getSpans(segment).get(1);
assertThat(abstractTracingSpan.getOperationName(), is("org.skywalking.test.grpc.GreetService.sayHello/StreamCall"));
assertThat(abstractTracingSpan.isEntry(), is(true));
assertThat(SpanHelper.getTags(abstractTracingSpan).size(), is(1));
assertThat(SpanHelper.getTags(abstractTracingSpan).get(0).getKey(), is("onNext.count"));
assertThat(SpanHelper.getTags(abstractTracingSpan).get(0).getValue(), is("1"));
}
}