I have spring boot application and I want to track no of logged in users using a metric ( I am using micrometer
) . My idea was to track /login rest call which we have and increment a guage. However I don't have a solution to decrease metric ( gauge count ) with user expire. If i want to decrease gauge based on fixed time after i increase the gauge, how can i do that ? Also my service has two instance, therefore to be consistent i plan to use load
balancer session stickiness. With that i hope same user ( IP ) will land on same machine.
Following is my gauge
AtomicInteger noOfLoggedInUsers = registry.gauge("logged.user.count", new AtomicInteger(0));
This is how i increment gauge
public int addUser(final String username) {
final String usernameVal = cache.get(username);
if (usernameVal == null) {
cache.put(username, username);
noOfLoggedInUsers.incrementAndGet();
}
return noOfLoggedInUsers.get();
}
I am using map to store unique users. Also i want to know is there a better way i can do to track no of logged in users.