diff --git a/java-client/src/main/java/co/elastic/clients/transport/ElasticsearchTransportBase.java b/java-client/src/main/java/co/elastic/clients/transport/ElasticsearchTransportBase.java index 913c10d479..f51b488f47 100644 --- a/java-client/src/main/java/co/elastic/clients/transport/ElasticsearchTransportBase.java +++ b/java-client/src/main/java/co/elastic/clients/transport/ElasticsearchTransportBase.java @@ -129,6 +129,7 @@ public final ResponseT performRequest( @Nullable TransportOptions options ) throws IOException { try (Instrumentation.Context ctx = instrumentation.newContext(request, endpoint)) { + // HTTP client span is parented to the logical Elasticsearch request span try (Instrumentation.ThreadScope ts = ctx.makeCurrent()) { TransportOptions opts = options == null ? transportOptions : options; @@ -158,10 +159,18 @@ public final CompletableFuture performR Instrumentation.Context ctx = instrumentation.newContext(request, endpoint); TransportOptions opts = options == null ? transportOptions : options; - TransportHttpClient.Request clientReq; - try (Instrumentation.ThreadScope ss = ctx.makeCurrent()) { - clientReq = prepareTransportRequest(request, endpoint); + // Propagate required property checks to the thread that will decode the response + boolean disableRequiredChecks = ApiTypeHelper.requiredPropertiesCheckDisabled(); + + CompletableFuture clientFuture; + // HTTP client span is parented to the logical Elasticsearch request span + try (Instrumentation.ThreadScope ts = ctx.makeCurrent()) { + TransportHttpClient.Request clientReq = prepareTransportRequest(request, endpoint); ctx.beforeSendingHttpRequest(clientReq, options); + + clientFuture = httpClient.performRequestAsync( + endpoint.id(), null, clientReq, opts + ); } catch (Exception e) { // Terminate early ctx.recordException(e); @@ -171,14 +180,8 @@ public final CompletableFuture performR return future; } - // Propagate required property checks to the thread that will decode the response - boolean disableRequiredChecks = ApiTypeHelper.requiredPropertiesCheckDisabled(); - - CompletableFuture clientFuture = httpClient.performRequestAsync( - endpoint.id(), null, clientReq, opts - ); - - // Cancelling the result will cancel the upstream future created by the http client, allowing to stop in-flight requests + // Cancelling the result will cancel the upstream future created by the http client, allowing to + // stop in-flight requests CompletableFuture future = new CompletableFuture() { @Override public boolean cancel(boolean mayInterruptIfRunning) {