Repository: wso2/micro-integrator
Issue: Metric Handler causes OOM issues.

**Description:**
Under heavy load (in cases where APIs with query parameters), the metrics publishing leads to OOM issues. Suspected cause for this having the invocation URL in labels, which causes more and more metrics to be created each time the path param changes. This leads to issues when formatting the metrics in https://github.com/wso2/micro-integrator/blob/9f7ceb0cc800e7df7d0ba6102a6ab26e2c9fc52a/components/mediation/data-publishers/org.wso2.micro.integrator.observability/src/main/java/org/wso2/micro/integrator/observability/metric/publisher/MetricFormatter.java#L57

This OOM issue needs to addresses either by ignoring path params of the invocation URL or removing the invocation URL.
We need to limit the number of metrics created and make sure this doesn't fail under heavy load.


Changed files:
components/mediation/data-publishers/org.wso2.micro.integrator.observability/src/main/java/org/wso2/micro/integrator/observability/metric/handler/MetricHandler.java

diff --git a/components/mediation/data-publishers/org.wso2.micro.integrator.observability/src/main/java/org/wso2/micro/integrator/observability/metric/handler/MetricHandler.java b/components/mediation/data-publishers/org.wso2.micro.integrator.observability/src/main/java/org/wso2/micro/integrator/observability/metric/handler/MetricHandler.java
index d5853ef54..dcd3f3479 100644
--- a/components/mediation/data-publishers/org.wso2.micro.integrator.observability/src/main/java/org/wso2/micro/integrator/observability/metric/handler/MetricHandler.java
+++ b/components/mediation/data-publishers/org.wso2.micro.integrator.observability/src/main/java/org/wso2/micro/integrator/observability/metric/handler/MetricHandler.java
@@ -133,12 +133,19 @@ public class MetricHandler extends AbstractExtendedSynapseHandler {
 
             if ((serviceInvokePort != internalHttpApiPort) && (null !=
                     axis2MessageContext.getProperty(MetricConstants.SERVICE_PREFIX))) {
-                String context = axis2MessageContext.getProperty(MetricConstants.TRANSPORT_IN_URL).
+                String url = axis2MessageContext.getProperty(MetricConstants.TRANSPORT_IN_URL).
                         toString();
-                String apiInvocationUrl = axis2MessageContext.getProperty(MetricConstants.SERVICE_PREFIX).
-                        toString() + context.replaceFirst(DELIMITER, EMPTY);
-                String apiName = getApiName(context, synCtx);
+                String apiName = getApiName(url, synCtx);
                 if (apiName != null) {
+                    String context = "";
+                    if (synCtx.getConfiguration() != null) {
+                        API api = synCtx.getConfiguration().getAPI(apiName);
+                        if (api != null) {
+                            context = api.getContext();
+                        }
+                    }
+                    String apiInvocationUrl = axis2MessageContext.getProperty(MetricConstants.SERVICE_PREFIX).
+                            toString() + context.replaceFirst(DELIMITER, EMPTY);
                     incrementAPICount(apiName, apiInvocationUrl);
                     startTimers(synCtx, apiName, SynapseConstants.FAIL_SAFE_MODE_API, apiInvocationUrl);
                 }