From d51c78811350f6eba2a741beb01cb762f1ac779e Mon Sep 17 00:00:00 2001 From: Jared Tan Date: Wed, 29 Apr 2020 19:39:52 +0800 Subject: [PATCH] support user to customize an endpoint (#4716) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * support user to customize an endpoint with toolkit. * fix code style. * add md. * Update docs/en/setup/service-agent/java-agent/Application-toolkit-trace.md Co-Authored-By: 吴晟 Wu Sheng * add test case. * fix expecteddata.tml. * fix data. * fix expecteddata.tml. * remove operationId in expectedData.yaml. Co-authored-by: 吴晟 Wu Sheng --- .../apm/toolkit/trace/ActiveSpan.java | 3 + .../trace/ActiveSpanActivation.java | 23 +++++++- ...ActiveSpanSetOperationNameInterceptor.java | 58 +++++++++++++++++++ .../java-agent/Application-toolkit-trace.md | 2 + .../config/expectedData.yaml | 40 +++++-------- .../apm/toolkit/trace/ActiveSpan.java | 3 + .../toolkit/controller/TestController.java | 1 + .../toolkit/controller/TestService.java | 8 ++- 8 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanSetOperationNameInterceptor.java diff --git a/apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java b/apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java index 2878905d3c..6954a40bc1 100644 --- a/apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java +++ b/apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java @@ -43,4 +43,7 @@ public class ActiveSpan { public static void info(String infoMsg) { } + + public static void setOperationName(String operationName) { + } } diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanActivation.java index 9303969d6a..b4f9b1eca0 100644 --- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanActivation.java +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanActivation.java @@ -20,11 +20,11 @@ package org.apache.skywalking.apm.toolkit.activation.trace; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine; -import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; @@ -52,6 +52,9 @@ public class ActiveSpanActivation extends ClassStaticMethodsEnhancePluginDefine private static final String INFO_INTERCEPTOR_METHOD_NAME = "info"; private static final String INFO_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.ActiveSpanInfoInterceptor"; + private static final String SET_OPERATION_NAME_METHOD_NAME = "setOperationName"; + private static final String SET_OPERATION_NAME_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.ActiveSpanSetOperationNameInterceptor"; + @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[0]; @@ -151,6 +154,22 @@ public class ActiveSpanActivation extends ClassStaticMethodsEnhancePluginDefine return ERROR_INTERCEPTOR_CLASS; } + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new StaticMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(SET_OPERATION_NAME_METHOD_NAME).and(takesArgumentWithType(0, "java.lang.String")); + } + + @Override + public String getMethodsInterceptor() { + return SET_OPERATION_NAME_INTERCEPTOR_CLASS; + } + @Override public boolean isOverrideArgs() { return false; diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanSetOperationNameInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanSetOperationNameInterceptor.java new file mode 100644 index 0000000000..b4293f4060 --- /dev/null +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/ActiveSpanSetOperationNameInterceptor.java @@ -0,0 +1,58 @@ +/* + * 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.activation.trace; + +import java.lang.reflect.Method; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor; + +public class ActiveSpanSetOperationNameInterceptor implements StaticMethodsAroundInterceptor { + @Override + public void beforeMethod(final Class clazz, + final Method method, + final Object[] allArguments, + final Class[] parameterTypes, + final MethodInterceptResult result) { + try { + AbstractSpan activeSpan = ContextManager.activeSpan(); + activeSpan.setOperationName(String.valueOf(allArguments[0])); + } catch (NullPointerException ignored) { + } + } + + @Override + public Object afterMethod(final Class clazz, + final Method method, + final Object[] allArguments, + final Class[] parameterTypes, + final Object ret) { + return null; + } + + @Override + public void handleMethodException(final Class clazz, + final Method method, + final Object[] allArguments, + final Class[] parameterTypes, + final Throwable t) { + + } +} diff --git a/docs/en/setup/service-agent/java-agent/Application-toolkit-trace.md b/docs/en/setup/service-agent/java-agent/Application-toolkit-trace.md index 3586367418..8d4e95af7b 100644 --- a/docs/en/setup/service-agent/java-agent/Application-toolkit-trace.md +++ b/docs/en/setup/service-agent/java-agent/Application-toolkit-trace.md @@ -27,6 +27,7 @@ The `value` of `Tag` is the same as what are supported in [Customize Enhance Tra * `ActiveSpan.error(Throwable throwable)` Mark the current span as error status with a Throwable. * `ActiveSpan.debug(String debugMsg)` Add a debug level log message in the current span. * `ActiveSpan.info(String infoMsg)` Add an info level log message in the current span. +* `ActiveSpan.setOperationName(String operationName)` Customize an operation name. ```java ActiveSpan.tag("my_tag", "my_value"); @@ -49,6 +50,7 @@ ActiveSpan.debug("Test-debug-Msg"); @Tag(key = "username", value = "returnedObj.username") @Tag(key = "age", value = "returnedObj.age") public User methodYouWantToTrace(String param1, String param2) { + // ActiveSpan.setOperationName("Customize your own operation name, if this is an entry span, this would be an endpoint name"); // ... } ``` 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 0e66dddc75..5bad99e9e3 100644 --- a/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml @@ -19,10 +19,12 @@ segmentItems: segments: - segmentId: not null spans: + - {operationName: tool-kit-set-operation-name, parentSpanId: 0, + spanId: 1, spanLayer: Unknown, startTime: nq 0, endTime: nq 0, + componentId: 0, isError: false, spanType: Local, peer: '', skipAnalysis: true} - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTag() - operationId: 0 parentSpanId: 0 - spanId: 1 + spanId: 2 spanLayer: Unknown startTime: nq 0 endTime: nq 0 @@ -34,9 +36,8 @@ segmentItems: - {key: key, value: value} skipAnalysis: 'true' - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testInfo(java.lang.String) - operationId: 0 parentSpanId: 0 - spanId: 2 + spanId: 3 spanLayer: Unknown startTime: nq 0 endTime: nq 0 @@ -52,9 +53,8 @@ segmentItems: - {key: testTag, value: testInfoParam} skipAnalysis: 'true' - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testDebug() - operationId: 0 parentSpanId: 0 - spanId: 3 + spanId: 4 spanLayer: Unknown startTime: nq 0 endTime: nq 0 @@ -68,13 +68,12 @@ segmentItems: - {key: message, value: TestDebugMsg} skipAnalysis: 'true' - {operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testError(), - operationId: 0, parentSpanId: 0, spanId: 4, spanLayer: Unknown, startTime: nq - 0, endTime: nq 0, componentId: 0, isError: true, spanType: Local, peer: '', + parentSpanId: 0, spanId: 5, spanLayer: Unknown, startTime: nq 0, endTime: nq 0, + componentId: 0, isError: true, spanType: Local, peer: '', skipAnalysis: 'true'} - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testErrorMsg() - operationId: 0 parentSpanId: 0 - spanId: 5 + spanId: 6 spanLayer: Unknown startTime: nq 0 endTime: nq 0 @@ -88,9 +87,8 @@ segmentItems: - {key: message, value: TestErrorMsg} skipAnalysis: 'true' - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testErrorThrowable() - operationId: 0 parentSpanId: 0 - spanId: 6 + spanId: 7 spanLayer: Unknown startTime: nq 0 endTime: nq 0 @@ -106,9 +104,8 @@ segmentItems: - {key: stack, value: not null} skipAnalysis: 'true' - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTagAnnotation(java.lang.String,java.lang.String) - operationId: 0 parentSpanId: 0 - spanId: 7 + spanId: 8 spanLayer: Unknown startTime: nq 0 endTime: nq 0 @@ -121,9 +118,8 @@ segmentItems: - {key: p2, value: testTagAnnotationParam2} skipAnalysis: 'true' - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTagAnnotationReturnInfo(java.lang.String,java.lang.Integer) - operationId: 0 parentSpanId: 0 - spanId: 8 + spanId: 9 spanLayer: Unknown startTime: nq 0 endTime: nq 0 @@ -135,7 +131,6 @@ segmentItems: - {key: username, value: zhangsan} skipAnalysis: 'true' - operationName: /case/tool-kit - operationId: 0 parentSpanId: -1 spanId: 0 spanLayer: Http @@ -153,10 +148,10 @@ segmentItems: - {parentEndpoint: /app, networkAddress: '127.0.0.1:8080', refType: CrossProcess, parentSpanId: 4, parentTraceSegmentId: 1.2.3, parentServiceInstance: instance, parentService: service, traceId: 3.4.5} + - segmentId: not null spans: - operationName: /case/asyncVisit/callable - operationId: 0 parentSpanId: -1 spanId: 0 spanLayer: Http @@ -179,7 +174,6 @@ segmentItems: - segmentId: not null spans: - operationName: /apm-toolkit-trace-scenario/case/asyncVisit/runnable - operationId: 0 parentSpanId: 0 spanId: 1 spanLayer: Http @@ -194,7 +188,6 @@ segmentItems: - {key: http.method, value: GET} skipAnalysis: 'true' - operationName: Thread/org.apache.skywalking.apm.toolkit.trace.RunnableWrapper/run - operationId: 0 parentSpanId: -1 spanId: 0 spanLayer: Unknown @@ -212,7 +205,6 @@ segmentItems: - segmentId: not null spans: - operationName: /apm-toolkit-trace-scenario/case/asyncVisit/callable - operationId: 0 parentSpanId: 0 spanId: 1 spanLayer: Http @@ -227,7 +219,7 @@ segmentItems: - {key: http.method, value: GET} skipAnalysis: 'true' - operationName: Thread/org.apache.skywalking.apm.toolkit.trace.CallableWrapper/call - operationId: 0 + parentSpanId: -1 spanId: 0 spanLayer: Unknown @@ -245,7 +237,6 @@ segmentItems: - segmentId: not null spans: - operationName: /case/asyncVisit/runnable - operationId: 0 parentSpanId: -1 spanId: 0 spanLayer: Http @@ -268,7 +259,6 @@ segmentItems: - segmentId: not null spans: - operationName: /apm-toolkit-trace-scenario/case/asyncVisit/supplier - operationId: 0 parentSpanId: 0 spanId: 1 spanLayer: Http @@ -283,7 +273,6 @@ segmentItems: - {key: http.method, value: GET} skipAnalysis: 'true' - operationName: Thread/org.apache.skywalking.apm.toolkit.trace.SupplierWrapper/get - operationId: 0 parentSpanId: -1 spanId: 0 spanLayer: Unknown @@ -301,7 +290,6 @@ segmentItems: - segmentId: not null spans: - operationName: /case/asyncVisit/supplier - operationId: 0 parentSpanId: -1 spanId: 0 spanLayer: Http diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java index 2878905d3c..6954a40bc1 100644 --- a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java +++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/ActiveSpan.java @@ -43,4 +43,7 @@ public class ActiveSpan { public static void info(String infoMsg) { } + + public static void setOperationName(String operationName) { + } } 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 efd761cb5b..1b22c7bd05 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 @@ -46,6 +46,7 @@ public class TestController { @RequestMapping("/tool-kit") public String toolKitCase() { + testService.testSetOperationName("tool-kit-set-operation-name"); testService.testTag(); testService.testInfo("testInfoParam"); testService.testDebug(); 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 4c8827dd10..ce7f44f7fa 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 @@ -18,12 +18,12 @@ package test.org.apache.skywalking.apm.testcase.toolkit.controller; -import org.apache.skywalking.apm.toolkit.model.User; 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; +import org.apache.skywalking.apm.toolkit.model.User; import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; import org.apache.skywalking.apm.toolkit.trace.CallableWrapper; import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper; @@ -78,6 +78,7 @@ public class TestService { public User testTagAnnotationReturnInfo(final String username, final Integer age) { return new User(username, age); } + @Trace @Tag(key = "testTag", value = "arg[0]") public void testInfo(final String testInfoParam) { @@ -96,4 +97,9 @@ public class TestService { CompletableFuture.supplyAsync(SupplierWrapper.of(supplier)); } + @Trace + public void testSetOperationName(String operationName) { + ActiveSpan.setOperationName(operationName); + } + } -- GitLab