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 540d80081f..7cfccd57dd 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 @@ -17,6 +17,8 @@ */ package org.wso2.micro.integrator.observability.metric.handler; +import java.util.ArrayList; +import java.util.Collection; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -309,8 +311,23 @@ private void incrementInboundEndpointErrorCount(String name) { * @return String The api name */ private String getApiName(String contextPath, MessageContext synCtx) { + Collection apiList = synCtx.getEnvironment().getSynapseConfiguration().getAPIs(); + Collection withVersionsApiList = new ArrayList<>(); + Collection defaultApiList = new ArrayList<>(); + updateApiLists(apiList, withVersionsApiList, defaultApiList); + if (!withVersionsApiList.isEmpty()) { + String apiName = getResolvedApiName(contextPath, synCtx, withVersionsApiList); + if (apiName != null) { + return apiName; + } + } + return getResolvedApiName(contextPath, synCtx, defaultApiList); + } + + private static String getResolvedApiName(String contextPath, MessageContext synCtx, + Collection apiList) { String apiName = null; - for (API api : synCtx.getEnvironment().getSynapseConfiguration().getAPIs()) { + for (API api : apiList) { String apiContextPath = api.getContext(); if (StringUtils.isNotBlank(api.getVersionStrategy().getVersion())) { apiContextPath = apiContextPath + "/" + api.getVersionStrategy().getVersion(); @@ -318,15 +335,23 @@ private String getApiName(String contextPath, MessageContext synCtx) { if (RESTUtils.matchApiPath(contextPath, apiContextPath)) { apiName = api.getName(); synCtx.setProperty(RESTConstants.PROCESSED_API, api); - // Since the APIs are already sorted in descending order, we should stop at the first match with the context path. - // Continuing the loop could result in matching a path that only shares the same starting string. - // Ex: /test/ API context path will match with /test/1.0.0 break; } } return apiName; } + private void updateApiLists(Collection apiList, Collection withVersionsApiList, + Collection defaultApiList) { + for (API api : apiList) { + if (StringUtils.isNotBlank(api.getVersionStrategy().getVersion())) { + withVersionsApiList.add(api); + } else { + defaultApiList.add(api); + } + } + } + /** * Return the port the service was invoked. * diff --git a/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/ServiceBusInitializer.java b/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/ServiceBusInitializer.java index 890a4903c8..4426a4d75f 100644 --- a/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/ServiceBusInitializer.java +++ b/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/ServiceBusInitializer.java @@ -172,7 +172,10 @@ protected void activate(ComponentContext ctxt) { } SynapseEnvironment synapseEnvironment = contextInfo.getSynapseEnvironment(); List handlers = synapseEnvironment.getSynapseHandlers(); - if (System.getProperty(ServiceBusConstants.ENABLE_PROMETHEUS_API_PROPERTY) != null) { + String prometheusApiEnabled = System.getProperty(ServiceBusConstants.ENABLE_PROMETHEUS_API_PROPERTY); + if ("false".equals(prometheusApiEnabled)) { + handlers.remove(handlers.stream().filter(c -> c instanceof MetricHandler).findFirst().orElse(null)); + } else if ("true".equals(prometheusApiEnabled)) { if (!handlers.stream().anyMatch(c -> c instanceof MetricHandler)) { handlers.add(new MetricHandler()); }