NinjaGraphite { @Start(order = 90) public void start() { if (ninjaProperties.getBooleanWithDefault("metrics.graphite.enabled", false)) { final String customPrefix = ninjaProperties.get("metrics.graphite.prefix"); final String hostname = metricsService.getHostname(); final String prefix = StringUtils.isNotBlank(customPrefix) ? customPrefix : hostname; final String address = ninjaProperties .getOrDie("metrics.graphite.address"); final int port = ninjaProperties.getIntegerWithDefault( "metrics.graphite.port", 2003); final boolean isPickled = ninjaProperties.getBooleanWithDefault( "metrics.graphite.pickled", false); final String period = ninjaProperties.getWithDefault( "metrics.graphite.period", "60s"); final int delay = TimeUtil.parseDuration(period); final InetSocketAddress graphiteAddress = new InetSocketAddress( address, port); final GraphiteSender sender; if (isPickled) { sender = new PickledGraphite(graphiteAddress); } else { sender = new Graphite(graphiteAddress); } reporter = GraphiteReporter .forRegistry(metricsService.getMetricRegistry()) .prefixedWith(prefix).convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .filter(MetricFilter.ALL).build(sender); reporter.start(delay, TimeUnit.SECONDS); log.info( "Started Graphite Metrics reporter for '{}', updating every {}", prefix, period); } } @Inject  NinjaGraphite(NinjaProperties ninjaProperties,
                         MetricsService metricsService); @Start(order = 90) void start(); @Dispose(order = 10) void stop();  }
@Test public void testWithoutCustomPrefix() { Mockito.when(ninjaProperties.get(Mockito.eq("metrics.graphite.prefix"))).thenReturn(null); ninjaGraphite.start(); Mockito.verify(builder, Mockito.times(1)).prefixedWith(TEST_HOSTNAME); }
@Test public void testWithEmptyCustomPrefix() { Mockito.when(ninjaProperties.get(Mockito.eq("metrics.graphite.prefix"))).thenReturn(""); ninjaGraphite.start(); Mockito.verify(builder, Mockito.times(1)).prefixedWith(TEST_HOSTNAME); }
@Test public void testWithCustomPrefix() { Mockito.when(ninjaProperties.get(Mockito.eq("metrics.graphite.prefix"))).thenReturn(TEST_PREFIX); ninjaGraphite.start(); Mockito.verify(builder, Mockito.times(1)).prefixedWith(TEST_PREFIX); }