Repository: quarkusio/quarkus
Issue: Quarkus doesn’t send X-Ray traces to AWS for sub-segments in Native build while using RESTEasy “controller” based approach

### Describe the bug

I an using "Controller" based approach using RESTEasy. I have added Quarkus AWS Xray dependency in my POM.xml..

And then we are instrumenting calls to AWS Services using below dependency:
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
    <version>2.10.0</version>
</dependency>


Also, we create manual sub-segment for external API calls..

However, Quarkus send sub-segment traces to XRay in the JVM build..
But in Native build, it does not send any sub-segments... also, there is no error thrown. In cloudwatch logs, it says "TRACE_ID missing.

It works for native build in AWS Lambda "Handler" based approach but not in "Controller" based approach in RESTEasy.
Attaching the screenshots of JVM and Native Build below:








### Expected behavior

It should create and show sub-segments in the graph also.

### Actual behavior

It does not show the sub-segments in Native build.

### How to Reproduce?

[https://github.com/amitkumar7566/xray-native-image-test](url)

### Output of `uname -a` or `ver`

_No response_

### Output of `java -version`

java 11

### GraalVM version (if different from Java)

21.3-java11

### Quarkus version or git rev

2.6.1.Final

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

maven

### Additional information

_No response_

Changed files:
extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AbstractLambdaPollLoop.java
extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/TraceId.java
extensions/amazon-lambda-xray/runtime/src/main/java/io/quarkus/amazon/lambda/xray/graal/LambdaSegmentContextSubstitution.java

diff --git a/extensions/amazon-lambda-xray/runtime/src/main/java/io/quarkus/amazon/lambda/xray/graal/LambdaSegmentContextSubstitution.java b/extensions/amazon-lambda-xray/runtime/src/main/java/io/quarkus/amazon/lambda/xray/graal/LambdaSegmentContextSubstitution.java
deleted file mode 100644
index bd68ad06ee1..00000000000
--- a/extensions/amazon-lambda-xray/runtime/src/main/java/io/quarkus/amazon/lambda/xray/graal/LambdaSegmentContextSubstitution.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package io.quarkus.amazon.lambda.xray.graal;
-
-import com.amazonaws.xray.contexts.LambdaSegmentContext;
-import com.amazonaws.xray.entities.TraceHeader;
-import com.oracle.svm.core.annotate.Substitute;
-import com.oracle.svm.core.annotate.TargetClass;
-
-import io.quarkus.amazon.lambda.runtime.TraceId;
-
-@TargetClass(LambdaSegmentContext.class)
-public final class LambdaSegmentContextSubstitution {
-
-    @Substitute
-    private static TraceHeader getTraceHeaderFromEnvironment() {
-        return TraceHeader.fromString(TraceId.getTraceId());
-    }
-
-}
diff --git a/extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AbstractLambdaPollLoop.java b/extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AbstractLambdaPollLoop.java
index 4bb45bba97e..03d2883bb28 100644
--- a/extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AbstractLambdaPollLoop.java
+++ b/extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AbstractLambdaPollLoop.java
@@ -25,6 +25,7 @@ public abstract class AbstractLambdaPollLoop {
     private final ObjectReader cognitoIdReader;
     private final ObjectReader clientCtxReader;
     private final LaunchMode launchMode;
+    private static final String LAMBDA_TRACE_HEADER_PROP = "com.amazonaws.xray.traceHeader";
 
     public AbstractLambdaPollLoop(ObjectMapper objectMapper, ObjectReader cognitoIdReader, ObjectReader clientCtxReader,
             LaunchMode launchMode) {
@@ -102,7 +103,9 @@ public void run() {
                                     }
                                 }
                                 String traceId = requestConnection.getHeaderField(AmazonLambdaApi.LAMBDA_TRACE_HEADER_KEY);
-                                TraceId.setTraceId(traceId);
+                                if (traceId != null) {
+                                    System.setProperty(LAMBDA_TRACE_HEADER_PROP, traceId);
+                                }
                                 URL url = AmazonLambdaApi.invocationResponse(baseUrl, requestId);
                                 if (isStream()) {
                                     HttpURLConnection responseConnection = responseStream(url);
diff --git a/extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/TraceId.java b/extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/TraceId.java
deleted file mode 100644
index 60fc529c20b..00000000000
--- a/extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/TraceId.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package io.quarkus.amazon.lambda.runtime;
-
-public class TraceId {
-    private static ThreadLocal<String> traceHeader = new ThreadLocal<>();
-
-    public static void setTraceId(String id) {
-        traceHeader.set(id);
-    }
-
-    public static String getTraceId() {
-        return traceHeader.get();
-    }
-
-    public static void clearTraceId() {
-        traceHeader.set(null);
-    }
-}