GraphiteTargetChecker implements TargetChecker { @Override public Map<String, Optional<BigDecimal>> check(Check check) throws Exception { Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); try { JsonNode node = graphiteHttpClient.getTargetJson(check.getTarget(), check.getFrom(), check.getUntil()); for (JsonNode metric : node) { String target = metric.path("target").asText(); try { BigDecimal value = getLatestValue(metric); targetValues.put(target, Optional.of(value)); } catch (InvalidGraphiteValueException e) { LOGGER.warn("{} failed to read from Graphite", check.getName(), e); targetValues.put(target, Optional.<BigDecimal> absent()); } } } catch (GraphiteReadException e) { LOGGER.warn(check.getName() + " failed to read from Graphite", e); } return targetValues; } @Inject  GraphiteTargetChecker(GraphiteHttpClient graphiteHttpClient); @Override Map<String, Optional<BigDecimal>> check(Check check);  }
@Test public void singleValidTargetIsPresent() throws Exception { JsonNode node = MAPPER.readTree("[{\"target\": \"service.error.1MinuteRate\", \"datapoints\": [[0.06, 1337453460]]}]"); when(mockGraphiteHttpClient.getTargetJson("service.error.1MinuteRate", null, null)).thenReturn(node); Map<String, Optional<BigDecimal>> values = checker.check(check()); assertThat(values, hasKey("service.error.1MinuteRate")); }
@Test public void singleValidTargetHasCorrectValue() throws Exception { JsonNode node = MAPPER.readTree("[{\"target\": \"service.error.1MinuteRate\", \"datapoints\": [[0.06, 1337453460]]}]"); when(mockGraphiteHttpClient.getTargetJson("service.error.1MinuteRate", null, null)).thenReturn(node); Map<String, Optional<BigDecimal>> values = checker.check(check()); assertThat(values.get("service.error.1MinuteRate").isPresent(), is(true)); assertThat(values.get("service.error.1MinuteRate").get(), is(new BigDecimal("0.06"))); }
@Test public void valueIsDeterminedByGoingThroughDatapointsInReverserOrder() throws Exception { JsonNode node = MAPPER.readTree("[{\"target\": \"service.error.1MinuteRate\", \"datapoints\": [[0.20, 1337453460],[0.01, 1337453463]]}]"); when(mockGraphiteHttpClient.getTargetJson("service.error.1MinuteRate", null, null)).thenReturn(node); Map<String, Optional<BigDecimal>> values = checker.check(check()); assertThat(values.get("service.error.1MinuteRate").get(), is(new BigDecimal("0.01"))); }
@Test public void valueIsDeterminedBySkippingNullValues() throws Exception { JsonNode node = MAPPER.readTree("[{\"target\": \"service.error.1MinuteRate\", \"datapoints\": [[0.17, 1337453460],[null, 1337453463]]}]"); when(mockGraphiteHttpClient.getTargetJson("service.error.1MinuteRate", null, null)).thenReturn(node); Map<String, Optional<BigDecimal>> values = checker.check(check()); assertThat(values.get("service.error.1MinuteRate").get(), is(new BigDecimal("0.17"))); }
@Test public void targetWhichOnlyHasNullValuesIsAbsent() throws Exception { JsonNode node = MAPPER.readTree("[{\"target\": \"service.error.1MinuteRate\", \"datapoints\": [[null, 1337453460],[null, 1337453463]]}]"); when(mockGraphiteHttpClient.getTargetJson("service.error.1MinuteRate", null, null)).thenReturn(node); Map<String, Optional<BigDecimal>> values = checker.check(check()); assertThat(values.get("service.error.1MinuteRate").isPresent(), is(false)); }
@Test public void multipleTargetsAreHandledCorrectly() throws Exception { JsonNode node = MAPPER.readTree("[" + "{\"target\": \"service.error.1MinuteRate\", \"datapoints\": [[0.20, 1337453460],[0.01, 1337453463]]}," + "{\"target\": \"service.warn.1MinuteRate\", \"datapoints\": [[0.56, 1337453460],[0.78, 1337453463]]}" + "]"); when(mockGraphiteHttpClient.getTargetJson("service.*.1MinuteRate", null, null)).thenReturn(node); Map<String, Optional<BigDecimal>> values = checker.check(checkWithTarget("service.*.1MinuteRate")); assertThat(values.entrySet(), hasSize(2)); assertThat(values.get("service.error.1MinuteRate").get(), is(new BigDecimal("0.01"))); assertThat(values.get("service.warn.1MinuteRate").get(), is(new BigDecimal("0.78"))); }
@Test public void exceptionGettingDataFromGraphiteIsHandled() throws Exception { when(mockGraphiteHttpClient.getTargetJson("service.*.1MinuteRate", null, null)).thenThrow(new GraphiteReadException("Graphite bad times", new RuntimeException("Bad times"))); Map<String, Optional<BigDecimal>> values = checker.check(checkWithTarget("service.*.1MinuteRate")); assertThat(values.size(), is(0)); }