Added UserMetricsAspect
This commit is contained in:
parent
3faa88734d
commit
4817e5e395
@ -7,6 +7,8 @@ import io.swagger.v3.oas.annotations.info.License;
|
||||
import io.swagger.v3.oas.annotations.servers.Server;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@OpenAPIDefinition(
|
||||
info = @Info(
|
||||
@ -24,6 +26,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
}
|
||||
)
|
||||
@SpringBootApplication
|
||||
@EnableAspectJAutoProxy
|
||||
@EnableScheduling
|
||||
public class LoadBalancerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
package ru.dragonestia.picker.aspect;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.dragonestia.picker.repository.UserRepository;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
@RequiredArgsConstructor
|
||||
@Log4j2
|
||||
public class UserMetricsAspect {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final MeterRegistry meterRegistry;
|
||||
|
||||
private final AtomicInteger totalUsers = new AtomicInteger(0);
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
meterRegistry.gauge("roompicker_total_users", totalUsers);
|
||||
}
|
||||
|
||||
@After("execution(void ru.dragonestia.picker.repository.UserRepository.linkWithRoom(..))")
|
||||
void onLinkUsers() {
|
||||
totalUsers.set(userRepository.countAllUsers());
|
||||
}
|
||||
|
||||
@After("execution(void ru.dragonestia.picker.repository.UserRepository.unlinkWithRoom(..))")
|
||||
void onUnlinkUsers() {
|
||||
totalUsers.set(userRepository.countAllUsers());
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 3_000)
|
||||
void updateUserMetrics() {
|
||||
// TODO: metrics for userRepository.countUsersForNodes()
|
||||
}
|
||||
}
|
||||
@ -12,7 +12,7 @@ public interface UserRepository {
|
||||
|
||||
Map<User, Boolean> linkWithRoom(Room room, Collection<User> users, boolean force) throws RoomAreFullException;
|
||||
|
||||
int unlinkWithRoom(Room room, Collection<User> users);
|
||||
void unlinkWithRoom(Room room, Collection<User> users);
|
||||
|
||||
List<Room> findAllLinkedUserRooms(User user);
|
||||
|
||||
|
||||
@ -66,7 +66,7 @@ public class UserRepositoryImpl implements UserRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int unlinkWithRoom(Room room, Collection<User> users) {
|
||||
public void unlinkWithRoom(Room room, Collection<User> users) {
|
||||
var counter = new AtomicInteger();
|
||||
|
||||
lock.writeLock().lock();
|
||||
@ -98,7 +98,7 @@ public class UserRepositoryImpl implements UserRepository {
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
return counter.get();
|
||||
counter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user