Repository: wso2/product-microgateway
Issue: Envoy access log entry should be tracked in enforcer as well

### Description:
In envoy we can print the random UUID for each request in access logs using the parameter `%REQ(X-REQUEST-ID)%` .
This is useful to track the request. We should use the same tracking ID in the enforcer side access logs as well.
 
### Steps to reproduce:
Currently enforcer prints id attribute of the http request(https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/auth/v3/attribute_context.proto#service-auth-v3-attributecontext-httprequest), which is different from the UUID printed by X-REQUEST-ID in envoy access log.

### Affected Product Version:
0.9.0

### Environment details (with versions):
- OS:
- Client:
- Env (Docker/K8s):

---
### Optional Fields
#### Related Issues:
<!-- Any related issues from this/other repositories-->

#### Suggested Labels:
<!--Only to be used by non-members-->

#### Suggested Assignees:
<!--Only to be used by non-members-->


Changed files:
enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java
enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/interceptors/AccessLogInterceptor.java
enforcer/src/main/java/org/wso2/choreo/connect/enforcer/constants/HttpConstants.java

diff --git a/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/constants/HttpConstants.java b/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/constants/HttpConstants.java
index 300f10bcd..898312a2b 100644
--- a/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/constants/HttpConstants.java
+++ b/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/constants/HttpConstants.java
@@ -25,4 +25,5 @@ public class HttpConstants {
     public static final int NO_CONTENT_STATUS_CODE = 204;
     public static final String OPTIONS = "OPTIONS";
     public static final String ALLOW_HEADER = "allow";
+    public static final String X_REQUEST_ID_HEADER = "x-request-id";
 }
diff --git a/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java b/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java
index c70fa9afa..6c2e17ae4 100644
--- a/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java
+++ b/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java
@@ -48,7 +48,9 @@ public class ExtAuthService extends AuthorizationGrpc.AuthorizationImplBase {
 
     @Override
     public void check(CheckRequest request, StreamObserver<CheckResponse> responseObserver) {
-        ThreadContext.put(APIConstants.LOG_TRACE_ID, request.getAttributes().getRequest().getHttp().getId());
+        ThreadContext.put(APIConstants.LOG_TRACE_ID, request.getAttributes().getRequest().getHttp()
+                .getHeadersOrDefault(HttpConstants.X_REQUEST_ID_HEADER,
+                        request.getAttributes().getRequest().getHttp().getId()));
         ResponseObject responseObject = requestHandler.process(request);
         CheckResponse response = buildResponse(request, responseObject);
         responseObserver.onNext(response);
diff --git a/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/interceptors/AccessLogInterceptor.java b/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/interceptors/AccessLogInterceptor.java
index cdbbf9267..dcdb43141 100644
--- a/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/interceptors/AccessLogInterceptor.java
+++ b/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/interceptors/AccessLogInterceptor.java
@@ -30,6 +30,7 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.wso2.choreo.connect.discovery.service.websocket.WebSocketFrameRequest;
 import org.wso2.choreo.connect.discovery.service.websocket.WebSocketFrameResponse;
+import org.wso2.choreo.connect.enforcer.constants.HttpConstants;
 import org.wso2.choreo.connect.enforcer.websocket.MetadataConstants;
 
 /**
@@ -50,7 +51,12 @@ public class AccessLogInterceptor implements ServerInterceptor {
                     if (message instanceof CheckRequest) {
                         CheckRequest checkRequest = (CheckRequest) message;
                         enforcerServerCall.setStartTime(System.currentTimeMillis());
-                        enforcerServerCall.setTraceId(checkRequest.getAttributes().getRequest().getHttp().getId());
+                        String requestId = checkRequest.getAttributes().getRequest().getHttp().getId();
+                        // x-request-id equals to envoy access log entries "%REQ(X-REQUEST-ID)%". This allows
+                        // to correlate the same request in Router and the Enforcer. If this header is not coming then
+                        // we set the http request Id property as the default value.
+                        enforcerServerCall.setTraceId(checkRequest.getAttributes().getRequest().getHttp()
+                                .getHeadersOrDefault(HttpConstants.X_REQUEST_ID_HEADER, requestId));
                         super.onMessage(message);
                     } else if (message instanceof WebSocketFrameRequest) {
                         WebSocketFrameRequest webSocketFrameRequest = (WebSocketFrameRequest) message;