未验证 提交 d51c7881 编写于 作者: J Jared Tan 提交者: GitHub

support user to customize an endpoint (#4716)

* 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's avatar吴晟 Wu Sheng <wu.sheng@foxmail.com>

* add test case.

* fix expecteddata.tml.

* fix data.

* fix expecteddata.tml.

* remove operationId in expectedData.yaml.
Co-authored-by: wu-sheng's avatar吴晟 Wu Sheng <wu.sheng@foxmail.com>
上级 217bb624
...@@ -43,4 +43,7 @@ public class ActiveSpan { ...@@ -43,4 +43,7 @@ public class ActiveSpan {
public static void info(String infoMsg) { public static void info(String infoMsg) {
} }
public static void setOperationName(String operationName) {
}
} }
...@@ -20,11 +20,11 @@ package org.apache.skywalking.apm.toolkit.activation.trace; ...@@ -20,11 +20,11 @@ package org.apache.skywalking.apm.toolkit.activation.trace;
import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher; 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.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; 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.ClassMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
...@@ -52,6 +52,9 @@ public class ActiveSpanActivation extends ClassStaticMethodsEnhancePluginDefine ...@@ -52,6 +52,9 @@ public class ActiveSpanActivation extends ClassStaticMethodsEnhancePluginDefine
private static final String INFO_INTERCEPTOR_METHOD_NAME = "info"; 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 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 @Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0]; return new ConstructorInterceptPoint[0];
...@@ -151,6 +154,22 @@ public class ActiveSpanActivation extends ClassStaticMethodsEnhancePluginDefine ...@@ -151,6 +154,22 @@ public class ActiveSpanActivation extends ClassStaticMethodsEnhancePluginDefine
return ERROR_INTERCEPTOR_CLASS; return ERROR_INTERCEPTOR_CLASS;
} }
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> 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 @Override
public boolean isOverrideArgs() { public boolean isOverrideArgs() {
return false; return false;
......
/*
* 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) {
}
}
...@@ -27,6 +27,7 @@ The `value` of `Tag` is the same as what are supported in [Customize Enhance Tra ...@@ -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.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.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.info(String infoMsg)` Add an info level log message in the current span.
* `ActiveSpan.setOperationName(String operationName)` Customize an operation name.
```java ```java
ActiveSpan.tag("my_tag", "my_value"); ActiveSpan.tag("my_tag", "my_value");
...@@ -49,6 +50,7 @@ ActiveSpan.debug("Test-debug-Msg"); ...@@ -49,6 +50,7 @@ ActiveSpan.debug("Test-debug-Msg");
@Tag(key = "username", value = "returnedObj.username") @Tag(key = "username", value = "returnedObj.username")
@Tag(key = "age", value = "returnedObj.age") @Tag(key = "age", value = "returnedObj.age")
public User methodYouWantToTrace(String param1, String param2) { 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");
// ... // ...
} }
``` ```
......
...@@ -19,10 +19,12 @@ segmentItems: ...@@ -19,10 +19,12 @@ segmentItems:
segments: segments:
- segmentId: not null - segmentId: not null
spans: 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() - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTag()
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 1 spanId: 2
spanLayer: Unknown spanLayer: Unknown
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -34,9 +36,8 @@ segmentItems: ...@@ -34,9 +36,8 @@ segmentItems:
- {key: key, value: value} - {key: key, value: value}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testInfo(java.lang.String) - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testInfo(java.lang.String)
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 2 spanId: 3
spanLayer: Unknown spanLayer: Unknown
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -52,9 +53,8 @@ segmentItems: ...@@ -52,9 +53,8 @@ segmentItems:
- {key: testTag, value: testInfoParam} - {key: testTag, value: testInfoParam}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testDebug() - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testDebug()
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 3 spanId: 4
spanLayer: Unknown spanLayer: Unknown
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -68,13 +68,12 @@ segmentItems: ...@@ -68,13 +68,12 @@ segmentItems:
- {key: message, value: TestDebugMsg} - {key: message, value: TestDebugMsg}
skipAnalysis: 'true' skipAnalysis: 'true'
- {operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testError(), - {operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testError(),
operationId: 0, parentSpanId: 0, spanId: 4, spanLayer: Unknown, startTime: nq parentSpanId: 0, spanId: 5, spanLayer: Unknown, startTime: nq 0, endTime: nq 0,
0, endTime: nq 0, componentId: 0, isError: true, spanType: Local, peer: '', componentId: 0, isError: true, spanType: Local, peer: '',
skipAnalysis: 'true'} skipAnalysis: 'true'}
- operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testErrorMsg() - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testErrorMsg()
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 5 spanId: 6
spanLayer: Unknown spanLayer: Unknown
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -88,9 +87,8 @@ segmentItems: ...@@ -88,9 +87,8 @@ segmentItems:
- {key: message, value: TestErrorMsg} - {key: message, value: TestErrorMsg}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testErrorThrowable() - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testErrorThrowable()
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 6 spanId: 7
spanLayer: Unknown spanLayer: Unknown
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -106,9 +104,8 @@ segmentItems: ...@@ -106,9 +104,8 @@ segmentItems:
- {key: stack, value: not null} - {key: stack, value: not null}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTagAnnotation(java.lang.String,java.lang.String) - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTagAnnotation(java.lang.String,java.lang.String)
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 7 spanId: 8
spanLayer: Unknown spanLayer: Unknown
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -121,9 +118,8 @@ segmentItems: ...@@ -121,9 +118,8 @@ segmentItems:
- {key: p2, value: testTagAnnotationParam2} - {key: p2, value: testTagAnnotationParam2}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTagAnnotationReturnInfo(java.lang.String,java.lang.Integer) - operationName: test.org.apache.skywalking.apm.testcase.toolkit.controller.TestService.testTagAnnotationReturnInfo(java.lang.String,java.lang.Integer)
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 8 spanId: 9
spanLayer: Unknown spanLayer: Unknown
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
...@@ -135,7 +131,6 @@ segmentItems: ...@@ -135,7 +131,6 @@ segmentItems:
- {key: username, value: zhangsan} - {key: username, value: zhangsan}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: /case/tool-kit - operationName: /case/tool-kit
operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Http spanLayer: Http
...@@ -153,10 +148,10 @@ segmentItems: ...@@ -153,10 +148,10 @@ segmentItems:
- {parentEndpoint: /app, networkAddress: '127.0.0.1:8080', refType: CrossProcess, - {parentEndpoint: /app, networkAddress: '127.0.0.1:8080', refType: CrossProcess,
parentSpanId: 4, parentTraceSegmentId: 1.2.3, parentServiceInstance: instance, parentSpanId: 4, parentTraceSegmentId: 1.2.3, parentServiceInstance: instance,
parentService: service, traceId: 3.4.5} parentService: service, traceId: 3.4.5}
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: /case/asyncVisit/callable - operationName: /case/asyncVisit/callable
operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Http spanLayer: Http
...@@ -179,7 +174,6 @@ segmentItems: ...@@ -179,7 +174,6 @@ segmentItems:
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: /apm-toolkit-trace-scenario/case/asyncVisit/runnable - operationName: /apm-toolkit-trace-scenario/case/asyncVisit/runnable
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 1 spanId: 1
spanLayer: Http spanLayer: Http
...@@ -194,7 +188,6 @@ segmentItems: ...@@ -194,7 +188,6 @@ segmentItems:
- {key: http.method, value: GET} - {key: http.method, value: GET}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: Thread/org.apache.skywalking.apm.toolkit.trace.RunnableWrapper/run - operationName: Thread/org.apache.skywalking.apm.toolkit.trace.RunnableWrapper/run
operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Unknown spanLayer: Unknown
...@@ -212,7 +205,6 @@ segmentItems: ...@@ -212,7 +205,6 @@ segmentItems:
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: /apm-toolkit-trace-scenario/case/asyncVisit/callable - operationName: /apm-toolkit-trace-scenario/case/asyncVisit/callable
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 1 spanId: 1
spanLayer: Http spanLayer: Http
...@@ -227,7 +219,7 @@ segmentItems: ...@@ -227,7 +219,7 @@ segmentItems:
- {key: http.method, value: GET} - {key: http.method, value: GET}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: Thread/org.apache.skywalking.apm.toolkit.trace.CallableWrapper/call - operationName: Thread/org.apache.skywalking.apm.toolkit.trace.CallableWrapper/call
operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Unknown spanLayer: Unknown
...@@ -245,7 +237,6 @@ segmentItems: ...@@ -245,7 +237,6 @@ segmentItems:
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: /case/asyncVisit/runnable - operationName: /case/asyncVisit/runnable
operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Http spanLayer: Http
...@@ -268,7 +259,6 @@ segmentItems: ...@@ -268,7 +259,6 @@ segmentItems:
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: /apm-toolkit-trace-scenario/case/asyncVisit/supplier - operationName: /apm-toolkit-trace-scenario/case/asyncVisit/supplier
operationId: 0
parentSpanId: 0 parentSpanId: 0
spanId: 1 spanId: 1
spanLayer: Http spanLayer: Http
...@@ -283,7 +273,6 @@ segmentItems: ...@@ -283,7 +273,6 @@ segmentItems:
- {key: http.method, value: GET} - {key: http.method, value: GET}
skipAnalysis: 'true' skipAnalysis: 'true'
- operationName: Thread/org.apache.skywalking.apm.toolkit.trace.SupplierWrapper/get - operationName: Thread/org.apache.skywalking.apm.toolkit.trace.SupplierWrapper/get
operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Unknown spanLayer: Unknown
...@@ -301,7 +290,6 @@ segmentItems: ...@@ -301,7 +290,6 @@ segmentItems:
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: /case/asyncVisit/supplier - operationName: /case/asyncVisit/supplier
operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Http spanLayer: Http
......
...@@ -43,4 +43,7 @@ public class ActiveSpan { ...@@ -43,4 +43,7 @@ public class ActiveSpan {
public static void info(String infoMsg) { public static void info(String infoMsg) {
} }
public static void setOperationName(String operationName) {
}
} }
...@@ -46,6 +46,7 @@ public class TestController { ...@@ -46,6 +46,7 @@ public class TestController {
@RequestMapping("/tool-kit") @RequestMapping("/tool-kit")
public String toolKitCase() { public String toolKitCase() {
testService.testSetOperationName("tool-kit-set-operation-name");
testService.testTag(); testService.testTag();
testService.testInfo("testInfoParam"); testService.testInfo("testInfoParam");
testService.testDebug(); testService.testDebug();
......
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
package test.org.apache.skywalking.apm.testcase.toolkit.controller; 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.Callable;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.function.Supplier; 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.ActiveSpan;
import org.apache.skywalking.apm.toolkit.trace.CallableWrapper; import org.apache.skywalking.apm.toolkit.trace.CallableWrapper;
import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper; import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper;
...@@ -78,6 +78,7 @@ public class TestService { ...@@ -78,6 +78,7 @@ public class TestService {
public User testTagAnnotationReturnInfo(final String username, final Integer age) { public User testTagAnnotationReturnInfo(final String username, final Integer age) {
return new User(username, age); return new User(username, age);
} }
@Trace @Trace
@Tag(key = "testTag", value = "arg[0]") @Tag(key = "testTag", value = "arg[0]")
public void testInfo(final String testInfoParam) { public void testInfo(final String testInfoParam) {
...@@ -96,4 +97,9 @@ public class TestService { ...@@ -96,4 +97,9 @@ public class TestService {
CompletableFuture.supplyAsync(SupplierWrapper.of(supplier)); CompletableFuture.supplyAsync(SupplierWrapper.of(supplier));
} }
@Trace
public void testSetOperationName(String operationName) {
ActiveSpan.setOperationName(operationName);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册