Skip to content

Commit

Permalink
removing singleton bean implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowleyRajapakse committed Jul 22, 2024
1 parent 8f84267 commit 75223d4
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -70,89 +50,20 @@ 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() {
requestCountWindowStartTimeMillis = System.currentTimeMillis();
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++;;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 75223d4

Please sign in to comment.