Search in sources :

Example 31 with KafkaMetric

use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.

the class MeteredTimestampedKeyValueStoreTest method shouldPutIfAbsentAndRecordPutIfAbsentMetric.

@Test
public void shouldPutIfAbsentAndRecordPutIfAbsentMetric() {
    expect(inner.putIfAbsent(eq(KEY_BYTES), aryEq(VALUE_AND_TIMESTAMP_BYTES))).andReturn(null);
    init();
    metered.putIfAbsent(KEY, VALUE_AND_TIMESTAMP);
    final KafkaMetric metric = metric("put-if-absent-rate");
    assertTrue((Double) metric.metricValue() > 0);
    verify(inner);
}
Also used : KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Test(org.junit.Test)

Example 32 with KafkaMetric

use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.

the class MeteredTimestampedKeyValueStoreTest method shouldPutAllToInnerStoreAndRecordPutAllMetric.

@SuppressWarnings("unchecked")
@Test
public void shouldPutAllToInnerStoreAndRecordPutAllMetric() {
    inner.putAll(anyObject(List.class));
    expectLastCall();
    init();
    metered.putAll(Collections.singletonList(KeyValue.pair(KEY, VALUE_AND_TIMESTAMP)));
    final KafkaMetric metric = metric("put-all-rate");
    assertTrue((Double) metric.metricValue() > 0);
    verify(inner);
}
Also used : List(java.util.List) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Test(org.junit.Test)

Example 33 with KafkaMetric

use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.

the class StreamsMetricsImplTest method testTotalMetricDoesntDecrease.

@Test
public void testTotalMetricDoesntDecrease() {
    final MockTime time = new MockTime(1);
    final MetricConfig config = new MetricConfig().timeWindow(1, TimeUnit.MILLISECONDS);
    final Metrics metrics = new Metrics(config, time);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, "", VERSION, time);
    final String scope = "scope";
    final String entity = "entity";
    final String operation = "op";
    final Sensor sensor = streamsMetrics.addLatencyRateTotalSensor(scope, entity, operation, RecordingLevel.INFO);
    final double latency = 100.0;
    final MetricName totalMetricName = metrics.metricName("op-total", "stream-scope-metrics", "", "thread-id", Thread.currentThread().getName(), "scope-id", "entity");
    final KafkaMetric totalMetric = metrics.metric(totalMetricName);
    for (int i = 0; i < 10; i++) {
        assertEquals(i, Math.round(totalMetric.measurable().measure(config, time.milliseconds())));
        sensor.record(latency, time.milliseconds());
    }
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) EasyMock.anyString(org.easymock.EasyMock.anyString) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) MockTime(org.apache.kafka.common.utils.MockTime) Sensor(org.apache.kafka.common.metrics.Sensor) StreamsMetricsImpl.addInvocationRateAndCountToSensor(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.addInvocationRateAndCountToSensor) StreamsMetricsImpl.addAvgAndMaxLatencyToSensor(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.addAvgAndMaxLatencyToSensor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with KafkaMetric

use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.

the class StreamTaskTest method verifyCloseTaskMetric.

private void verifyCloseTaskMetric(final double expected, final StreamsMetricsImpl streamsMetrics, final MetricName metricName) {
    final KafkaMetric metric = (KafkaMetric) streamsMetrics.metrics().get(metricName);
    final double totalCloses = metric.measurable().measure(metric.config(), System.currentTimeMillis());
    assertThat(totalCloses, equalTo(expected));
}
Also used : KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric)

Example 35 with KafkaMetric

use of org.apache.kafka.common.metrics.KafkaMetric in project kafka by apache.

the class ActiveTaskCreatorTest method shouldConstructProducerMetricsPerTask.

private void shouldConstructProducerMetricsPerTask() {
    mockClientSupplier.setApplicationIdForProducer("appId");
    createTasks();
    final MetricName testMetricName1 = new MetricName("test_metric_1", "", "", new HashMap<>());
    final Metric testMetric1 = new KafkaMetric(new Object(), testMetricName1, (Measurable) (config, now) -> 0, null, new MockTime());
    mockClientSupplier.producers.get(0).setMockMetrics(testMetricName1, testMetric1);
    final MetricName testMetricName2 = new MetricName("test_metric_2", "", "", new HashMap<>());
    final Metric testMetric2 = new KafkaMetric(new Object(), testMetricName2, (Measurable) (config, now) -> 0, null, new MockTime());
    mockClientSupplier.producers.get(0).setMockMetrics(testMetricName2, testMetric2);
    final Map<MetricName, Metric> producerMetrics = activeTaskCreator.producerMetrics();
    assertThat(producerMetrics, is(mkMap(mkEntry(testMetricName1, testMetric1), mkEntry(testMetricName2, testMetric2))));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) StreamsConfig(org.apache.kafka.streams.StreamsConfig) MockTime(org.apache.kafka.common.utils.MockTime) TaskId(org.apache.kafka.streams.processor.TaskId) Mock(org.easymock.Mock) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) EasyMock.mock(org.easymock.EasyMock.mock) HashMap(java.util.HashMap) StreamsException(org.apache.kafka.streams.errors.StreamsException) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) EasyMock.reset(org.easymock.EasyMock.reset) Matchers.closeTo(org.hamcrest.Matchers.closeTo) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) EasyMockRunner(org.easymock.EasyMockRunner) EasyMock.replay(org.easymock.EasyMock.replay) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) IsNot.not(org.hamcrest.core.IsNot.not) TopicPartition(org.apache.kafka.common.TopicPartition) ThreadCache(org.apache.kafka.streams.state.internals.ThreadCache) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) Test(org.junit.Test) Measurable(org.apache.kafka.common.metrics.Measurable) UUID(java.util.UUID) EasyMock.expect(org.easymock.EasyMock.expect) Collectors(java.util.stream.Collectors) MockClientSupplier(org.apache.kafka.test.MockClientSupplier) File(java.io.File) TimestampExtractor(org.apache.kafka.streams.processor.TimestampExtractor) Metrics(org.apache.kafka.common.metrics.Metrics) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) TopologyConfig(org.apache.kafka.streams.processor.internals.namedtopology.TopologyConfig) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) MockType(org.easymock.MockType) Collections(java.util.Collections) MockProducer(org.apache.kafka.clients.producer.MockProducer) MetricName(org.apache.kafka.common.MetricName) Metric(org.apache.kafka.common.Metric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) MockTime(org.apache.kafka.common.utils.MockTime)

Aggregations

KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)95 Test (org.junit.Test)73 MetricName (org.apache.kafka.common.MetricName)36 HashMap (java.util.HashMap)17 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)14 LinkedHashMap (java.util.LinkedHashMap)12 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)12 Windowed (org.apache.kafka.streams.kstream.Windowed)12 Test (org.junit.jupiter.api.Test)11 TopicPartition (org.apache.kafka.common.TopicPartition)10 List (java.util.List)8 Cluster (org.apache.kafka.common.Cluster)8 Node (org.apache.kafka.common.Node)8 Sensor (org.apache.kafka.common.metrics.Sensor)8 LegacyRecord (org.apache.kafka.common.record.LegacyRecord)8 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)8 Record (org.apache.kafka.common.record.Record)8 SimpleRecord (org.apache.kafka.common.record.SimpleRecord)8 Metrics (org.apache.kafka.common.metrics.Metrics)7 PartitionData (org.apache.kafka.common.requests.FetchRequest.PartitionData)7