You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2021/05/26 11:01:46 UTC

[skywalking] branch master updated: Optimize Class.getDeclaredField in Feign plugin (#7017)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/master by this push:
     new 7ae2b74  Optimize Class.getDeclaredField in Feign plugin (#7017)
7ae2b74 is described below

commit 7ae2b74a6499bcd2913ca77de3e33de7072b0f75
Author: Lu Jiajing <lu...@gmail.com>
AuthorDate: Wed May 26 19:01:16 2021 +0800

    Optimize Class.getDeclaredField in Feign plugin (#7017)
---
 CHANGES.md                                         |  1 +
 .../http/v9/DefaultHttpClientInterceptor.java      | 35 ++++++++++++++--------
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index bb05844..e22c381 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -32,6 +32,7 @@ Release Notes.
 * Introduce method interceptor API v2
 * Fix ClassCast issue for RequestHolder/ResponseHolder.
 * fixed `jdk-threading-plugin` memory leak.
+* Optimize multiple field reflection opeartion in Fiegn plugin.
 
 #### OAP-Backend
 * BugFix: filter invalid Envoy access logs whose socket address is empty.
diff --git a/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java b/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java
index c3c5861..6b91d15 100644
--- a/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java
+++ b/apm-sniffer/apm-sdk-plugin/feign-default-http-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/feign/http/v9/DefaultHttpClientInterceptor.java
@@ -50,6 +50,17 @@ import static feign.Util.valuesOrEmpty;
 public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterceptor {
 
     private static final String CONTENT_TYPE_HEADER = "Content-Type";
+    private static Field FIELD_HEADERS_OF_REQUEST;
+
+    static {
+        try {
+            final Field field = Request.class.getDeclaredField("headers");
+            field.setAccessible(true);
+            FIELD_HEADERS_OF_REQUEST = field;
+        } catch (Exception ignore) {
+            FIELD_HEADERS_OF_REQUEST = null;
+        }
+    }
 
     /**
      * Get the {@link feign.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, port,
@@ -101,19 +112,19 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
             }
         }
 
-        Field headersField = Request.class.getDeclaredField("headers");
-        headersField.setAccessible(true);
-        Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
-        CarrierItem next = contextCarrier.items();
-        while (next.hasNext()) {
-            next = next.next();
-            List<String> contextCollection = new ArrayList<String>(1);
-            contextCollection.add(next.getHeadValue());
-            headers.put(next.getHeadKey(), contextCollection);
-        }
-        headers.putAll(request.headers());
+        if (FIELD_HEADERS_OF_REQUEST != null) {
+            Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
+            CarrierItem next = contextCarrier.items();
+            while (next.hasNext()) {
+                next = next.next();
+                List<String> contextCollection = new ArrayList<String>(1);
+                contextCollection.add(next.getHeadValue());
+                headers.put(next.getHeadKey(), contextCollection);
+            }
+            headers.putAll(request.headers());
 
-        headersField.set(request, Collections.unmodifiableMap(headers));
+            FIELD_HEADERS_OF_REQUEST.set(request, Collections.unmodifiableMap(headers));
+        }
     }
 
     private void collectHttpBody(final Request request, final AbstractSpan span) {