wu-sheng commented on a change in pull request #4177: Enhance gRPC plugin
URL: https://github.com/apache/skywalking/pull/4177#discussion_r371105093
 
 

 ##########
 File path: 
apm-sniffer/apm-sdk-plugin/grpc-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/grpc/v1/server/TracingServerCallListener.java
 ##########
 @@ -0,0 +1,102 @@
+/*
+ * 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.ForwardingServerCallListener;
+import io.grpc.MethodDescriptor;
+import io.grpc.ServerCall;
+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.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 TracingServerCallListener<REQUEST> extends 
ForwardingServerCallListener.SimpleForwardingServerCallListener<REQUEST> {
+
+    private final ContextSnapshot contextSnapshot;
+    private final MethodDescriptor.MethodType methodType;
+    private final String operationPrefix;
+
+    protected TracingServerCallListener(ServerCall.Listener<REQUEST> delegate, 
MethodDescriptor<REQUEST, ?> descriptor,
+                                        ContextSnapshot contextSnapshot) {
+        super(delegate);
+        this.contextSnapshot = contextSnapshot;
+        this.methodType = descriptor.getType();
+        this.operationPrefix = 
OperationNameFormatUtil.formatOperationName(descriptor) + SERVER;
+    }
+
+    @Override
+    public void onMessage(REQUEST message) {
+        // We just create the request on message span for client stream calls.
+        if (!methodType.clientSendsOneMessage()) {
+            try {
+                final AbstractSpan span = 
ContextManager.createLocalSpan(operationPrefix + 
REQUEST_ON_MESSAGE_OPERATION_NAME);
+                span.setComponent(ComponentsDefine.GRPC);
+                span.setLayer(SpanLayer.RPC_FRAMEWORK);
+                ContextManager.continued(contextSnapshot);
+                super.onMessage(message);
+            } catch (Throwable t) {
+                ContextManager.activeSpan().errorOccurred().log(t);
+                throw t;
+            } finally {
+                ContextManager.stopSpan();
+            }
+        } else {
+            super.onMessage(message);
+        }
+    }
+
+    @Override
+    public void onCancel() {
+        try {
+            final AbstractSpan span = 
ContextManager.createLocalSpan(operationPrefix + 
REQUEST_ON_CANCEL_OPERATION_NAME);
+            span.setComponent(ComponentsDefine.GRPC);
+            span.setLayer(SpanLayer.RPC_FRAMEWORK);
+            ContextManager.continued(contextSnapshot);
+            super.onCancel();
+        } catch (Throwable t) {
+            ContextManager.activeSpan().errorOccurred().log(t);
+            throw t;
+        } finally {
+            ContextManager.stopSpan();
+        }
+    }
+
+    @Override
+    public void onHalfClose() {
+        try {
+            final AbstractSpan span = 
ContextManager.createLocalSpan(operationPrefix + 
REQUEST_ON_COMPLETE_OPERATION_NAME);
+            span.setComponent(ComponentsDefine.GRPC);
+            span.setLayer(SpanLayer.RPC_FRAMEWORK);
+            ContextManager.continued(contextSnapshot);
+            super.onHalfClose();
 
 Review comment:
   A similar suggestion, `try/final` should be added here.

----------------------------------------------------------------
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

Reply via email to