Search in sources :

Example 1 with CumulativeSum

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

the class MetricsTest method testQuotas.

@Test
public void testQuotas() {
    Sensor sensor = metrics.sensor("test");
    sensor.add(metrics.metricName("test1.total", "grp1"), new CumulativeSum(), new MetricConfig().quota(Quota.upperBound(5.0)));
    sensor.add(metrics.metricName("test2.total", "grp1"), new CumulativeSum(), new MetricConfig().quota(Quota.lowerBound(0.0)));
    sensor.record(5.0);
    try {
        sensor.record(1.0);
        fail("Should have gotten a quota violation.");
    } catch (QuotaViolationException e) {
    // this is good
    }
    assertEquals(6.0, (Double) metrics.metrics().get(metrics.metricName("test1.total", "grp1")).metricValue(), EPS);
    sensor.record(-6.0);
    try {
        sensor.record(-1.0);
        fail("Should have gotten a quota violation.");
    } catch (QuotaViolationException e) {
    // this is good
    }
}
Also used : CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) Test(org.junit.jupiter.api.Test)

Example 2 with CumulativeSum

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

the class MetricsTest method testDuplicateMetricName.

@Test
public void testDuplicateMetricName() {
    metrics.sensor("test").add(metrics.metricName("test", "grp1"), new Avg());
    assertThrows(IllegalArgumentException.class, () -> metrics.sensor("test2").add(metrics.metricName("test", "grp1"), new CumulativeSum()));
}
Also used : CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) Avg(org.apache.kafka.common.metrics.stats.Avg) Test(org.junit.jupiter.api.Test)

Example 3 with CumulativeSum

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

the class MetricsTest method verifyStats.

private void verifyStats(Function<KafkaMetric, Double> metricValueFunc) {
    ConstantMeasurable measurable = new ConstantMeasurable();
    metrics.addMetric(metrics.metricName("direct.measurable", "grp1", "The fraction of time an appender waits for space allocation."), measurable);
    Sensor s = metrics.sensor("test.sensor");
    s.add(metrics.metricName("test.avg", "grp1"), new Avg());
    s.add(metrics.metricName("test.max", "grp1"), new Max());
    s.add(metrics.metricName("test.min", "grp1"), new Min());
    s.add(new Meter(TimeUnit.SECONDS, metrics.metricName("test.rate", "grp1"), metrics.metricName("test.total", "grp1")));
    s.add(new Meter(TimeUnit.SECONDS, new WindowedCount(), metrics.metricName("test.occurences", "grp1"), metrics.metricName("test.occurences.total", "grp1")));
    s.add(metrics.metricName("test.count", "grp1"), new WindowedCount());
    s.add(new Percentiles(100, -100, 100, BucketSizing.CONSTANT, new Percentile(metrics.metricName("test.median", "grp1"), 50.0), new Percentile(metrics.metricName("test.perc99_9", "grp1"), 99.9)));
    Sensor s2 = metrics.sensor("test.sensor2");
    s2.add(metrics.metricName("s2.total", "grp1"), new CumulativeSum());
    s2.record(5.0);
    int sum = 0;
    int count = 10;
    for (int i = 0; i < count; i++) {
        s.record(i);
        sum += i;
    }
    // prior to any time passing
    double elapsedSecs = (config.timeWindowMs() * (config.samples() - 1)) / 1000.0;
    assertEquals(count / elapsedSecs, metricValueFunc.apply(metrics.metrics().get(metrics.metricName("test.occurences", "grp1"))), EPS, String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs));
    // pretend 2 seconds passed...
    long sleepTimeMs = 2;
    time.sleep(sleepTimeMs * 1000);
    elapsedSecs += sleepTimeMs;
    assertEquals(5.0, metricValueFunc.apply(metrics.metric(metrics.metricName("s2.total", "grp1"))), EPS, "s2 reflects the constant value");
    assertEquals(4.5, metricValueFunc.apply(metrics.metric(metrics.metricName("test.avg", "grp1"))), EPS, "Avg(0...9) = 4.5");
    assertEquals(count - 1, metricValueFunc.apply(metrics.metric(metrics.metricName("test.max", "grp1"))), EPS, "Max(0...9) = 9");
    assertEquals(0.0, metricValueFunc.apply(metrics.metric(metrics.metricName("test.min", "grp1"))), EPS, "Min(0...9) = 0");
    assertEquals(sum / elapsedSecs, metricValueFunc.apply(metrics.metric(metrics.metricName("test.rate", "grp1"))), EPS, "Rate(0...9) = 1.40625");
    assertEquals(count / elapsedSecs, metricValueFunc.apply(metrics.metric(metrics.metricName("test.occurences", "grp1"))), EPS, String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs));
    assertEquals(count, metricValueFunc.apply(metrics.metric(metrics.metricName("test.count", "grp1"))), EPS, "Count(0...9) = 10");
}
Also used : Percentile(org.apache.kafka.common.metrics.stats.Percentile) Max(org.apache.kafka.common.metrics.stats.Max) Meter(org.apache.kafka.common.metrics.stats.Meter) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) Avg(org.apache.kafka.common.metrics.stats.Avg) Min(org.apache.kafka.common.metrics.stats.Min)

Example 4 with CumulativeSum

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

the class JmxReporterTest method testPredicateAndDynamicReload.

