Skip to content

Commit

Permalink
Update AnalyticsDataPublisher to Handle Multiple Reporter Types
Browse files Browse the repository at this point in the history
  • Loading branch information
Piumal Rathnayake authored and Piumal Rathnayake committed Sep 22, 2024
1 parent a1bcf9d commit 994bb31
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ public class Constants {
public static final String USERNAME_MASK_VALUE = "*****";

public static final String AUTH_API_URL = "auth.api.url";

public static final String CHOREO_REPORTER_NAME = "choreo";
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
import org.wso2.carbon.apimgt.common.analytics.Constants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.wso2.carbon.apimgt.common.analytics.Constants.CHOREO_REPORTER_NAME;

/**
* Analytics event publisher for APIM.
*/
Expand All @@ -53,6 +57,20 @@ public static AnalyticsDataPublisher getInstance() {
return instance;
}

private List<String> getReporterTypesOrNull(String typeConfig) {
if (typeConfig == null) {
return null;
}
if (typeConfig.startsWith("[") && typeConfig.endsWith("]")) {
return Arrays.stream(typeConfig.substring(1, typeConfig.length() - 1).split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
} else {
return Collections.singletonList(typeConfig);
}
}

private List<String> getReportersClassesOrNull(Map<String, String> configs) {
List<String> reporterClasses = new ArrayList<>();
List<String> reporterKeys = configs.keySet()
Expand Down Expand Up @@ -93,7 +111,7 @@ private List<CounterMetric> getSuccessOrFaultyCounterMetrics(List<MetricReporter
public void initialize(AnalyticsCommonConfiguration commonConfig) {
Map<String, String> configs = commonConfig.getConfigurations();
String reporterClass = configs.get("publisher.reporter.class");
String reporterType = configs.get("type");
List<String> reporterTypes = getReporterTypesOrNull(configs.get("type"));
List<String> reporterClasses = getReportersClassesOrNull(configs);
try {
List<MetricReporter> metricReporters = new ArrayList<>();
Expand All @@ -113,9 +131,21 @@ public void initialize(AnalyticsCommonConfiguration commonConfig) {
" out of multiple metric reporters.", e);
}
}
} else if (reporterType != null && !reporterType.equals("")) {
metricReporter = MetricReporterFactory.getInstance().createLogMetricReporter(configs);
metricReporters.add(metricReporter);
} else if (reporterTypes != null && !reporterTypes.isEmpty()) {
for (String type : reporterTypes) {
if (type.equals(CHOREO_REPORTER_NAME)) {
String authEndpoint = configs.get(Constants.AUTH_API_URL);
if (authEndpoint == null || authEndpoint.isEmpty()) {
throw new MetricCreationException("Analytics Config Endpoint is not provided.");
}
metricReporter = MetricReporterFactory.getInstance().createMetricReporter(configs);
metricReporters.add(metricReporter);
} else {
metricReporter = MetricReporterFactory.getInstance().createLogMetricReporter(configs);
metricReporters.add(metricReporter);
}

}
} else {
String authEndpoint = configs.get(Constants.AUTH_API_URL);

Expand Down

0 comments on commit 994bb31

Please sign in to comment.