Friday, 19 May 2023

Design an utility class to capture application metrics summary in Java

Example1: Metric to capture api’s response time

 

Following metrics to be captured on api response time.

a.   Total number of requests

b.   Sum of all the response times

c.    Maximum response time across all the apis

d.   Minimum response time across all the apis

 

Example 2: Metric to capture outbound data transfer

a.   Total number of data outbound requests.

b.   Sum of data transferred

c.    Maximum data size transferred in a given request

d.   Minimum data size transferred in a given request

 

If you see above examples, it is good to capture the unit type in the metrics summary.

 

For example,

a.   Unit for the response time can be milliseconds, seconds, minutes etc., for response time and

b.   Unit for the data size can be in bits, bytes, KBs, MBs etc.,

 

Every Metric should have a name like apiResponseTimes, outboundDataSize etc., As we are going to maintain only the summary, we no need to store the granular level details like what is the response time at api level etc.,

 

Find the below working application.

 

AppMetric.java

package com.sample.app.commons;

public enum AppMetric {
	API_RESPONSE_TIMES("Api response times"), OUT_BOUND_DATA_SIZE("Out bound data size");

	AppMetric(String metricName) {
		this.metricName = metricName;
	}

	String metricName;
}

AppMetricUnit.java

package com.sample.app.commons;

public enum AppMetricUnit {
	
	MILLI_SECONDS,
	SECONDS,
	MINUTES,
	BITS,
	BYTES
}

 

AppMetricStats.java

package com.sample.app.commons;

import java.util.concurrent.atomic.AtomicLong;

public class AppMetricStats {
	private final AppMetric appMetric;
	private final AppMetricUnit appMetricUnit;
	private final AtomicLong sum = new AtomicLong();
	private final AtomicLong count = new AtomicLong();
	private final AtomicLong maxValue = new AtomicLong(Long.MIN_VALUE);
	private final AtomicLong minValue = new AtomicLong(Long.MAX_VALUE);

	public AppMetricStats(AppMetric appMetric, AppMetricUnit appMetricUnit) {
		this.appMetric = appMetric;
		this.appMetricUnit = appMetricUnit;
	}

	public AppMetric getAppMeric() {
		return appMetric;
	}

	public long getSum() {
		return sum.get();
	}

	public long getCount() {
		return count.get();
	}

	public long getMaxValue() {
		return maxValue.get();
	}

	public long getMinValue() {
		return minValue.get();
	}

	public AppMetricUnit getAppMetricUnit() {
		return appMetricUnit;
	}

	public void addValue(long value) {
		sum.addAndGet(value);
		count.incrementAndGet();
		maxValue.accumulateAndGet(value, Math::max);
		minValue.accumulateAndGet(value, Math::min);
	}

	public void set(long sum, long count, long max, long min) {
		this.sum.set(sum);
		this.count.set(count);
		this.maxValue.set(max);
		this.minValue.set(min);
	}

	@Override
	public String toString() {
		return "AppMetricStats [appMetric=" + appMetric + ", appMetricUnit=" + appMetricUnit + ", sum=" + sum
				+ ", count=" + count + ", maxValue=" + maxValue + ", minValue=" + minValue + "]";
	}

}

AppMetricStatsDemo.java

package com.sample.app;

import com.sample.app.commons.AppMetric;
import com.sample.app.commons.AppMetricStats;
import com.sample.app.commons.AppMetricUnit;

public class AppMetricStatsDemo {
	
	public static void main(String[] args) {
		AppMetricStats stats = new AppMetricStats(AppMetric.API_RESPONSE_TIMES, AppMetricUnit.MILLI_SECONDS);
		
		stats.addValue(100);
		System.out.println(stats);
		
		stats.addValue(90);
		System.out.println(stats);
		
		stats.addValue(900);
		System.out.println(stats);
		
	}

}

Output

AppMetricStats [appMetric=API_RESPONSE_TIMES, appMetricUnit=MILLI_SECONDS, sum=100, count=1, maxValue=100, minValue=100]
AppMetricStats [appMetric=API_RESPONSE_TIMES, appMetricUnit=MILLI_SECONDS, sum=190, count=2, maxValue=100, minValue=90]
AppMetricStats [appMetric=API_RESPONSE_TIMES, appMetricUnit=MILLI_SECONDS, sum=1090, count=3, maxValue=900, minValue=90]






You may like

Interview Questions

How to break out of nested loops in Java?

Implementation of Bag data structure in Java

Scanner throws java.util.NoSuchElementException while reading the input

How to get all the enum values in Java?

Extend the thread to check it’s running status

Implement retry handler for a task in Java

No comments:

Post a Comment