use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.
the class FetcherTest method testFetcherMetrics.
/*
* Send multiple requests. Verify that the client side quota metrics have the right values
*/
@Test
public void testFetcherMetrics() {
buildFetcher();
assignFromUser(singleton(tp0));
subscriptions.seek(tp0, 0);
MetricName maxLagMetric = metrics.metricInstance(metricsRegistry.recordsLagMax);
Map<String, String> tags = new HashMap<>();
tags.put("topic", tp0.topic());
tags.put("partition", String.valueOf(tp0.partition()));
MetricName partitionLagMetric = metrics.metricName("records-lag", metricGroup, tags);
Map<MetricName, KafkaMetric> allMetrics = metrics.metrics();
KafkaMetric recordsFetchLagMax = allMetrics.get(maxLagMetric);
// recordsFetchLagMax should be initialized to NaN
assertEquals(Double.NaN, (Double) recordsFetchLagMax.metricValue(), EPSILON);
// recordsFetchLagMax should be hw - fetchOffset after receiving an empty FetchResponse
fetchRecords(tidp0, MemoryRecords.EMPTY, Errors.NONE, 100L, 0);
assertEquals(100, (Double) recordsFetchLagMax.metricValue(), EPSILON);
KafkaMetric partitionLag = allMetrics.get(partitionLagMetric);
assertEquals(100, (Double) partitionLag.metricValue(), EPSILON);
// recordsFetchLagMax should be hw - offset of the last message after receiving a non-empty FetchResponse
MemoryRecordsBuilder builder = MemoryRecords.builder(ByteBuffer.allocate(1024), CompressionType.NONE, TimestampType.CREATE_TIME, 0L);
for (int v = 0; v < 3; v++) builder.appendWithOffset(v, RecordBatch.NO_TIMESTAMP, "key".getBytes(), ("value-" + v).getBytes());
fetchRecords(tidp0, builder.build(), Errors.NONE, 200L, 0);
assertEquals(197, (Double) recordsFetchLagMax.metricValue(), EPSILON);
assertEquals(197, (Double) partitionLag.metricValue(), EPSILON);
// verify de-registration of partition lag
subscriptions.unsubscribe();
fetcher.sendFetches();
assertFalse(allMetrics.containsKey(partitionLagMetric));
}
use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.
the class StreamThreadTest method shouldConstructAdminMetrics.
@Test
public void shouldConstructAdminMetrics() {
final Node broker1 = new Node(0, "dummyHost-1", 1234);
final Node broker2 = new Node(1, "dummyHost-2", 1234);
final List<Node> cluster = Arrays.asList(broker1, broker2);
final MockAdminClient adminClient = new MockAdminClient.Builder().brokers(cluster).clusterId(null).build();
final Consumer<byte[], byte[]> consumer = EasyMock.createNiceMock(Consumer.class);
final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
expect(consumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
EasyMock.replay(consumer, consumerGroupMetadata);
final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, StreamsConfig.METRICS_LATEST, mockTime);
final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
topologyMetadata.buildAndRewriteTopology();
final StreamThread thread = new StreamThread(mockTime, config, adminClient, consumer, consumer, null, null, taskManager, streamsMetrics, topologyMetadata, CLIENT_ID, new LogContext(""), new AtomicInteger(), new AtomicLong(Long.MAX_VALUE), new LinkedList<>(), null, HANDLER, null);
final MetricName testMetricName = new MetricName("test_metric", "", "", new HashMap<>());
final Metric testMetric = new KafkaMetric(new Object(), testMetricName, (Measurable) (config, now) -> 0, null, new MockTime());
EasyMock.replay(taskManager);
adminClient.setMockMetrics(testMetricName, testMetric);
final Map<MetricName, Metric> adminClientMetrics = thread.adminClientMetrics();
assertEquals(testMetricName, adminClientMetrics.get(testMetricName).metricName());
}
use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.
the class StreamsMetricsImplTest method verifyMetric.
private void verifyMetric(final String name, final String description, final double valueToRecord1, final double valueToRecord2, final double expectedMetricValue) {
final KafkaMetric metric = metrics.metric(new MetricName(name, group, description, tags));
assertThat(metric, is(notNullValue()));
assertThat(metric.metricName().description(), equalTo(description));
sensor.record(valueToRecord1, time.milliseconds());
sensor.record(valueToRecord2, time.milliseconds());
assertThat(metric.measurable().measure(new MetricConfig(), time.milliseconds()), equalTo(expectedMetricValue));
}
use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.
the class MeteredKeyValueStoreTest method shouldPutIfAbsentAndRecordPutIfAbsentMetric.
@Test
public void shouldPutIfAbsentAndRecordPutIfAbsentMetric() {
expect(inner.putIfAbsent(eq(KEY_BYTES), aryEq(VALUE_BYTES))).andReturn(null);
init();
metered.putIfAbsent(KEY, VALUE);
final KafkaMetric metric = metric("put-if-absent-rate");
assertTrue((Double) metric.metricValue() > 0);
verify(inner);
}
use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.
the class MeteredKeyValueStoreTest method shouldPutAllToInnerStoreAndRecordPutAllMetric.
@SuppressWarnings("unchecked")
@Test
public void shouldPutAllToInnerStoreAndRecordPutAllMetric() {
inner.putAll(anyObject(List.class));
expectLastCall();
init();
metered.putAll(Collections.singletonList(KeyValue.pair(KEY, VALUE)));
final KafkaMetric metric = metric("put-all-rate");
assertTrue((Double) metric.metricValue() > 0);
verify(inner);
}
Aggregations