ikhoon commented on a change in pull request #4024: Add Armeria Java agent 
plugin
URL: https://github.com/apache/skywalking/pull/4024#discussion_r355167285
 
 

 ##########
 File path: 
apm-sniffer/apm-sdk-plugin/armeria-0.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/armeria/ArmeriaClientInterceptor.java
 ##########
 @@ -0,0 +1,108 @@
+/*
+ * 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.armeria;
+
+import com.linecorp.armeria.client.UserClient;
+import com.linecorp.armeria.common.HttpHeaders;
+import com.linecorp.armeria.common.HttpMethod;
+import com.linecorp.armeria.common.HttpRequest;
+import io.netty.util.AsciiString;
+import org.apache.skywalking.apm.agent.core.context.CarrierItem;
+import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+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.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
+import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
+import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
+
+import java.lang.reflect.Method;
+import java.net.URI;
+
+/**
+ * @author kezhenxu94
+ */
+@SuppressWarnings("rawtypes")
+public class ArmeriaClientInterceptor implements 
InstanceMethodsAroundInterceptor {
+    @Override
+    public void beforeMethod(
+        final EnhancedInstance objInst,
+        final Method method,
+        final Object[] allArguments,
+        final Class<?>[] argumentsTypes,
+        final MethodInterceptResult result) throws Throwable {
+
+        UserClient userClient = (UserClient) objInst;
+        URI uri = userClient.uri();
+        HttpMethod httpMethod = (HttpMethod) allArguments[1];
+        String path = (String) allArguments[2];
+        Object req = allArguments[5];
+
+        if (!(req instanceof HttpRequest)) {
+            return;
+        }
+
+        final HttpRequest httpReq = (HttpRequest) req;
+
+        ContextCarrier contextCarrier = new ContextCarrier();
+        String remotePeer = uri.getHost() + ":" + uri.getPort();
+
+        AbstractSpan exitSpan = ContextManager.createExitSpan(path, 
contextCarrier, remotePeer);
+
+        exitSpan.setComponent(ComponentsDefine.ARMERIA);
+        exitSpan.setLayer(SpanLayer.HTTP);
+        Tags.HTTP.METHOD.set(exitSpan, httpMethod.name());
+
+        HttpHeaders headers = httpReq.headers();
+        for (CarrierItem item = contextCarrier.items(); item.hasNext(); ) {
+            item = item.next();
+            headers.add(AsciiString.of(item.getHeadKey()), 
item.getHeadValue());
+        }
 
 Review comment:
   
[`HttpRequest#headers()`](https://line.github.io/armeria/apidocs/com/linecorp/armeria/common/HttpRequest.html#headers())
 now returns [`RequestHeaders`](
   
https://line.github.io/armeria/apidocs/com/linecorp/armeria/common/RequestHeaders.html)
 which dose not have `add` method because it is immutable.
   If you might want to use one of the following APIs.
   * 
https://line.github.io/armeria/apidocs/com/linecorp/armeria/common/RequestHeaders.html#toBuilder()
   * 
https://line.github.io/armeria/apidocs/com/linecorp/armeria/common/HttpHeaders.html#withMutations(java.util.function.Consumer)
   
   
   

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