@Test
public void testPredicateAndDynamicReload() throws Exception {
    Metrics metrics = new Metrics();
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Map<String, String> configs = new HashMap<>();
    configs.put(JmxReporter.EXCLUDE_CONFIG, JmxReporter.getMBeanName("", metrics.metricName("pack.bean2.total", "grp2")));
    try {
        JmxReporter reporter = new JmxReporter();
        reporter.configure(configs);
        metrics.addReporter(reporter);
        Sensor sensor = metrics.sensor("kafka.requests");
        sensor.add(metrics.metricName("pack.bean2.avg", "grp1"), new Avg());
        sensor.add(metrics.metricName("pack.bean2.total", "grp2"), new CumulativeSum());
        sensor.record();
        assertTrue(server.isRegistered(new ObjectName(":type=grp1")));
        assertEquals(1.0, server.getAttribute(new ObjectName(":type=grp1"), "pack.bean2.avg"));
        assertFalse(server.isRegistered(new ObjectName(":type=grp2")));
        sensor.record();
        configs.put(JmxReporter.EXCLUDE_CONFIG, JmxReporter.getMBeanName("", metrics.metricName("pack.bean2.avg", "grp1")));
        reporter.reconfigure(configs);
        assertFalse(server.isRegistered(new ObjectName(":type=grp1")));
        assertTrue(server.isRegistered(new ObjectName(":type=grp2")));
        assertEquals(2.0, server.getAttribute(new ObjectName(":type=grp2"), "pack.bean2.total"));
        metrics.removeMetric(metrics.metricName("pack.bean2.total", "grp2"));
        assertFalse(server.isRegistered(new ObjectName(":type=grp2")));
    } finally {
        metrics.close();
    }
}
Also used : CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) Avg(org.apache.kafka.common.metrics.stats.Avg) HashMap(java.util.HashMap) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Example 5 with CumulativeSum

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

the class JmxReporterTest method testJmxRegistrationSanitization.

@Test
public void testJmxRegistrationSanitization() throws Exception {
    Metrics metrics = new Metrics();
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    try {
        metrics.addReporter(new JmxReporter());
        Sensor sensor = metrics.sensor("kafka.requests");
        sensor.add(metrics.metricName("name", "group", "desc", "id", "foo*"), new CumulativeSum());
        sensor.add(metrics.metricName("name", "group", "desc", "id", "foo+"), new CumulativeSum());
        sensor.add(metrics.metricName("name", "group", "desc", "id", "foo?"), new CumulativeSum());
        sensor.add(metrics.metricName("name", "group", "desc", "id", "foo:"), new CumulativeSum());
        sensor.add(metrics.metricName("name", "group", "desc", "id", "foo%"), new CumulativeSum());
        assertTrue(server.isRegistered(new ObjectName(":type=group,id=\"foo\\*\"")));
        assertEquals(0.0, server.getAttribute(new ObjectName(":type=group,id=\"foo\\*\""), "name"));
        assertTrue(server.isRegistered(new ObjectName(":type=group,id=\"foo+\"")));
        assertEquals(0.0, server.getAttribute(new ObjectName(":type=group,id=\"foo+\""), "name"));
        assertTrue(server.isRegistered(new ObjectName(":type=group,id=\"foo\\?\"")));
        assertEquals(0.0, server.getAttribute(new ObjectName(":type=group,id=\"foo\\?\""), "name"));
        assertTrue(server.isRegistered(new ObjectName(":type=group,id=\"foo:\"")));
        assertEquals(0.0, server.getAttribute(new ObjectName(":type=group,id=\"foo:\""), "name"));
        assertTrue(server.isRegistered(new ObjectName(":type=group,id=foo%")));
        assertEquals(0.0, server.getAttribute(new ObjectName(":type=group,id=foo%"), "name"));
        metrics.removeMetric(metrics.metricName("name", "group", "desc", "id", "foo*"));
        metrics.removeMetric(metrics.metricName("name", "group", "desc", "id", "foo+"));
        metrics.removeMetric(metrics.metricName("name", "group", "desc", "id", "foo?"));
        metrics.removeMetric(metrics.metricName("name", "group", "desc", "id", "foo:"));
        metrics.removeMetric(metrics.metricName("name", "group", "desc", "id", "foo%"));
        assertFalse(server.isRegistered(new ObjectName(":type=group,id=\"foo\\*\"")));
        assertFalse(server.isRegistered(new ObjectName(":type=group,id=foo+")));
        assertFalse(server.isRegistered(new ObjectName(":type=group,id=\"foo\\?\"")));
        assertFalse(server.isRegistered(new ObjectName(":type=group,id=\"foo:\"")));
        assertFalse(server.isRegistered(new ObjectName(":type=group,id=foo%")));
    } finally {
        metrics.close();
    }
}
Also used : CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Aggregations

CumulativeSum (org.apache.kafka.common.metrics.stats.CumulativeSum)10 Avg (org.apache.kafka.common.metrics.stats.Avg)5 Test (org.junit.jupiter.api.Test)5 MetricName (org.apache.kafka.common.MetricName)4 MBeanServer (javax.management.MBeanServer)3 ObjectName (javax.management.ObjectName)3 Sensor (org.apache.kafka.common.metrics.Sensor)3 HashMap (java.util.HashMap)1 Max (org.apache.kafka.common.metrics.stats.Max)1 Meter (org.apache.kafka.common.metrics.stats.Meter)1 Min (org.apache.kafka.common.metrics.stats.Min)1 Percentile (org.apache.kafka.common.metrics.stats.Percentile)1 Percentiles (org.apache.kafka.common.metrics.stats.Percentiles)1 WindowedCount (org.apache.kafka.common.metrics.stats.WindowedCount)1