Repository: wso2/product-microgateway
Issue: Websocket Analytics are not working 

### Description

Getting the following error log in the Enforcer.

choreo-connect-with-apim-enforcer-1  | [2023-03-08 06:18:02,970][][] ERROR - {org.wso2.am.analytics.publisher.reporter.cloud.ParallelQueueWorker} - Builder instance is not duly filled. Event building failed [severity:Default error_code:0]
choreo-connect-with-apim-enforcer-1  | org.wso2.am.analytics.publisher.exception.MetricReportingException: apiContext is missing in metric data. This metric event will not be processed further.
choreo-connect-with-apim-enforcer-1  | 	at org.wso2.am.analytics.publisher.reporter.cloud.DefaultResponseMetricEventBuilder.validate(DefaultResponseMetricEventBuilder.java:62) ~[org.wso2.choreo.connect.enforcer-1.2.0-beta2-SNAPSHOT.jar:1.2.0-beta2-SNAPSHOT]
choreo-connect-with-apim-enforcer-1  | 	at org.wso2.am.analytics.publisher.reporter.AbstractMetricEventBuilder.build(AbstractMetricEventBuilder.java:32) ~[org.wso2.choreo.connect.enforcer-1.2.0-beta2-SNAPSHOT.jar:1.2.0-beta2-SNAPSHOT]
choreo-connect-with-apim-enforcer-1  | 	at org.wso2.am.analytics.publisher.reporter.cloud.ParallelQueueWorker.run(ParallelQueueWorker.java:56) ~[org.wso2.choreo.connect.enforcer-1.2.0-beta2-SNAPSHOT.jar:1.2.0-beta2-SNAPSHOT]
choreo-connect-with-apim-enforcer-1  | 	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:?]
choreo-connect-with-apim-enforcer-1  | 	at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:?]
choreo-connect-with-apim-enforcer-1  | 	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[?:?]
choreo-connect-with-apim-enforcer-1  | 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[?:?]
choreo-connect-with-apim-enforcer-1  | 	at java.lang.Thread.run(Unknown Source) ~[?:?]

### Steps to Reproduce

Invoke a web socket API with Analytics.

### Version

1.2.0-beta

### Environment Details (with versions)

_No response_

### Relevant Log Output

_No response_

### Related Issues

_No response_

### Suggested Labels

_No response_

Changed files:
enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/MetricsUtils.java
enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/DefaultAnalyticsEventPublisher.java
enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/ChoreoAnalyticsForWSProvider.java

diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/ChoreoAnalyticsForWSProvider.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/ChoreoAnalyticsForWSProvider.java
index 47255d5fd..260a51d15 100644
--- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/ChoreoAnalyticsForWSProvider.java
+++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/ChoreoAnalyticsForWSProvider.java
@@ -26,6 +26,7 @@ import org.wso2.carbon.apimgt.common.analytics.exceptions.DataNotFoundException;
 import org.wso2.carbon.apimgt.common.analytics.publishers.dto.API;
 import org.wso2.carbon.apimgt.common.analytics.publishers.dto.Application;
 import org.wso2.carbon.apimgt.common.analytics.publishers.dto.Error;
+import org.wso2.carbon.apimgt.common.analytics.publishers.dto.ExtendedAPI;
 import org.wso2.carbon.apimgt.common.analytics.publishers.dto.Latencies;
 import org.wso2.carbon.apimgt.common.analytics.publishers.dto.MetaInfo;
 import org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation;
