Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class Min extends SampledStat {

public Min() {
super(Double.MIN_VALUE);
super(Double.MAX_VALUE);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch (and sorry for long delay in review).
I saw we are using NEGATIVE_INFINITY for Max() default. Any reason not to use INFINITY here? It seems more consistent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I noticed that too, and I think it would be more consistent to use INFINITY, but the convention seems to be that the combine implementation uses the same value for the de facto default (even though it doesn't make use of initialValue). Min() is already using Double.MAX_VALUE in combine, so also changing that felt like a bigger change (and possibly unwelcome if users were already handling Double.MAX_VALUE in their metrics tooling).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough. LGTM.

}

@Override
Expand All @@ -32,10 +32,10 @@ protected void update(Sample sample, MetricConfig config, double value, long now

@Override
public double combine(List<Sample> samples, MetricConfig config, long now) {
double max = Double.MAX_VALUE;
double min = Double.MAX_VALUE;
for (int i = 0; i < samples.size(); i++)
max = Math.min(max, samples.get(i).value);
return max;
min = Math.min(min, samples.get(i).value);
return min;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,38 @@ public void testOldDataHasNoEffect() {
assertEquals(Double.NEGATIVE_INFINITY, max.measure(config, time.milliseconds()), EPS);
}

@Test
public void testSampledStatInitialValue() {
// initialValue from each SampledStat is set as the initialValue on its Sample.
// The only way to test the initialValue is to infer it by having a SampledStat
// with expired Stats, because their values are reset to the initial values.
// Most implementations of combine on SampledStat end up returning the default
// value, so we can use this. This doesn't work for Percentiles though.
// This test looks a lot like testOldDataHasNoEffect because it's the same
// flow that leads to this state.
Max max = new Max();
Min min = new Min();
Avg avg = new Avg();
Count count = new Count();
Rate.SampledTotal sampledTotal = new Rate.SampledTotal();

long windowMs = 100;
int samples = 2;
MetricConfig config = new MetricConfig().timeWindow(windowMs, TimeUnit.MILLISECONDS).samples(samples);
max.record(config, 50, time.milliseconds());
min.record(config, 50, time.milliseconds());
avg.record(config, 50, time.milliseconds());
count.record(config, 50, time.milliseconds());
sampledTotal.record(config, 50, time.milliseconds());
time.sleep(samples * windowMs);

assertEquals(Double.NEGATIVE_INFINITY, max.measure(config, time.milliseconds()), EPS);
assertEquals(Double.MAX_VALUE, min.measure(config, time.milliseconds()), EPS);
assertEquals(0.0, avg.measure(config, time.milliseconds()), EPS);
assertEquals(0, count.measure(config, time.milliseconds()), EPS);
assertEquals(0.0, sampledTotal.measure(config, time.milliseconds()), EPS);
}

@Test(expected = IllegalArgumentException.class)
public void testDuplicateMetricName() {
metrics.sensor("test").add(metrics.metricName("test", "grp1"), new Avg());
Expand Down