From f796e4c5e35d92a5a4029fa074ad2e2147e551f9 Mon Sep 17 00:00:00 2001 From: Steven Schlansker Date: Tue, 24 Sep 2024 17:12:35 -0700 Subject: [PATCH] jetty-httpclient-12: send method must pass along implemented response listeners The super HttpRequest.send call says: > The listener passed to this method may implement not only Response.CompleteListener > but also other response listener interfaces, and all the events implemented will be notified. The current implementation passes through a lambda that implements CompleteListener only, and does not delegate-in-scope to any other callback methods you might provide --- .../httpclient/v12_0/TracingHttpRequest.java | 85 ++++++++++++++++++- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/instrumentation/jetty-httpclient/jetty-httpclient-12.0/library/src/main/java/io/opentelemetry/instrumentation/jetty/httpclient/v12_0/TracingHttpRequest.java b/instrumentation/jetty-httpclient/jetty-httpclient-12.0/library/src/main/java/io/opentelemetry/instrumentation/jetty/httpclient/v12_0/TracingHttpRequest.java index 9bdc687c250d..18846fc140a1 100644 --- a/instrumentation/jetty-httpclient/jetty-httpclient-12.0/library/src/main/java/io/opentelemetry/instrumentation/jetty/httpclient/v12_0/TracingHttpRequest.java +++ b/instrumentation/jetty-httpclient/jetty-httpclient-12.0/library/src/main/java/io/opentelemetry/instrumentation/jetty/httpclient/v12_0/TracingHttpRequest.java @@ -14,8 +14,12 @@ import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.Request; import org.eclipse.jetty.client.Response; +import org.eclipse.jetty.client.Result; import org.eclipse.jetty.client.transport.HttpConversation; import org.eclipse.jetty.client.transport.HttpRequest; +import org.eclipse.jetty.http.HttpField; +import org.eclipse.jetty.io.Content.Chunk; +import org.eclipse.jetty.io.Content.Source; class TracingHttpRequest extends HttpRequest { @@ -37,9 +41,84 @@ public void send(Response.CompleteListener listener) { // start span and attach listeners. JettyClientTracingListener.handleRequest(parentContext, this, instrumenter); super.send( - result -> { - try (Scope scope = openScope()) { - listener.onComplete(result); + new Response.Listener() { + @Override + public void onBegin(Response response) { + if (listener instanceof Response.BeginListener l) { + try (Scope scope = openScope()) { + l.onBegin(response); + } + } + } + + @Override + public void onComplete(Result result) { + try (Scope scope = openScope()) { + listener.onComplete(result); + } + } + + @Override + public void onContent(Response response, ByteBuffer content) { + if (listener instanceof Response.ContentListener l) { + try (Scope scope = openScope()) { + l.onContent(response, content); + } + } + } + + @Override + public void onContent(Response response, Chunk chunk, Runnable demander) { + if (listener instanceof Response.AsyncContentListener l) { + try (Scope scope = openScope()) { + l.onContent(response, chunk, demander); + } + } + } + + @Override + public void onContentSource(Response response, Source contentSource) { + if (listener instanceof Response.ContentSourceListener l) { + try (Scope scope = openScope()) { + l.onContentSource(response, contentSource); + } + } + } + + @Override + public void onFailure(Response response, Throwable failure) { + if (listener instanceof Response.FailureListener l) { + try (Scope scope = openScope()) { + l.onFailure(response, failure); + } + } + } + + @Override + public void onHeaders(Response response) { + if (listener instanceof Response.HeadersListener l) { + try (Scope scope = openScope()) { + l.onHeaders(response); + } + } + } + + @Override + public boolean onHeader(Response response, HttpField field) { + if (listener instanceof Response.HeaderListener l) { + try (Scope scope = openScope()) { + return l.onHeader(response, field); + } + } + } + + @Override + public void onSuccess(Response response) { + if (listener instanceof Response.SuccessListener l) { + try (Scope scope = openScope()) { + l.onSuccess(response); + } + } } }); }