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 2019/11/06 11:32:28 UTC

[skywalking] branch master updated: add SupplierWrapper to support java 1.8+ CompletableFuture.supplyAsync #3779 (#3783)

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 4c68870  add SupplierWrapper to support java 1.8+ CompletableFuture.supplyAsync #3779 (#3783)
4c68870 is described below

commit 4c688701dc26e8a54829de67e7ee493edd451af2
Author: sxzaihua <sx...@163.com>
AuthorDate: Wed Nov 6 19:32:16 2019 +0800

    add SupplierWrapper to support java 1.8+ CompletableFuture.supplyAsync #3779 (#3783)
    
    * add SupplierWrapper to support java 1.8+
    
    Signed-off-by: shixiang <xi...@bkjk.com>
    
    * add SupplierWrapper to support java 1.8+
    
    Signed-off-by: shixiang <xi...@bkjk.com>
    
    * Update SupplierWrapper.java
    
    add new line
    
    * add supplier
    
    * add supplier test
    
    * add test
    
    * add test yaml
    
    * Update Application-toolkit-trace-cross-thread.md
    
    Add how to use SupplierWrapper in Java 1.8+
---
 .../apm/toolkit/trace/SupplierWrapper.java         | 41 +++++++++++++++
 .../trace/CallableOrRunnableActivation.java        |  4 +-
 .../Application-toolkit-trace-cross-thread.md      | 19 ++++++-
 .../config/expectedData.yaml                       | 61 +++++++++++++++++++++-
 .../apm/toolkit/trace/SupplierWrapper.java         | 41 +++++++++++++++
 .../toolkit/controller/TestController.java         | 13 +++++
 .../testcase/toolkit/controller/TestService.java   |  8 +++
 7 files changed, 184 insertions(+), 3 deletions(-)

diff --git a/apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/SupplierWrapper.java b/apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/SupplierWrapper.java
new file mode 100644
index 0000000..31eda09
--- /dev/null
+++ b/apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/SupplierWrapper.java
@@ -0,0 +1,41 @@
+/*
+ * 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.toolkit.trace;
+
+import java.util.function.Supplier;
+
+/**
+ * @author sxzaihua
+ */
+@TraceCrossThread
+public class SupplierWrapper<V> implements Supplier<V> {
+    final Supplier<V> supplier;
+
+    public static <V> SupplierWrapper of(Supplier<V> r) {
+        return new SupplierWrapper<V>(r);
+    }
+
+    public SupplierWrapper(Supplier<V> supplier) {
+        this.supplier = supplier;
+    }
+
+    @Override
+    public V get() {
+        return supplier.get();
+    }
+}
diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/CallableOrRunnableActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/CallableOrRunnableActivation.java
index 739a0e2..99266d5 100644
--- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/CallableOrRunnableActivation.java
+++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/CallableOrRunnableActivation.java
@@ -40,6 +40,7 @@ public class CallableOrRunnableActivation extends ClassInstanceMethodsEnhancePlu
     private static final String CALL_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.CallableOrRunnableInvokeInterceptor";
     private static final String CALL_METHOD_NAME = "call";
     private static final String RUN_METHOD_NAME = "run";
