anotheria / moskito

Monitoring for Java applications. Complete ecosystem for DevOps. Free & open source
http://www.moskito.org
MIT License
225 stars 69 forks source link

Utility to monitor collection sizes #275

Closed dvayanu closed 10 months ago

dvayanu commented 10 months ago

There are some use-cases in which the amount of data stored by a service is relevant, especially to see if its growing unreasonable. For example we have the use-case of an offerservice, where offers were generated by not deleted and the amount of entities in the database was growing and growing. It would be nice to have a utility to monitor this. The way I imagine this is:

Lets say we have a EntityService. We want to monitor the amount of entities in the easiest possible way. So we define an interface,

interface EntityManagingService{
  int getEntityCount(String entityName);
}

Now our entity service implements this and calls in constructor to MoSKito:

class OurEntityService implements EntityManagingService{
   int getEntityCount(String entityName){
     case "Account":
       return "select count(*) from account;
    case "SomethingElse":
      return "db.somecollection.count()";
  }

  OurEntityService(){
     EntityManagingServices.createEntityCounter(this, "Account", "SomethingElse", ...);
  }
}

The call to EntityManagingServices and the implementation of getEntityCount() should be the only things that are needed by the implementation. On MoSKito side the EntityManagingServices should have a scheduler and a task for each service. The task should run once an hour and call getEntityCount() for each Entity submitted in the createEntityCounter. It should create a Counter and update the value of the Counter after each iteration. It should also create an Accumulator.

i.e. (this is not correct code, just an idea)

class EntityManagingServices{

  private static ScheduledExecutorService executorService;
   {
    executorService = Executors.newSingleThreadScheduledExecutor(); // since we only check once an hour, 1 thread should be sufficient and also reduce the load
   } 
 public static void EntityManagingServices(EntityManagingService service, String... topics){
   for (String topic : topics){
    Counter c = Counter(service.getClass().getSimpleName()+"Entity"+topic+"Counter";
   //also create accumulator
     executorService.scheduleAtFixedRate(new Updater(service, topic, c), 10, 60*60, TimeUnit.SECONDS);
  }
}

static class Updater implements Runnable{
  public void run(){
      int entityCount = service.getEntityCount(topic);
      counter.setValue(entityCount);
  }
}
}