Search in sources :

Example 1 with WindowedSum

use of org.apache.kafka.common.metrics.stats.WindowedSum in project kafka by apache.

the class SensorTest method testIdempotentAdd.

@Test
public void testIdempotentAdd() {
    final Metrics metrics = new Metrics();
    final Sensor sensor = metrics.sensor("sensor");
    assertTrue(sensor.add(metrics.metricName("test-metric", "test-group"), new Avg()));
    // adding the same metric to the same sensor is a no-op
    assertTrue(sensor.add(metrics.metricName("test-metric", "test-group"), new Avg()));
    // but adding the same metric to a DIFFERENT sensor is an error
    final Sensor anotherSensor = metrics.sensor("another-sensor");
    try {
        anotherSensor.add(metrics.metricName("test-metric", "test-group"), new Avg());
        fail("should have thrown");
    } catch (final IllegalArgumentException ignored) {
    // pass
    }
    // note that adding a different metric with the same name is also a no-op
    assertTrue(sensor.add(metrics.metricName("test-metric", "test-group"), new WindowedSum()));
    // so after all this, we still just have the original metric registered
    assertEquals(1, sensor.metrics().size());
    assertEquals(org.apache.kafka.common.metrics.stats.Avg.class, sensor.metrics().get(0).measurable().getClass());
}
Also used : Avg(org.apache.kafka.common.metrics.stats.Avg) WindowedSum(org.apache.kafka.common.metrics.stats.WindowedSum) Test(org.junit.jupiter.api.Test)

Example 2 with WindowedSum

use of org.apache.kafka.common.metrics.stats.WindowedSum in project kafka by apache.

the class KafkaMbeanTest method setup.

@BeforeEach
public void setup() throws Exception {
    metrics = new Metrics();
    metrics.addReporter(new JmxReporter());
    sensor = metrics.sensor("kafka.requests");
    countMetricName = metrics.metricName("pack.bean1.count", "grp1");
    sensor.add(countMetricName, new WindowedCount());
    sumMetricName = metrics.metricName("pack.bean1.sum", "grp1");
    sensor.add(sumMetricName, new WindowedSum());
}
Also used : WindowedSum(org.apache.kafka.common.metrics.stats.WindowedSum) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with WindowedSum

use of org.apache.kafka.common.metrics.stats.WindowedSum in project kafka by apache.

the class MetricsTest method testSampledStatReturnsInitialValueWhenNoValuesExist.

/**
 * Some implementations of SampledStat make sense to return the initial value
 * when there are no values set
 */
@Test
public void testSampledStatReturnsInitialValueWhenNoValuesExist() {
    WindowedCount count = new WindowedCount();
    WindowedSum sampledTotal = new WindowedSum();
    long windowMs = 100;
    int samples = 2;
    MetricConfig config = new MetricConfig().timeWindow(windowMs, TimeUnit.MILLISECONDS).samples(samples);
    count.record(config, 50, time.milliseconds());
    sampledTotal.record(config, 50, time.milliseconds());
    time.sleep(samples * windowMs);
    assertEquals(0, count.measure(config, time.milliseconds()), EPS);
    assertEquals(0.0, sampledTotal.measure(config, time.milliseconds()), EPS);
}
Also used : WindowedSum(org.apache.kafka.common.metrics.stats.WindowedSum) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) Test(org.junit.jupiter.api.Test)

Example 4 with WindowedSum

use of org.apache.kafka.common.metrics.stats.WindowedSum in project kafka by apache.

the class SensorTest method shouldReturnPresenceOfMetrics.

@Test
public void shouldReturnPresenceOfMetrics() {
    final Metrics metrics = new Metrics();
    final Sensor sensor = metrics.sensor("sensor");
    assertFalse(sensor.hasMetrics());
    sensor.add(new MetricName("name1", "group1", "description1", Collections.emptyMap()), new WindowedSum());
    assertTrue(sensor.hasMetrics());
    sensor.add(new MetricName("name2", "group2", "description2", Collections.emptyMap()), new CumulativeCount());
    assertTrue(sensor.hasMetrics());
}
Also used : MetricName(org.apache.kafka.common.MetricName) CumulativeCount(org.apache.kafka.common.metrics.stats.CumulativeCount) WindowedSum(org.apache.kafka.common.metrics.stats.WindowedSum) Test(org.junit.jupiter.api.Test)

Aggregations

WindowedSum (org.apache.kafka.common.metrics.stats.WindowedSum)4 Test (org.junit.jupiter.api.Test)3 WindowedCount (org.apache.kafka.common.metrics.stats.WindowedCount)2 MetricName (org.apache.kafka.common.MetricName)1 Avg (org.apache.kafka.common.metrics.stats.Avg)1 CumulativeCount (org.apache.kafka.common.metrics.stats.CumulativeCount)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1