Skip to content

Commit 694efb1

Browse files
committed
Fix Jetty client cannot receive the HTTP response body
1 parent 762d31f commit 694efb1

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
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.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jetty/v9/client/AsyncHttpRequestSendInterceptor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ 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+
61+
if (allArguments[0] instanceof Response.Listener) {
62+
Response.Listener callback = (Response.Listener) allArguments[0];
63+
allArguments[0] = new ResponseListenerWrapper(callback, ContextManager.capture());
64+
} else {
65+
Response.CompleteListener callback = (Response.CompleteListener) allArguments[0];
66+
allArguments[0] = new CompleteListenerWrapper(callback, ContextManager.capture());
67+
}
6268
}
6369

6470
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
30+
import java.nio.ByteBuffer;
31+
32+
public class ResponseListenerWrapper implements Response.Listener {
33+
private final Response.Listener callback;
34+
private final ContextSnapshot context;
35+
36+
public ResponseListenerWrapper(Response.Listener callback, ContextSnapshot context) {
37+
this.callback = callback;
38+
this.context = context;
39+
}
40+
41+
@Override
42+
public void onComplete(Result result) {
43+
AbstractSpan span = ContextManager.createLocalSpan(Constants.PLUGIN_NAME + "/CompleteListener/onComplete");
44+
span.setComponent(ComponentsDefine.JETTY_CLIENT);
45+
SpanLayer.asHttp(span);
46+
if (context != null) {
47+
ContextManager.continued(context);
48+
}
49+
if (callback != null) {
50+
callback.onComplete(result);
51+
}
52+
ContextManager.stopSpan();
53+
}
54+
55+
@Override
56+
public void onHeaders(Response response) {
57+
callback.onHeaders(response);
58+
}
59+
60+
@Override
61+
public void onContent(Response response, ByteBuffer content) {
62+
callback.onContent(response, content);
63+
}
64+
65+
@Override
66+
public void onBegin(Response response) {
67+
callback.onBegin(response);
68+
}
69+
70+
@Override
71+
public boolean onHeader(Response response, HttpField field) {
72+
return callback.onHeader(response, field);
73+
}
74+
75+
@Override
76+
public void onSuccess(Response response) {
77+
callback.onSuccess(response);
78+
}
79+
80+
@Override
81+
public void onFailure(Response response, Throwable failure) {
82+
callback.onFailure(response, failure);
83+
}
84+
}

0 commit comments

Comments
 (0)