+    private static final String GET_METHOD_NAME = "get";
 
     @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
         return new ConstructorInterceptPoint[] {
@@ -61,7 +62,8 @@ public class CallableOrRunnableActivation extends ClassInstanceMethodsEnhancePlu
             new InstanceMethodsInterceptPoint() {
                 @Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
                     return (named(CALL_METHOD_NAME).and(takesArguments(0)))
-                        .or(named(RUN_METHOD_NAME).and(takesArguments(0)));
+                        .or(named(RUN_METHOD_NAME).and(takesArguments(0)))
+                        .or(named(GET_METHOD_NAME).and(takesArguments(0)));
                 }
 
                 @Override public String getMethodsInterceptor() {
diff --git a/docs/en/setup/service-agent/java-agent/Application-toolkit-trace-cross-thread.md b/docs/en/setup/service-agent/java-agent/Application-toolkit-trace-cross-thread.md
index 2b00c44..cfd171b 100644
--- a/docs/en/setup/service-agent/java-agent/Application-toolkit-trace-cross-thread.md
+++ b/docs/en/setup/service-agent/java-agent/Application-toolkit-trace-cross-thread.md
@@ -39,7 +39,24 @@ or
         }
     }));
 ```
-
+* usage 3.
+```java
+    @TraceCrossThread
+    public class MySupplier<String> implements Supplier<String> {
+        @Override
+        public String get() {
+            return null;
+        }
+    }
+...
+    CompletableFuture.supplyAsync(new MySupplier<String>());
+```
+or 
+```java
+    CompletableFuture.supplyAsync(SupplierWrapper.of(()->{
+            return "SupplierWrapper";
+    })).thenAccept(System.out::println);
+```
 _Sample codes only_
 
 
diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml b/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml
index 4f4f865..ec49d3c 100644
--- a/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml
+++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml
@@ -22,7 +22,7 @@ registryItems:
   operationNames:
     - apm-toolkit-trace-scenario: [/apm-toolkit-trace-scenario/case/asyncVisit/runnable,
                                    /case/asyncVisit/runnable, /case/asyncVisit/callable, /apm-toolkit-trace-scenario/case/asyncVisit/callable,
-                                   /case/tool-kit]
+                                   /case/tool-kit,/apm-toolkit-trace-scenario/case/asyncVisit/supplier,/case/asyncVisit/supplier]
   heartbeat: []
 segmentItems:
   - applicationCode: apm-toolkit-trace-scenario
@@ -261,4 +261,63 @@ segmentItems:
               - {parentEndpointId: 0, parentEndpoint: Thread/org.apache.skywalking.apm.toolkit.trace.RunnableWrapper/run,
                  networkAddressId: 0, entryEndpointId: 0, refType: CrossProcess, parentSpanId: 1,
                  parentTraceSegmentId: not null, parentServiceInstanceId: 1,
+                 networkAddress: 'localhost:8080', entryEndpoint: /case/tool-kit, entryServiceInstanceId: 1}
+      - segmentId: not null
+        spans:
+          - operationName: /apm-toolkit-trace-scenario/case/asyncVisit/supplier
+            operationId: 0
+            parentSpanId: 0
+            spanId: 1
+            spanLayer: Http
+            startTime: nq 0
+            endTime: nq 0
+            componentId: 2
+            componentName: ''
+            isError: false
+            spanType: Exit
+            peer: localhost:8080
+            peerId: 0
+            tags:
+              - {key: url, value: 'http://localhost:8080/apm-toolkit-trace-scenario/case/asyncVisit/supplier'}
+              - {key: http.method, value: GET}
+          - operationName: Thread/org.apache.skywalking.apm.toolkit.trace.SupplierWrapper/get
+            operationId: 0
+            parentSpanId: -1
+            spanId: 0
+            spanLayer: Unknown
+            startTime: nq 0
+            endTime: nq 0
+            componentId: 0
+            componentName: ''
+            isError: false
+            spanType: Local
+            peer: ''
+            peerId: 0
+            refs:
+              - {parentEndpointId: 0, parentEndpoint: /case/tool-kit, networkAddressId: 0,
+                 entryEndpointId: 0, refType: CrossThread, parentSpanId: 0, parentTraceSegmentId: not null,
+                 parentServiceInstanceId: 1, networkAddress: '', entryEndpoint: /case/tool-kit,
+                 entryServiceInstanceId: 1}
+      - segmentId: not null
+        spans:
+          - operationName: /case/asyncVisit/supplier
+            operationId: 0
+            parentSpanId: -1
+            spanId: 0
+            spanLayer: Http
+            startTime: nq 0
+            endTime: nq 0
+            componentId: 14
+            componentName: ''
+            isError: false
+            spanType: Entry
+            peer: ''
+            peerId: 0
+            tags:
+              - {key: url, value: 'http://localhost:8080/apm-toolkit-trace-scenario/case/asyncVisit/supplier'}
+              - {key: http.method, value: GET}
+            refs:
+              - {parentEndpointId: 0, parentEndpoint: Thread/org.apache.skywalking.apm.toolkit.trace.SupplierWrapper/get,
+                 networkAddressId: 0, entryEndpointId: 0, refType: CrossProcess, parentSpanId: 1,
+                 parentTraceSegmentId: not null, parentServiceInstanceId: 1,
                  networkAddress: 'localhost:8080', entryEndpoint: /case/tool-kit, entryServiceInstanceId: 1}
\ No newline at end of file
diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/SupplierWrapper.java b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/SupplierWrapper.java
new file mode 100644
index 0000000..31eda09
--- /dev/null
+++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/SupplierWrapper.java
@@ -0,0 +1,41 @@
+/*
+ * 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.toolkit.trace;
+
+import java.util.function.Supplier;
+
+/**
+ * @author sxzaihua
+ */
+@TraceCrossThread
+public class SupplierWrapper<V> implements Supplier<V> {
+    final Supplier<V> supplier;
+
+    public static <V> SupplierWrapper of(Supplier<V> r) {
+        return new SupplierWrapper<V>(r);
+    }
+
+    public SupplierWrapper(Supplier<V> supplier) {
+        this.supplier = supplier;
+    }
+
+    @Override
+    public V get() {
+        return supplier.get();
+    }
+}
diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestController.java b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestController.java
index dc17214..d793dda 100644
--- a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestController.java
+++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestController.java
@@ -62,6 +62,14 @@ public class TestController {
                 // ignore
             }
         });
+        testService.asyncSupplier(()->{
+            try {
+                visit("http://localhost:8080/apm-toolkit-trace-scenario/case/asyncVisit/supplier");
+            } catch (IOException e) {
+                // ignore
+            }
+            return true;
+        });
         return SUCCESS;
     }
 
@@ -80,6 +88,11 @@ public class TestController {
         return SUCCESS;
     }
 
+    @RequestMapping("/asyncVisit/supplier")
+    public String asyncVisitSupplier() {
+    	return SUCCESS;
+    }
+
 
     private static void visit(String url) throws IOException {
         CloseableHttpClient httpclient = HttpClients.createDefault();
diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestService.java b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestService.java
index b4b8228..dc384f4 100644
--- a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestService.java
+++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/org/apache/skywalking/apm/testcase/toolkit/controller/TestService.java
@@ -21,12 +21,15 @@ package test.org.apache.skywalking.apm.testcase.toolkit.controller;
 import org.apache.skywalking.apm.toolkit.trace.ActiveSpan;
 import org.apache.skywalking.apm.toolkit.trace.CallableWrapper;
 import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper;
+import org.apache.skywalking.apm.toolkit.trace.SupplierWrapper;
 import org.apache.skywalking.apm.toolkit.trace.Trace;
 import org.springframework.stereotype.Component;
 
 import java.util.concurrent.Callable;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.function.Supplier;
 
 /**
  * @author caoyixiong
@@ -78,4 +81,9 @@ public class TestService {
     public void asyncCallable(Callable<Boolean> callable) {
         SERVICE.submit(CallableWrapper.of(callable));
     }
+
+    public void asyncSupplier(Supplier<Boolean> supplier) {
+    	CompletableFuture.supplyAsync(SupplierWrapper.of(supplier));
+    }
+
 }