change: nutz http 1.x without java.lang.reflect.*
use ConstructorInterceptPoint to get "org.nutz.http.Request", and store in SkyWalkingDynamicField, then set header in SenderSendInterceptor
This commit is contained in:
parent
64b6c92292
commit
368535cea1
|
|
@ -0,0 +1,12 @@
|
|||
package org.skywalking.apm.plugin.nutz.http.sync;
|
||||
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
|
||||
|
||||
public class SenderConstructorInterceptor implements InstanceConstructorInterceptor {
|
||||
|
||||
@Override
|
||||
public void onConstruct(final EnhancedInstance objInst, final Object[] allArguments) {
|
||||
objInst.setSkyWalkingDynamicField(allArguments[0]);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,11 @@
|
|||
package org.skywalking.apm.plugin.nutz.http.sync;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
|
||||
import org.nutz.http.Request;
|
||||
import org.nutz.http.Request.METHOD;
|
||||
import org.nutz.http.Response;
|
||||
import org.nutz.http.Sender;
|
||||
import org.skywalking.apm.agent.core.conf.Config;
|
||||
import org.skywalking.apm.agent.core.context.ContextCarrier;
|
||||
import org.skywalking.apm.agent.core.context.ContextManager;
|
||||
|
|
@ -22,11 +20,9 @@ import org.skywalking.apm.network.trace.component.ComponentsDefine;
|
|||
public class SenderSendInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
|
||||
@Override
|
||||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
MethodInterceptResult result) throws Throwable {
|
||||
Field field = Sender.class.getDeclaredField("request");
|
||||
field.setAccessible(true);
|
||||
Request req = (Request) field.get(objInst);
|
||||
public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments, final Class<?>[] argumentsTypes,
|
||||
final MethodInterceptResult result) throws Throwable {
|
||||
Request req = (Request) objInst.getSkyWalkingDynamicField();
|
||||
final URI requestURL = req.getUrl().toURI();
|
||||
final METHOD httpMethod = req.getMethod();
|
||||
final ContextCarrier contextCarrier = new ContextCarrier();
|
||||
|
|
@ -42,7 +38,7 @@ public class SenderSendInterceptor implements InstanceMethodsAroundInterceptor {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
|
||||
public Object afterMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments, final Class<?>[] argumentsTypes,
|
||||
Object ret) throws Throwable {
|
||||
Response response = (Response)ret;
|
||||
int statusCode = response.getStatus();
|
||||
|
|
@ -55,8 +51,9 @@ public class SenderSendInterceptor implements InstanceMethodsAroundInterceptor {
|
|||
return ret;
|
||||
}
|
||||
|
||||
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
|
||||
Class<?>[] argumentsTypes, Throwable t) {
|
||||
@Override
|
||||
public void handleMethodException(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
|
||||
final Class<?>[] argumentsTypes, final Throwable t) {
|
||||
ContextManager.activeSpan().errorOccurred().log(t);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,41 +12,60 @@ import net.bytebuddy.description.method.MethodDescription;
|
|||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
|
||||
/**
|
||||
* {@link NutzHttpInstrumentation} enhance the <code>doExecute</code> method,<code>handleResponse</code> method and
|
||||
* <code>handleResponse</code> method of <code>org.springframework.web.client.RestTemplate</code> by
|
||||
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestExecuteInterceptor</code>,
|
||||
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestResponseInterceptor</code> and
|
||||
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestRequestInterceptor</code>.
|
||||
*
|
||||
* <code>org.skywalking.apm.plugin.spring.resttemplate.sync.RestResponseInterceptor</code> set context to header for
|
||||
* propagate trace context after execute <code>createRequest</code>.
|
||||
* {@link NutzHttpInstrumentation} enhance the <code>send</code>
|
||||
* method,<code>Constructor</code> of <code>org.nutz.http.Sender</code> by
|
||||
* <code>org.skywalking.apm.plugin.nutz.http.sync.SenderConstructorInterceptor</code>, and
|
||||
* <code>org.skywalking.apm.plugin.nutz.http.sync.SenderSendInterceptor</code>
|
||||
* set context to header for propagate trace context around execute
|
||||
* <code>send</code>.
|
||||
*
|
||||
* @author wendal
|
||||
*/
|
||||
public class NutzHttpInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
|
||||
|
||||
private static final String ENHANCE_CLASS = "org.nutz.http.Sender";
|
||||
private static final String DO_EXECUTE_METHOD_NAME = "send";
|
||||
private static final String DO_EXECUTE_INTERCEPTOR = "org.skywalking.apm.plugin.nutz.http.sync.SenderSendInterceptor";
|
||||
private static final String DO_SEND_METHOD_NAME = "send";
|
||||
private static final String DO_SEND_INTERCEPTOR = "org.skywalking.apm.plugin.nutz.http.sync.SenderSendInterceptor";
|
||||
private static final String DO_CONSTRUCTOR_INTERCEPTOR = "org.skywalking.apm.plugin.nutz.http.sync.SenderConstructorInterceptor";
|
||||
|
||||
@Override
|
||||
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
|
||||
return new ConstructorInterceptPoint[0];
|
||||
return new ConstructorInterceptPoint[]{
|
||||
new ConstructorInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getConstructorMatcher() {
|
||||
return new ElementMatcher<MethodDescription>() {
|
||||
@Override
|
||||
public boolean matches(MethodDescription target) {
|
||||
return target.isConstructor() && target.getParameters().size() > 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConstructorInterceptor() {
|
||||
return DO_CONSTRUCTOR_INTERCEPTOR;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
|
||||
return new InstanceMethodsInterceptPoint[] {
|
||||
return new InstanceMethodsInterceptPoint[]{
|
||||
new InstanceMethodsInterceptPoint() {
|
||||
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named(DO_EXECUTE_METHOD_NAME);
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named(DO_SEND_METHOD_NAME);
|
||||
}
|
||||
|
||||
@Override public String getMethodsInterceptor() {
|
||||
return DO_EXECUTE_INTERCEPTOR;
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return DO_SEND_INTERCEPTOR;
|
||||
}
|
||||
|
||||
@Override public boolean isOverrideArgs() {
|
||||
|
||||
@Override
|
||||
public boolean isOverrideArgs() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue