Skip to content

Commit bf2daf3

Browse files
authored
Fix Jetty client cannot receive the HTTP response body(#13500) (#775)
1 parent 762d31f commit bf2daf3

File tree

15 files changed

+244
-38
lines changed

15 files changed

+244
-38
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Release Notes.
1616
* Update Maven to 3.6.3 in mvnw.
1717
* Fix OOM due to too many span logs.
1818
* Fix ClassLoader cache OOM issue with WeakHashMap.
19+
* Fix Jetty client cannot receive the HTTP response body.
1920

2021
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/242?closed=1)
2122

apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-client-9.0-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v90/client/AsyncHttpRequestSendInterceptor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5858

5959
span.prepareForAsync();
6060
request.attribute(Constants.SW_JETTY_EXIT_SPAN_KEY, span);
61-
Response.CompleteListener callback = (Response.CompleteListener) allArguments[0];
62-
allArguments[0] = new CompleteListenerWrapper(callback, ContextManager.capture());
61+
if (allArguments[0] instanceof Response.Listener) {
62+
Response.Listener listener = (Response.Listener) allArguments[0];
63+
allArguments[0] = new ResponseListenerWrapper(listener, ContextManager.capture());
64+
} else {
65+
Response.CompleteListener listener = (Response.CompleteListener) allArguments[0];
66+
allArguments[0] = new CompleteListenerWrapper(listener, ContextManager.capture());
67+
}
6368
}
6469

6570
@Override

apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-client-9.0-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v90/client/CompleteListenerWrapper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import org.eclipse.jetty.client.api.Result;
2828

2929
public class CompleteListenerWrapper implements Response.CompleteListener {
30-
private Response.CompleteListener callback;
30+
private Response.CompleteListener listener;
3131
private ContextSnapshot context;
3232

33-
public CompleteListenerWrapper(Response.CompleteListener callback, ContextSnapshot context) {
34-
this.callback = callback;
33+
public CompleteListenerWrapper(Response.CompleteListener listener, ContextSnapshot context) {
34+
this.listener = listener;
3535
this.context = context;
3636
}
3737

@@ -43,9 +43,9 @@ public void onComplete(Result result) {
4343
if (context != null) {
4444
ContextManager.continued(context);
4545
}
46-
if (callback != null) {
47-
callback.onComplete(result);
46+
if (listener != null) {
47+
listener.onComplete(result);
4848
}
4949
ContextManager.stopSpan();
5050
}
51-
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.jetty.v90.client;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
22+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
23+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
24+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
25+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
26+
import org.eclipse.jetty.client.api.Response;
27+
import org.eclipse.jetty.client.api.Result;
28+
import org.eclipse.jetty.http.HttpField;
29+
import java.nio.ByteBuffer;
30+
31+
public class ResponseListenerWrapper implements Response.Listener {
32+
33+
private final Response.Listener listener;
34+
35+
private final ContextSnapshot context;
36+
37+
public ResponseListenerWrapper(Response.Listener listener, ContextSnapshot context) {
38+
this.listener = listener;
39+
this.context = context;
40+
}
41+
42+
@Override
43+
public void onComplete(Result result) {
44+
AbstractSpan span = ContextManager.createLocalSpan(Constants.PLUGIN_NAME + "/CompleteListener/onComplete");
45+
span.setComponent(ComponentsDefine.JETTY_CLIENT);
46+
SpanLayer.asHttp(span);
47+
if (context != null) {
48+
ContextManager.continued(context);
49+
}
50+
if (listener != null) {
51+
listener.onComplete(result);
52+
}
53+
ContextManager.stopSpan();
54+
}
55+
56+
@Override
57+
public void onHeaders(Response response) {
58+
listener.onHeaders(response);
59+
}
60+
61+
@Override
62+
public void onContent(Response response, ByteBuffer content) {
63+
listener.onContent(response, content);
64+
}
65+
66+
@Override
67+
public void onBegin(Response response) {
68+
listener.onBegin(response);
69+
}
70+
71+
@Override
72+
public boolean onHeader(Response response, HttpField field) {
73+
return listener.onHeader(response, field);
74+
}
75+
76+
@Override
77+
public void onSuccess(Response response) {
78+
listener.onSuccess(response);
79+
}
80+
81+
@Override
82+
public void onFailure(Response response, Throwable failure) {
83+
listener.onFailure(response, failure);
84+
}
85+
}

apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-client-9.0-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v90/client/define/HttpRequestInstrumentation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class HttpRequestInstrumentation extends ClassInstanceMethodsEnhancePlugi
4141
private static final String ENHANCE_CLASS = "org.eclipse.jetty.client.HttpRequest";
4242
private static final String ENHANCE_CLASS_NAME = "send";
4343
public static final String SYNC_SEND_INTERCEPTOR =
44-
"org.apache.skywalking.apm.plugin.jetty.v90.client.SyncHttpRequestSendV90Interceptor";
44+
"org.apache.skywalking.apm.plugin.jetty.v90.client.SyncHttpRequestSendInterceptor";
4545

4646
public static final String ASYNC_SEND_INTERCEPTOR =
4747
"org.apache.skywalking.apm.plugin.jetty.v90.client.AsyncHttpRequestSendInterceptor";
@@ -85,7 +85,7 @@ public String getMethodsInterceptor() {
8585

8686
@Override
8787
public boolean isOverrideArgs() {
88-
return false;
88+
return true;
8989
}
9090
}
9191
};

apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-client-9.0-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v90/client/define/ResponseNotifierInstrumentation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
*/
4242
public class ResponseNotifierInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
4343

44-
private static final String ENHANCE_CLASS = "org.eclipse.jetty.client.tar";
44+
private static final String ENHANCE_CLASS = "org.eclipse.jetty.client.ResponseNotifier";
4545
private static final String ENHANCE_CLASS_NAME = "notifyComplete";
4646
public static final String SYNC_SEND_INTERCEPTOR =
47-
"org.apache.skywalking.apm.plugin.jetty.v9.client.ResponseNotifierInterceptor";
47+
"org.apache.skywalking.apm.plugin.jetty.v90.client.ResponseNotifierInterceptor";
4848

4949
@Override
5050
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {

apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-client-9.x-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<url>http://maven.apache.org</url>
3232

3333
<properties>
34-
<jetty-client.version>9.1.0.v20131115</jetty-client.version>
34+
<jetty-client.version>9.2.23.v20171218</jetty-client.version>
3535
</properties>
3636

3737
<dependencies>

apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-client-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/client/AsyncHttpRequestSendInterceptor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
5757

5858
span.prepareForAsync();
5959
request.attribute(Constants.SW_JETTY_EXIT_SPAN_KEY, span);
60-
Response.CompleteListener callback = (Response.CompleteListener) allArguments[0];
61-
allArguments[0] = new CompleteListenerWrapper(callback, ContextManager.capture());
60+
if (allArguments[0] instanceof Response.Listener) {
61+
Response.Listener listener = (Response.Listener) allArguments[0];
62+
allArguments[0] = new ResponseListenerWrapper(listener, ContextManager.capture());
63+
} else {
64+
Response.CompleteListener listener = (Response.CompleteListener) allArguments[0];
65+
allArguments[0] = new CompleteListenerWrapper(listener, ContextManager.capture());
66+
}
6267
}
6368

6469
@Override

apm-sniffer/apm-sdk-plugin/jetty-plugin/jetty-client-9.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/client/CompleteListenerWrapper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import org.eclipse.jetty.client.api.Result;
2828

2929
public class CompleteListenerWrapper implements Response.CompleteListener {
30-
private Response.CompleteListener callback;
30+
private Response.CompleteListener listener;
3131
private ContextSnapshot context;
3232

33-
public CompleteListenerWrapper(Response.CompleteListener callback, ContextSnapshot context) {
34-
this.callback = callback;
33+
public CompleteListenerWrapper(Response.CompleteListener listener, ContextSnapshot context) {
34+
this.listener = listener;
3535
this.context = context;
3636
}
3737

@@ -43,9 +43,9 @@ public void onComplete(Result result) {
4343
if (context != null) {
4444
ContextManager.continued(context);
4545
}
46-
if (callback != null) {
47-
callback.onComplete(result);
46+
if (listener != null) {
47+
listener.onComplete(result);
4848
}
4949
ContextManager.stopSpan();
5050
}
51-
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.jetty.v9.client;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
22+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
23+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
24+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
25+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
26+
import org.eclipse.jetty.client.api.Response;
27+
import org.eclipse.jetty.client.api.Result;
28+
import org.eclipse.jetty.http.HttpField;
29+
import org.eclipse.jetty.util.Callback;
30+
31+
import java.nio.ByteBuffer;
32+
33+
public class ResponseListenerWrapper implements Response.Listener {
34+
35+
private final Response.Listener listener;
36+
37+
private final ContextSnapshot context;
38+
39+
public ResponseListenerWrapper(Response.Listener listener, ContextSnapshot context) {
40+
this.listener = listener;
41+
this.context = context;
42+
}
43+
44+
@Override
45+
public void onComplete(Result result) {
46+
AbstractSpan span = ContextManager.createLocalSpan(Constants.PLUGIN_NAME + "/CompleteListener/onComplete");
47+
span.setComponent(ComponentsDefine.JETTY_CLIENT);
48+
SpanLayer.asHttp(span);
49+
if (context != null) {
50+
ContextManager.continued(context);
51+
}
52+
if (listener != null) {
53+
listener.onComplete(result);
54+
}
55+
ContextManager.stopSpan();
56+
}
57+
58+
@Override
59+
public void onHeaders(Response response) {
60+
listener.onHeaders(response);
61+
}
62+
63+
@Override
64+
public void onContent(Response response, ByteBuffer content, Callback callback) {
65+
listener.onContent(response, content, callback);
66+
}
67+
68+
@Override
69+
public void onContent(Response response, ByteBuffer content) {
70+
listener.onContent(response, content);
71+
}
72+
73+
@Override
74+
public void onBegin(Response response) {
75+
listener.onBegin(response);
76+
}
77+
78+
@Override
79+
public boolean onHeader(Response response, HttpField field) {
80+
return listener.onHeader(response, field);
81+
}
82+
83+
@Override
84+
public void onSuccess(Response response) {
85+
listener.onSuccess(response);
86+
}
87+
88+
@Override
89+
public void onFailure(Response response, Throwable failure) {
90+
listener.onFailure(response, failure);
91+
}
92+
}

0 commit comments

Comments
 (0)