Search in sources :

Example 1 with Meter

use of org.apache.kafka.common.metrics.stats.Meter in project apache-kafka-on-k8s by banzaicloud.

the class BufferPoolTest method testCleanupMemoryAvailabilityOnMetricsException.

@PrepareForTest({ Sensor.class, MetricName.class })
@Test
public void testCleanupMemoryAvailabilityOnMetricsException() throws Exception {
    Metrics mockedMetrics = createNiceMock(Metrics.class);
    Sensor mockedSensor = createNiceMock(Sensor.class);
    MetricName metricName = createNiceMock(MetricName.class);
    MetricName rateMetricName = createNiceMock(MetricName.class);
    MetricName totalMetricName = createNiceMock(MetricName.class);
    expect(mockedMetrics.sensor(BufferPool.WAIT_TIME_SENSOR_NAME)).andReturn(mockedSensor);
    mockedSensor.record(anyDouble(), anyLong());
    expectLastCall().andThrow(new OutOfMemoryError());
    expect(mockedMetrics.metricName(anyString(), eq(metricGroup), anyString())).andReturn(metricName);
    expect(mockedSensor.add(new Meter(TimeUnit.NANOSECONDS, rateMetricName, totalMetricName))).andReturn(true);
    replay(mockedMetrics, mockedSensor, metricName);
    BufferPool bufferPool = new BufferPool(2, 1, mockedMetrics, time, metricGroup);
    bufferPool.allocate(1, 0);
    try {
        bufferPool.allocate(2, 1000);
        assertTrue("Expected oom.", false);
    } catch (OutOfMemoryError expected) {
    }
    assertEquals(1, bufferPool.availableMemory());
    assertEquals(0, bufferPool.queued());
    // This shouldn't timeout
    bufferPool.allocate(1, 0);
}
Also used : MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) Meter(org.apache.kafka.common.metrics.stats.Meter) Sensor(org.apache.kafka.common.metrics.Sensor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with Meter

use of org.apache.kafka.common.metrics.stats.Meter in project apache-kafka-on-k8s by banzaicloud.

the class SensorTest method testExpiredSensor.

@Test
public void testExpiredSensor() {
    MetricConfig config = new MetricConfig();
    Time mockTime = new MockTime();
    Metrics metrics = new Metrics(config, Arrays.asList((MetricsReporter) new JmxReporter()), mockTime, true);
    long inactiveSensorExpirationTimeSeconds = 60L;
    Sensor sensor = new Sensor(metrics, "sensor", null, config, mockTime, inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel.INFO);
    assertTrue(sensor.add(metrics.metricName("test1", "grp1"), new Avg()));
    Map<String, String> emptyTags = Collections.emptyMap();
    MetricName rateMetricName = new MetricName("rate", "test", "", emptyTags);
    MetricName totalMetricName = new MetricName("total", "test", "", emptyTags);
    Meter meter = new Meter(rateMetricName, totalMetricName);
    assertTrue(sensor.add(meter));
    mockTime.sleep(TimeUnit.SECONDS.toMillis(inactiveSensorExpirationTimeSeconds + 1));
    assertFalse(sensor.add(metrics.metricName("test3", "grp1"), new Avg()));
    assertFalse(sensor.add(meter));
    metrics.close();
}
Also used : Meter(org.apache.kafka.common.metrics.stats.Meter) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) SystemTime(org.apache.kafka.common.utils.SystemTime) MetricName(org.apache.kafka.common.MetricName) Avg(org.apache.kafka.common.metrics.stats.Avg) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 3 with Meter

use of org.apache.kafka.common.metrics.stats.Meter in project apache-kafka-on-k8s by banzaicloud.

the class RecordAccumulator method registerMetrics.

