devkanro commented on a change in pull request #4177: Enhance gRPC plugin URL: https://github.com/apache/skywalking/pull/4177#discussion_r371403331
########## File path: apm-sniffer/apm-sdk-plugin/grpc-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/grpc/v1/server/TracingServerCall.java ########## @@ -0,0 +1,111 @@ +/* + * 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.server; + +import io.grpc.ForwardingServerCall; +import io.grpc.Metadata; +import io.grpc.ServerCall; +import io.grpc.Status; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +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; +import org.apache.skywalking.apm.plugin.grpc.v1.OperationNameFormatUtil; + +import static org.apache.skywalking.apm.plugin.grpc.v1.Constants.*; + +/** + * @author wang zheng, kanro + */ +public class TracingServerCall<REQUEST, RESPONSE> extends ForwardingServerCall.SimpleForwardingServerCall<REQUEST, RESPONSE> { + + private final String operationPrefix; + private final ContextSnapshot contextSnapshot; + + protected TracingServerCall(ServerCall<REQUEST, RESPONSE> delegate, ContextSnapshot contextSnapshot) { + super(delegate); + this.operationPrefix = OperationNameFormatUtil.formatOperationName(delegate.getMethodDescriptor()) + SERVER; + this.contextSnapshot = contextSnapshot; + } + + @Override + public void sendMessage(RESPONSE message) { + // We just create the request on message span for server stream calls. + if (!getMethodDescriptor().getType().serverSendsOneMessage()) { + try { + final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + RESPONSE_ON_MESSAGE_OPERATION_NAME); + span.setComponent(ComponentsDefine.GRPC); + span.setLayer(SpanLayer.RPC_FRAMEWORK); + ContextManager.continued(contextSnapshot); + super.sendMessage(message); + } catch (Throwable t) { + ContextManager.activeSpan().errorOccurred().log(t); + throw t; + } finally { + ContextManager.stopSpan(); + } + } else { + super.sendMessage(message); + } + } + + @Override + public void close(Status status, Metadata trailers) { + try { + final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + RESPONSE_ON_CLOSE_OPERATION_NAME); + span.setComponent(ComponentsDefine.GRPC); + span.setLayer(SpanLayer.RPC_FRAMEWORK); + ContextManager.continued(contextSnapshot); + + switch (status.getCode()) { + case OK: + break; + // UNKNOWN/INTERNAL status code will case error in this span. + // Those status code means some unexpected error occurred in server. + // Similar to 5XX in HTTP status. + case UNKNOWN: + case INTERNAL: + if (status.getCause() == null) { + span.errorOccurred().log(status.asRuntimeException()); + } else { + span.errorOccurred().log(status.getCause()); + } + break; + // Other status code means some predictable error occurred in server. + // Like PERMISSION_DENIED or UNAUTHENTICATED somethings. + // Similar to 4XX in HTTP status. + default: Review comment: `UNKNOWN` and `INTERNAL` is similar to the `5xx` errors in HTTP, it must case span error even status has no cause exception. `default`(others) is similar to the `4xx` errors in HTTP, we just case error when status has cause exception for user debugging. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services