protected static MetricBuilder<Timer> generateDefaultTimerMetricBuilder(Pair<Long, TimeUnit> reportingFrequency) { if (reportingFrequency == null) throw new IllegalArgumentException("reportingFrequency cannot be null"); if (reportingFrequency.getLeft() == null) throw new IllegalArgumentException("reportingFrequency amount cannot be null"); if (reportingFrequency.getRight() == null) throw new IllegalArgumentException("reportingFrequency TimeUnit cannot be null"); return new RollingWindowTimerBuilder(reportingFrequency.getLeft(), reportingFrequency.getRight()); }
@Test public void RollingWindowTimerBuilder_constructor_sets_fields_as_expected() { // given long amount = 42; TimeUnit timeUnit = TimeUnit.DAYS; // when RollingWindowTimerBuilder rwtb = new RollingWindowTimerBuilder(amount, timeUnit); // then assertThat(rwtb.amount).isEqualTo(amount); assertThat(rwtb.timeUnit).isEqualTo(timeUnit); }
@DataProvider(value = { "true | true", "false | false" }, splitBy = "\\|") @Test public void RollingWindowTimerBuilder_isInstance_works_as_expected(boolean useTimer, boolean expectedResult) { // given Metric metric = (useTimer) ? mock(Timer.class) : mock(Gauge.class); RollingWindowTimerBuilder rwtb = new RollingWindowTimerBuilder(42, TimeUnit.DAYS); // when boolean result = rwtb.isInstance(metric); // then assertThat(result).isEqualTo(expectedResult); }
/** * Creates a new instance with the given {@link MetricRegistry}, and using the given {@link SignalFxReporterFactory} * to retrieve the {@link SignalFxReporter#getMetricMetadata()} and reporting frequency. * * <p>Note that {@link Timer}s and {@link Histogram}s created from this class will have {@link * SlidingTimeWindowReservoir} reservoirs that match the given {@link SignalFxReporterFactory#getInterval()} and * {@link SignalFxReporterFactory#getTimeUnit()}. * * @param sfxReporterFactory The {@link SignalFxReporterFactory} to use to get the {@link * SignalFxReporter#getMetricMetadata()} and reporting frequency. */ public SignalFxAwareCodahaleMetricsCollector(MetricRegistry metricRegistry, SignalFxReporterFactory sfxReporterFactory) { this(metricRegistry, sfxReporterFactory.getReporter(metricRegistry).getMetricMetadata(), new RollingWindowTimerBuilder(sfxReporterFactory.getInterval(), sfxReporterFactory.getTimeUnit()), new RollingWindowHistogramBuilder(sfxReporterFactory.getInterval(), sfxReporterFactory.getTimeUnit()) ); }
@DataProvider(value = { "42 | DAYS", "123 | SECONDS", "999 | MILLISECONDS", "3 | HOURS" }, splitBy = "\\|") @Test public void RollingWindowTimerBuilder_newMetric_creates_new_timer_with_SlidingTimeWindowReservoir_with_expected_values( long amount, TimeUnit timeUnit ) { // given RollingWindowTimerBuilder rwtb = new RollingWindowTimerBuilder(amount, timeUnit); // when Timer timer = rwtb.newMetric(); // then Histogram histogram = (Histogram) getInternalState(timer, "histogram"); Reservoir reservoir = (Reservoir) getInternalState(histogram, "reservoir"); assertThat(reservoir).isInstanceOf(SlidingTimeWindowReservoir.class); // The expected value here comes from logic in the SlidingTimeWindowReservoir constructor. assertThat(getInternalState(reservoir, "window")).isEqualTo(timeUnit.toNanos(amount) * 256); }
@Test public void RollingWindowTimerBuilder_newMetric_creates_a_new_timer_with_each_call() { // given RollingWindowTimerBuilder rwtb = new RollingWindowTimerBuilder(42, TimeUnit.DAYS); // when Timer firstCallTimer = rwtb.newMetric(); Timer secondCallTimer = rwtb.newMetric(); // then assertThat(firstCallTimer).isNotSameAs(secondCallTimer); }