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);
}
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();
}
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));
}
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");
}
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));
}
}
Aggregations