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 0000000000000000000000000000000000000000..31eda0996f4fa51229e7e720268a6932b3261238 --- /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 implements Supplier { + final Supplier supplier; + + public static SupplierWrapper of(Supplier r) { + return new SupplierWrapper(r); + } + + public SupplierWrapper(Supplier 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 739a0e267eea15c74dc29131e25bbb6e0777f99b..99266d5195b95b22e9f3d3cfc8d350b912cbbd07 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 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 2b00c44206ce9cef0588ac1ee212431166fcc986..cfd171bb41234d2ee17401a1ac61596be4fa6347 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 implements Supplier { + @Override + public String get() { + return null; + } + } +... + CompletableFuture.supplyAsync(new MySupplier()); +``` +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 4f4f8655f81cf4bc6e6d63b88c1b87dfaab6b63e..ec49d3c6ba77e3ee645dc900ea40619ab7281e42 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 @@ -259,6 +259,65 @@ segmentItems: - {key: http.method, value: GET} refs: - {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 0000000000000000000000000000000000000000..31eda0996f4fa51229e7e720268a6932b3261238 --- /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 implements Supplier { + final Supplier supplier; + + public static SupplierWrapper of(Supplier r) { + return new SupplierWrapper(r); + } + + public SupplierWrapper(Supplier 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 dc1721475152807265b816b737c34ede7658b189..d793ddad99caf208a3d2dee0e69e2de043b6c006 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 b4b82281f8f9e27ea1e7713291da580f4608a09a..dc384f4cfd29f0756b16d2dd0b3282b05c96867d 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 callable) { SERVICE.submit(CallableWrapper.of(callable)); } + + public void asyncSupplier(Supplier supplier) { + CompletableFuture.supplyAsync(SupplierWrapper.of(supplier)); + } + }