From 75223d48c524478f4eb302fc8e682d4c15d1fbdd Mon Sep 17 00:00:00 2001 From: CrowleyRajapakse Date: Mon, 22 Jul 2024 10:48:08 +0530 Subject: [PATCH] removing singleton bean implementation --- .../jmx/api/ExtAuthMetricsMXBean.java | 69 ------- .../publisher/jmx/impl/ExtAuthMetrics.java | 168 +----------------- .../prometheus/PrometheusCounterMetric.java | 17 -- 3 files changed, 4 insertions(+), 250 deletions(-) diff --git a/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/api/ExtAuthMetricsMXBean.java b/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/api/ExtAuthMetricsMXBean.java index 895f62794..6d75e6044 100644 --- a/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/api/ExtAuthMetricsMXBean.java +++ b/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/api/ExtAuthMetricsMXBean.java @@ -21,75 +21,6 @@ */ public interface ExtAuthMetricsMXBean { - /** - * Getter for total request count. - * - * @return long - */ - public long getTotalRequestCount(); - - /** - * Getter for average response time in milli seconds. - * - * @return double - */ - public double getAverageResponseTimeMillis(); - - /** - * Getter for maximum response time in milliseconds. - * - * @return double - */ - public double getMaxResponseTimeMillis(); - - /** - * Getter for mimnimum response time in milliseconds. - * - * @return double - */ - public double getMinResponseTimeMillis(); - - /** - * Resets all the metrics to their initial values. - */ - public void resetExtAuthMetrics(); - - /** - * Get request count in last five minutes window - * - * @return long - */ - public long getRequestCountInLastFiveMinuteWindow(); - - /** - * Get the start time of request count window - * - * @return - */ - public long getRequestCountWindowStartTimeMillis(); - - /** - * Get the number of token issuers - * - * @return - */ - public int getTokenIssuerCount(); - - /** - * Get the number of subscriptions - * - * @return - */ - public int getSubscriptionCount(); - - public int getTotalRequests(); - public int getPostRequests(); - public int getGetRequests(); - public String getResourcePaths(); - public String getApiName(); - public String getApplicationId(); public int getApiMessages(); - public int getSuccessRequests(); - public int getFailureRequests(); } \ No newline at end of file diff --git a/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/impl/ExtAuthMetrics.java b/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/impl/ExtAuthMetrics.java index 61ca40898..ff5650f61 100644 --- a/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/impl/ExtAuthMetrics.java +++ b/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/jmx/impl/ExtAuthMetrics.java @@ -32,31 +32,11 @@ public class ExtAuthMetrics extends TimerTask implements ExtAuthMetricsMXBean { private static final long REQUEST_COUNT_INTERVAL_MILLIS = 5 * 60 * 1000; private static ExtAuthMetrics extAuthMetricsMBean = null; private long requestCountInLastFiveMinuteWindow = 0; - private int tokenIssuerCount = 0; private long requestCountWindowStartTimeMillis = System.currentTimeMillis(); - private long totalRequestCount = 0; - private int subscriptionCount = 0; - private double averageResponseTimeMillis = 0; - private double maxResponseTimeMillis = Double.MIN_VALUE; - private double minResponseTimeMillis = Double.MAX_VALUE; private int apiMessages = 0; - private int totalRequests = 0; - private int postRequests = 0; - private int getRequests = 0; - private String resourcePaths = ""; - private String apiName = "sample1"; - private String applicationId = ""; - private int successRequests = 0; - private int failureRequests = 0; - -// private ExtAuthMetrics() { -// MBeanRegistrator.registerMBean(this); -// } private ExtAuthMetrics(APIInvocationEvent event) { - //getRequests++; - //this.apiName = apiName; try { MBeanRegistrator.registerMBean(this, event); } catch (UnsupportedEncodingException e) { @@ -70,63 +50,13 @@ private ExtAuthMetrics(APIInvocationEvent event) { * @return ExtAuthMetrics */ public static ExtAuthMetrics getInstance(APIInvocationEvent event) { - if (extAuthMetricsMBean == null) { - synchronized (ExtAuthMetrics.class) { - if (extAuthMetricsMBean == null) { - Timer timer = new Timer(); - extAuthMetricsMBean = new ExtAuthMetrics(event); - extAuthMetricsMBean.requestCountWindowStartTimeMillis = System.currentTimeMillis(); - timer.schedule(extAuthMetricsMBean, REQUEST_COUNT_INTERVAL_MILLIS, REQUEST_COUNT_INTERVAL_MILLIS); - } - } - } + Timer timer = new Timer(); + extAuthMetricsMBean = new ExtAuthMetrics(event); + extAuthMetricsMBean.requestCountWindowStartTimeMillis = System.currentTimeMillis(); + timer.schedule(extAuthMetricsMBean, REQUEST_COUNT_INTERVAL_MILLIS, REQUEST_COUNT_INTERVAL_MILLIS); return extAuthMetricsMBean; } - @Override - public long getTotalRequestCount() { - return totalRequestCount; - }; - - @Override - public double getAverageResponseTimeMillis() { - return averageResponseTimeMillis; - }; - - @Override - public double getMaxResponseTimeMillis() { - return maxResponseTimeMillis; - }; - - @Override - public double getMinResponseTimeMillis() { - return minResponseTimeMillis; - }; - - public synchronized void recordMetric(long responseTimeMillis) { - this.requestCountInLastFiveMinuteWindow += 1; - this.totalRequestCount += 1; - this.averageResponseTimeMillis = this.averageResponseTimeMillis + - (responseTimeMillis - this.averageResponseTimeMillis) / totalRequestCount; - this.minResponseTimeMillis = Math.min(this.minResponseTimeMillis, responseTimeMillis); - this.maxResponseTimeMillis = Math.max(this.maxResponseTimeMillis, responseTimeMillis); - } - - public synchronized void recordJWTIssuerMetrics(int jwtIssuers) { - this.tokenIssuerCount = jwtIssuers; - } - - public synchronized void recordSubscriptionMetrics(int subscriptionCount) { - this.subscriptionCount = subscriptionCount; - } - - @Override - public synchronized void resetExtAuthMetrics() { - this.totalRequestCount = 0; - this.averageResponseTimeMillis = 0; - this.maxResponseTimeMillis = Double.MIN_VALUE; - this.minResponseTimeMillis = Double.MAX_VALUE; - } @Override public synchronized void run() { @@ -134,25 +64,6 @@ public synchronized void run() { requestCountInLastFiveMinuteWindow = 0; } - @Override - public long getRequestCountInLastFiveMinuteWindow() { - return requestCountInLastFiveMinuteWindow; - } - - @Override - public long getRequestCountWindowStartTimeMillis() { - return requestCountWindowStartTimeMillis; - } - - @Override - public int getTokenIssuerCount() { - return tokenIssuerCount; - } - - @Override - public int getSubscriptionCount() { - return subscriptionCount; - } public void recordApiMessages() { apiMessages++;; @@ -163,75 +74,4 @@ public int getApiMessages() { return apiMessages; } - @Override - public int getTotalRequests() { - return totalRequests; - } - - @Override - public int getPostRequests() { - return postRequests; - } - - @Override - public int getGetRequests() { - return getRequests; - } - - @Override - public String getResourcePaths() { - return resourcePaths; - } - - public void incrementTotalRequests() { - totalRequests++; - } - - public void incrementPostRequests() { - postRequests++; - } - - public void incrementGetRequests() { - getRequests++; - } - - public void incrementResourcePath(String resourcePath) { - resourcePaths = resourcePath; - } - - public void incrementApiName(String apiName) { - this.apiName = apiName; - } - - @Override - public String getApiName() { - return apiName; - } - - public void incrementApplicationId(String applicationId) { - this.applicationId = applicationId; - } - - @Override - public String getApplicationId() { - return applicationId; - } - - public void incrementSuccessRequests() { - successRequests++; - } - - public void incrementFailureRequests() { - failureRequests++; - } - - @Override - public int getSuccessRequests() { - return successRequests; - } - - @Override - public int getFailureRequests() { - return failureRequests; - } } \ No newline at end of file diff --git a/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/reporter/prometheus/PrometheusCounterMetric.java b/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/reporter/prometheus/PrometheusCounterMetric.java index aa028ee60..18696f910 100644 --- a/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/reporter/prometheus/PrometheusCounterMetric.java +++ b/gateway/enforcer/org.wso2.apk.enforcer.analytics.publishers/src/main/java/org/wso2/apk/enforcer/analytics/publisher/reporter/prometheus/PrometheusCounterMetric.java @@ -108,23 +108,6 @@ public int incrementCount(MetricEventBuilder builder) throws MetricReportingExce log.info("Properties String:"+propertiesString); if (JMXUtils.isJMXMetricsEnabled()) { ExtAuthMetrics.getInstance(apiInvocationEvent).recordApiMessages(); - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementTotalRequests(); - if (apiMethod.equalsIgnoreCase("POST")) { - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementPostRequests(); - } else if (apiMethod.equalsIgnoreCase("GET")) { - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementGetRequests(); - log.info("apiMethod: " + apiMethod); - } - log.info("Proxy Response Code: " + proxyResponseCode); - log.info("Target Response Code: " + targetResponseCode); - if (proxyResponseCode == 200 || proxyResponseCode == 201 || proxyResponseCode == 202) { - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementSuccessRequests(); - } else { - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementFailureRequests(); - } - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementResourcePath(properties.get("x-original-gw-url")); - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementApiName(apiName); - ExtAuthMetrics.getInstance(apiInvocationEvent).incrementApplicationId(applicationId); log.info("apiName: " + apiName + ", applicationId: " + applicationId); } return 0;