vikaskr22 commented on code in PR #494: URL: https://github.com/apache/ranger/pull/494#discussion_r1902286199
########## kms/src/main/java/org/apache/ranger/kms/metrics/collector/KMSMetricsCollector.java: ########## @@ -82,4 +84,54 @@ public void updateMetric(KMSMetrics.KMSMetric metric, long val) { this.metrics.compute(metric, (k, v) -> null == v ? val : v + val); } } + + public APIMetric createAPIMetric(KMSMetrics.KMSMetric counter, KMSMetrics.KMSMetric elapsedTime) { + return new APIMetric(counter, elapsedTime); + } + + /** + * This method starts the Stopwatch to capture the elapsed time but KMSMetrics is not yet set. + * It may be used to address the use cases where exact metric name would be known after some processing, + * in such cases, Initially StopWatch can be started and specific metric can be set once known. + * + * If metric is not set, this StopWatch will be stopped and ignored. A warn log message will be logged. + * @return : APIMetric + */ + public APIMetric captureElapsedTime() { + return new APIMetric(); + } + + public class APIMetric implements AutoCloseable { + private KMSMetrics.KMSMetric counter; + private KMSMetrics.KMSMetric elapsedTime; + private Stopwatch sw; + + private APIMetric(KMSMetrics.KMSMetric counter, KMSMetrics.KMSMetric elapsedTime) { + this.counter = counter; + this.elapsedTime = elapsedTime; + this.sw = Stopwatch.createStarted(); + } + + private APIMetric() { + this.sw = Stopwatch.createStarted(); + } + + public void setMetrics(KMSMetrics.KMSMetric counter, KMSMetrics.KMSMetric elapsedTime) { + this.counter = counter; + this.elapsedTime = elapsedTime; + } + + @Override + public void close() { + if (null != counter && null != elapsedTime) { + incrementCounter(counter); + updateMetric(elapsedTime, sw.stop().elapsed(TimeUnit.MILLISECONDS)); + } + + if (null != sw && sw.isRunning()) { Review Comment: incorporated the review comment. Thanks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@ranger.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org