@@ -102,19 +103,25 @@ public class ChoreoAnalyticsForWSProvider implements AnalyticsDataProvider {
     @Override
     public API getApi() throws DataNotFoundException {
 
-        API api = new API();
+        ExtendedAPI api = new ExtendedAPI();
 
         String apiVersion = extAuthMetadata.get(MetadataConstants.API_VERSION_KEY);
         String apiName = extAuthMetadata.get(MetadataConstants.API_NAME_KEY);
         String apiId = extAuthMetadata.get(MetadataConstants.API_ID_KEY);
         String apiCreator = extAuthMetadata.get(MetadataConstants.API_CREATOR_KEY);
         String apiCreatorTenantDomain = extAuthMetadata.get(MetadataConstants.API_CREATOR_TENANT_DOMAIN_KEY);
+        String apiContext = extAuthMetadata.get(MetadataConstants.API_CONTEXT_KEY);
+        String org = extAuthMetadata.get(MetadataConstants.API_ORGANIZATION_ID);
+
         api.setApiType(APIConstants.ApiType.WEB_SOCKET);
         api.setApiId(apiId);
         api.setApiName(apiName);
         api.setApiVersion(apiVersion);
         api.setApiCreatorTenantDomain(apiCreatorTenantDomain);
         api.setApiCreator(apiCreator);
+        api.setOrganizationId(org);
+        api.setApiContext(apiContext);
+
         return api;
     }
 
diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/DefaultAnalyticsEventPublisher.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/DefaultAnalyticsEventPublisher.java
index 6f9d1e6e8..f805f62f3 100644
--- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/DefaultAnalyticsEventPublisher.java
+++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/analytics/DefaultAnalyticsEventPublisher.java
@@ -150,12 +150,12 @@ public class DefaultAnalyticsEventPublisher implements AnalyticsEventPublisher {
                 && logEntry.getResponse().getResponseCodeDetails()
                 .equals(AnalyticsConstants.EXT_AUTH_DENIED_RESPONSE_DETAIL)
                 // Token endpoint calls needs to be removed as well
-                || (AnalyticsConstants.TOKEN_ENDPOINT_PATH.equals(logEntry.getRequest().getOriginalPath()))
+                || (AnalyticsConstants.TOKEN_ENDPOINT_PATH.equals(logEntry.getCommonProperties().getRouteName()))
                 // Health endpoint calls are not published
-                || (AnalyticsConstants.HEALTH_ENDPOINT_PATH.equals(logEntry.getRequest().getOriginalPath()))
+                || (AnalyticsConstants.HEALTH_ENDPOINT_PATH.equals(logEntry.getCommonProperties().getRouteName()))
                 // already published websocket log entries should not be published to the analytics again.
                 // JWKS endpoint calls should not be published
-                || (AnalyticsConstants.JWKS_ENDPOINT_PATH.equals(logEntry.getRequest().getOriginalPath()))
+                || (AnalyticsConstants.JWKS_ENDPOINT_PATH.equals(logEntry.getCommonProperties().getRouteName()))
                 || alreadyPublishedWebsocketHttpLogEntry(logEntry);
     }
 
diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/MetricsUtils.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/MetricsUtils.java
index 0d04493f4..4ff5368eb 100644
--- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/MetricsUtils.java
+++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/MetricsUtils.java
@@ -60,9 +60,11 @@ public class MetricsUtils {
                     && logEntry.getResponse().getResponseCodeDetails()
                     .equals(AnalyticsConstants.EXT_AUTH_DENIED_RESPONSE_DETAIL)
                     // Token endpoint calls needs to be removed as well
-                    || (AnalyticsConstants.TOKEN_ENDPOINT_PATH.equals(logEntry.getRequest().getOriginalPath()))
+                    || (AnalyticsConstants.TOKEN_ENDPOINT_PATH.equals(logEntry.getCommonProperties().getRouteName()))
                     // Health endpoint calls are not published
-                    || (AnalyticsConstants.HEALTH_ENDPOINT_PATH.equals(logEntry.getRequest().getOriginalPath()))) {
+                    || (AnalyticsConstants.HEALTH_ENDPOINT_PATH.equals(logEntry.getCommonProperties().getRouteName()))
+                    // JWKS endpoint calls should not be published
+                    || (AnalyticsConstants.JWKS_ENDPOINT_PATH.equals(logEntry.getCommonProperties().getRouteName()))) {
                 LOGGER.debug("Metric is ignored as it is already published by the enforcer.");
                 publishMetrics(metricsExporter, latencies);
                 continue;