Search in sources :

Example 1 with Rate

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

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 = metrics.metricName("buffer-exhausted-rate", metricGrpName, "The average per-second number of record sends that are dropped due to buffer exhaustion");
    bufferExhaustedRecordSensor.add(metricName, new Rate());
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Measurable(org.apache.kafka.common.metrics.Measurable) Rate(org.apache.kafka.common.metrics.stats.Rate) Sensor(org.apache.kafka.common.metrics.Sensor)

Example 2 with Rate

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

the class MetricsTest method testSimpleStats.

@Test
public void testSimpleStats() throws Exception {
    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(metrics.metricName("test.rate", "grp1"), new Rate(TimeUnit.SECONDS));
    s.add(metrics.metricName("test.occurences", "grp1"), new Rate(TimeUnit.SECONDS, new Count()));
    s.add(metrics.metricName("test.count", "grp1"), new Count());
    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 Total());
    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(String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs), count / elapsedSecs, metrics.metrics().get(metrics.metricName("test.occurences", "grp1")).value(), EPS);
    // pretend 2 seconds passed...
    long sleepTimeMs = 2;
    time.sleep(sleepTimeMs * 1000);
    elapsedSecs += sleepTimeMs;
    assertEquals("s2 reflects the constant value", 5.0, metrics.metrics().get(metrics.metricName("s2.total", "grp1")).value(), EPS);
    assertEquals("Avg(0...9) = 4.5", 4.5, metrics.metrics().get(metrics.metricName("test.avg", "grp1")).value(), EPS);
    assertEquals("Max(0...9) = 9", count - 1, metrics.metrics().get(metrics.metricName("test.max", "grp1")).value(), EPS);
    assertEquals("Min(0...9) = 0", 0.0, metrics.metrics().get(metrics.metricName("test.min", "grp1")).value(), EPS);
    assertEquals("Rate(0...9) = 1.40625", sum / elapsedSecs, metrics.metrics().get(metrics.metricName("test.rate", "grp1")).value(), EPS);
    assertEquals(String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs), count / elapsedSecs, metrics.metrics().get(metrics.metricName("test.occurences", "grp1")).value(), EPS);
    assertEquals("Count(0...9) = 10", (double) count, metrics.metrics().get(metrics.metricName("test.count", "grp1")).value(), EPS);
}
Also used : Percentile(org.apache.kafka.common.metrics.stats.Percentile) Max(org.apache.kafka.common.metrics.stats.Max) Rate(org.apache.kafka.common.metrics.stats.Rate) SimpleRate(org.apache.kafka.common.metrics.stats.SimpleRate) Count(org.apache.kafka.common.metrics.stats.Count) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) Avg(org.apache.kafka.common.metrics.stats.Avg) Min(org.apache.kafka.common.metrics.stats.Min) Total(org.apache.kafka.common.metrics.stats.Total) Test(org.junit.Test)

Example 3 with Rate

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

the class MetricsTest method testRateWindowing.

@Test
public void testRateWindowing() throws Exception {
    // Use the default time window. Set 3 samples
    MetricConfig cfg = new MetricConfig().samples(3);
    Sensor s = metrics.sensor("test.sensor", cfg);
    s.add(metrics.metricName("test.rate", "grp1"), new Rate(TimeUnit.SECONDS));
    int sum = 0;
    int count = cfg.samples() - 1;
    // Advance 1 window after every record
    for (int i = 0; i < count; i++) {
        s.record(100);
        sum += 100;
        time.sleep(cfg.timeWindowMs());
    }
    // Sleep for half the window.
    time.sleep(cfg.timeWindowMs() / 2);
    // prior to any time passing
    double elapsedSecs = (cfg.timeWindowMs() * (cfg.samples() - 1) + cfg.timeWindowMs() / 2) / 1000.0;
    KafkaMetric km = metrics.metrics().get(metrics.metricName("test.rate", "grp1"));
    assertEquals("Rate(0...2) = 2.666", sum / elapsedSecs, km.value(), EPS);
    assertEquals("Elapsed Time = 75 seconds", elapsedSecs, ((Rate) km.measurable()).windowSize(cfg, time.milliseconds()) / 1000, EPS);
}
Also used : Rate(org.apache.kafka.common.metrics.stats.Rate) SimpleRate(org.apache.kafka.common.metrics.stats.SimpleRate) Test(org.junit.Test)

Aggregations

Rate (org.apache.kafka.common.metrics.stats.Rate)3 SimpleRate (org.apache.kafka.common.metrics.stats.SimpleRate)2 Test (org.junit.Test)2 MetricName (org.apache.kafka.common.MetricName)1 Measurable (org.apache.kafka.common.metrics.Measurable)1 MetricConfig (org.apache.kafka.common.metrics.MetricConfig)1 Sensor (org.apache.kafka.common.metrics.Sensor)1 Avg (org.apache.kafka.common.metrics.stats.Avg)1 Count (org.apache.kafka.common.metrics.stats.Count)1 Max (org.apache.kafka.common.metrics.stats.Max)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 Total (org.apache.kafka.common.metrics.stats.Total)1