RedisMonitorJob implements Job { Stats stats(Map<String, String> info) { var stats = new Stats(); double maxMem = get(info, "maxmemory"); if (maxMem == 0) maxMem = get(info, "total_system_memory"); stats.put("redis_mem_max", maxMem); double usedMem = get(info, "used_memory"); stats.put("redis_mem_used", usedMem); stats.checkHighUsage(usedMem / maxMem, highMemUsageThreshold, "mem"); int keys = keys(info); stats.put("redis_keys", keys); return stats; } RedisMonitorJob(Redis redis, String app, String host, MessagePublisher<StatMessage> publisher); @Override void execute(JobContext context); public double highMemUsageThreshold; }
@Test void stats() { Stats stats = job.stats(Map.of("maxmemory", "0", "total_system_memory", "1000000", "used_memory", "800000", "db0", "keys=5,expires=0,avg_ttl=0")); assertThat(stats.result()).isEqualTo("WARN"); assertThat(stats.errorCode).isEqualTo("HIGH_MEM_USAGE"); assertThat(stats.stats) .containsEntry("redis_keys", 5d) .containsEntry("redis_mem_used", 800000d); }