private void registerMetrics(Metrics metrics, String metricGrpName) {
    MetricName metricName = metrics.metricName("waiting-threads", metricGrpName, "The number of user threads blocked waiting for buffer memory to enqueue their records");
    Measurable waitingThreads = new Measurable() {

        public double measure(MetricConfig config, long now) {
            return free.queued();
        }
    };
    metrics.addMetric(metricName, waitingThreads);
    metricName = metrics.metricName("buffer-total-bytes", metricGrpName, "The maximum amount of buffer memory the client can use (whether or not it is currently used).");
    Measurable totalBytes = new Measurable() {

        public double measure(MetricConfig config, long now) {
            return free.totalMemory();
        }
    };
    metrics.addMetric(metricName, totalBytes);
    metricName = metrics.metricName("buffer-available-bytes", metricGrpName, "The total amount of buffer memory that is not being used (either unallocated or in the free list).");
    Measurable availableBytes = new Measurable() {

        public double measure(MetricConfig config, long now) {
            return free.availableMemory();
        }
    };
    metrics.addMetric(metricName, availableBytes);
    Sensor bufferExhaustedRecordSensor = metrics.sensor("buffer-exhausted-records");
    MetricName rateMetricName = metrics.metricName("buffer-exhausted-rate", metricGrpName, "The average per-second number of record sends that are dropped due to buffer exhaustion");
    MetricName totalMetricName = metrics.metricName("buffer-exhausted-total", metricGrpName, "The total number of record sends that are dropped due to buffer exhaustion");
    bufferExhaustedRecordSensor.add(new Meter(rateMetricName, totalMetricName));
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Measurable(org.apache.kafka.common.metrics.Measurable) Meter(org.apache.kafka.common.metrics.stats.Meter) Sensor(org.apache.kafka.common.metrics.Sensor)

Example 4 with Meter

use of org.apache.kafka.common.metrics.stats.Meter 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 5 with Meter

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

the class SensorTest method testExpiredSensor.

@Test
public void testExpiredSensor() {
    MetricConfig config = new MetricConfig();
    Time mockTime = new MockTime();
    try (Metrics metrics = new Metrics(config, Arrays.asList(new JmxReporter()), mockTime, true)) {
        long inactiveSensorExpirationTimeSeconds = 60L;
        Sensor sensor = new Sensor(metrics, "sensor", null, config, mockTime, inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel.INFO);
        assertTrue(sensor.add(metrics.metricName("test1", "grp1"), new Avg()));
        Map<String, String> emptyTags = Collections.emptyMap();
        MetricName rateMetricName = new MetricName("rate", "test", "", emptyTags);
        MetricName totalMetricName = new MetricName("total", "test", "", emptyTags);
        Meter meter = new Meter(rateMetricName, totalMetricName);
        assertTrue(sensor.add(meter));
        mockTime.sleep(TimeUnit.SECONDS.toMillis(inactiveSensorExpirationTimeSeconds + 1));
        assertFalse(sensor.add(metrics.metricName("test3", "grp1"), new Avg()));
        assertFalse(sensor.add(meter));
    }
}
Also used : MetricName(org.apache.kafka.common.MetricName) Avg(org.apache.kafka.common.metrics.stats.Avg) Meter(org.apache.kafka.common.metrics.stats.Meter) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) SystemTime(org.apache.kafka.common.utils.SystemTime) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Aggregations

Meter (org.apache.kafka.common.metrics.stats.Meter)9 MetricName (org.apache.kafka.common.MetricName)7 Avg (org.apache.kafka.common.metrics.stats.Avg)4 Test (org.junit.Test)4 Sensor (org.apache.kafka.common.metrics.Sensor)2 Count (org.apache.kafka.common.metrics.stats.Count)2 Max (org.apache.kafka.common.metrics.stats.Max)2 Min (org.apache.kafka.common.metrics.stats.Min)2 Percentile (org.apache.kafka.common.metrics.stats.Percentile)2 Percentiles (org.apache.kafka.common.metrics.stats.Percentiles)2 Rate (org.apache.kafka.common.metrics.stats.Rate)2 SimpleRate (org.apache.kafka.common.metrics.stats.SimpleRate)2 WindowedCount (org.apache.kafka.common.metrics.stats.WindowedCount)2 MockTime (org.apache.kafka.common.utils.MockTime)2 SystemTime (org.apache.kafka.common.utils.SystemTime)2 Time (org.apache.kafka.common.utils.Time)2 Test (org.junit.jupiter.api.Test)2 Measurable (org.apache.kafka.common.metrics.Measurable)1 MetricConfig (org.apache.kafka.common.metrics.MetricConfig)1 Metrics (org.apache.kafka.common.metrics.Metrics)1