未验证 提交 8820287f 编写于 作者: A ArjenDavid-sjtu 提交者: GitHub

Improve okhttp plugin performance by optimizing Class.getDeclaredField(). (#7364)

Simple polish only.
上级 a7abb1f5
......@@ -39,6 +39,7 @@ Release Notes.
* Remove plugins for ShardingSphere legacy version.
* Update agent plugin for ElasticJob GA version
* Remove the logic of generating instance name in `KafkaServiceManagementServiceClient` class.
* Improve `okhttp` plugin performance by optimizing Class.getDeclaredField().
#### OAP-Backend
......
......@@ -44,6 +44,19 @@ import java.lang.reflect.Method;
* called.
*/
public class AsyncCallInterceptor implements InstanceConstructorInterceptor, InstanceMethodsAroundInterceptor {
private static Field FIELD_HEADERS_OF_REQUEST;
static {
try {
final Field field = Request.class.getDeclaredField("headers");
field.setAccessible(true);
FIELD_HEADERS_OF_REQUEST = field;
} catch (Exception ignore) {
FIELD_HEADERS_OF_REQUEST = null;
}
}
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
/**
......@@ -73,15 +86,15 @@ public class AsyncCallInterceptor implements InstanceConstructorInterceptor, Ins
Tags.URL.set(span, requestUrl.uri().toString());
SpanLayer.asHttp(span);
Field headersField = Request.class.getDeclaredField("headers");
headersField.setAccessible(true);
Headers.Builder headerBuilder = request.headers().newBuilder();
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
headerBuilder.set(next.getHeadKey(), next.getHeadValue());
if (FIELD_HEADERS_OF_REQUEST != null) {
Headers.Builder headerBuilder = request.headers().newBuilder();
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
headerBuilder.set(next.getHeadKey(), next.getHeadValue());
}
FIELD_HEADERS_OF_REQUEST.set(request, headerBuilder.build());
}
headersField.set(request, headerBuilder.build());
}
......
......@@ -42,6 +42,18 @@ import java.lang.reflect.Method;
*/
public class RealCallInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
private static Field FIELD_HEADERS_OF_REQUEST;
static {
try {
final Field field = Request.class.getDeclaredField("headers");
field.setAccessible(true);
FIELD_HEADERS_OF_REQUEST = field;
} catch (Exception ignore) {
FIELD_HEADERS_OF_REQUEST = null;
}
}
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
objInst.setSkyWalkingDynamicField(allArguments[1]);
......@@ -69,15 +81,15 @@ public class RealCallInterceptor implements InstanceMethodsAroundInterceptor, In
Tags.URL.set(span, requestUrl.uri().toString());
SpanLayer.asHttp(span);
Field headersField = Request.class.getDeclaredField("headers");
headersField.setAccessible(true);
Headers.Builder headerBuilder = request.headers().newBuilder();
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
headerBuilder.set(next.getHeadKey(), next.getHeadValue());
if (FIELD_HEADERS_OF_REQUEST != null) {
Headers.Builder headerBuilder = request.headers().newBuilder();
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
headerBuilder.set(next.getHeadKey(), next.getHeadValue());
}
FIELD_HEADERS_OF_REQUEST.set(request, headerBuilder.build());
}
headersField.set(request, headerBuilder.build());
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册