Skip to content

Commit 912b60a

Browse files
Fix the HttpChannel trace termination in case of exceptions (opensearch-project#10325) (opensearch-project#10344)
(cherry picked from commit bddf0d3) Signed-off-by: Andriy Redko <andriy.redko@aiven.io> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 6170dd9 commit 912b60a

File tree

6 files changed

+19
-2
lines changed

6 files changed

+19
-2
lines changed

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/DefaultTracer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
*/
2626
@InternalApi
2727
class DefaultTracer implements Tracer {
28-
static final String THREAD_NAME = "th_name";
28+
/**
29+
* Current thread name.
30+
*/
31+
static final String THREAD_NAME = "thread.name";
2932

3033
private final TracingTelemetry tracingTelemetry;
3134
private final TracerContextStorage<String, Span> tracerContextStorage;

server/src/main/java/org/opensearch/http/AbstractHttpServerTransport.java

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ static int resolvePublishPort(Settings settings, List<TransportAddress> boundAdd
298298
}
299299

300300
public void onException(HttpChannel channel, Exception e) {
301+
channel.handleException(e);
301302
if (lifecycle.started() == false) {
302303
// just close and ignore - we are already stopped and just need to make sure we release all resources
303304
CloseableChannel.closeChannel(channel);

server/src/main/java/org/opensearch/http/HttpChannel.java

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
* @opensearch.internal
4444
*/
4545
public interface HttpChannel extends CloseableChannel {
46+
/**
47+
* Notify HTTP channel that exception happens and the response may not be sent (for example, timeout)
48+
* @param ex the exception being raised
49+
*/
50+
default void handleException(Exception ex) {}
4651

4752
/**
4853
* Sends an http response to the channel. The listener will be executed once the send process has been

server/src/main/java/org/opensearch/telemetry/tracing/channels/TraceableHttpChannel.java

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public static HttpChannel create(HttpChannel delegate, Span span, Tracer tracer)
5656
}
5757
}
5858

59+
@Override
60+
public void handleException(Exception ex) {
61+
span.addEvent("The HttpChannel was closed without sending the response");
62+
span.setError(ex);
63+
span.endSpan();
64+
}
65+
5966
@Override
6067
public void close() {
6168
delegate.close();

server/src/main/java/org/opensearch/telemetry/tracing/handler/TraceableTransportResponseHandler.java

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public void handleRejection(Exception exp) {
101101
try (SpanScope scope = tracer.withSpanInScope(span)) {
102102
delegate.handleRejection(exp);
103103
} finally {
104+
span.setError(exp);
104105
span.endSpan();
105106
}
106107
}

server/src/main/java/org/opensearch/transport/TransportResponseHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public interface TransportResponseHandler<T extends TransportResponse> extends W
5757
* It should be used to clear up the resources held by the {@link TransportResponseHandler}.
5858
* @param exp exception
5959
*/
60-
default void handleRejection(Exception exp) {};
60+
default void handleRejection(Exception exp) {}
6161

6262
default <Q extends TransportResponse> TransportResponseHandler<Q> wrap(Function<Q, T> converter, Writeable.Reader<Q> reader) {
6363
final TransportResponseHandler<T> self = this;

0 commit comments

Comments
 (0)