Skip to content

Commit

Permalink
Merge pull request #3767 from Bhashinee/master
Browse files Browse the repository at this point in the history
Fix the issues with dispatching and the -DenablePrometheusApi environment variable not being applied when observability is enabled
  • Loading branch information
Bhashinee authored Nov 11, 2024
2 parents 1a481af + 3ea3869 commit dec1b05
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -309,24 +311,47 @@ private void incrementInboundEndpointErrorCount(String name) {
* @return String The api name
*/
private String getApiName(String contextPath, MessageContext synCtx) {
Collection<API> apiList = synCtx.getEnvironment().getSynapseConfiguration().getAPIs();
Collection<API> withVersionsApiList = new ArrayList<>();
Collection<API> 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<API> 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();
}
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<API> apiList, Collection<API> withVersionsApiList,
Collection<API> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit dec1b05

Please sign in to comment.