Search in sources :

Example 11 with Metric

use of org.apache.kafka.common.Metric in project kafka by apache.

the class StreamTaskTest method shouldRecordE2ELatencyOnSourceNodeAndTerminalNodes.

@Test
public void shouldRecordE2ELatencyOnSourceNodeAndTerminalNodes() {
    time = new MockTime(0L, 0L, 0L);
    metrics = new Metrics(new MetricConfig().recordLevel(Sensor.RecordingLevel.INFO), time);
    // Create a processor that only forwards even keys to test the metrics at the source and terminal nodes
    final MockSourceNode<Integer, Integer> evenKeyForwardingSourceNode = new MockSourceNode<Integer, Integer>(intDeserializer, intDeserializer) {

        InternalProcessorContext<Integer, Integer> context;

        @Override
        public void init(final InternalProcessorContext<Integer, Integer> context) {
            this.context = context;
            super.init(context);
        }

        @Override
        public void process(final Record<Integer, Integer> record) {
            if (record.key() % 2 == 0) {
                context.forward(record);
            }
        }
    };
    task = createStatelessTaskWithForwardingTopology(evenKeyForwardingSourceNode);
    task.initializeIfNeeded();
    task.completeRestoration(noOpResetter -> {
    });
    final String sourceNodeName = evenKeyForwardingSourceNode.name();
    final String terminalNodeName = processorStreamTime.name();
    final Metric sourceAvg = getProcessorMetric("record-e2e-latency", "%s-avg", task.id().toString(), sourceNodeName, StreamsConfig.METRICS_LATEST);
    final Metric sourceMin = getProcessorMetric("record-e2e-latency", "%s-min", task.id().toString(), sourceNodeName, StreamsConfig.METRICS_LATEST);
    final Metric sourceMax = getProcessorMetric("record-e2e-latency", "%s-max", task.id().toString(), sourceNodeName, StreamsConfig.METRICS_LATEST);
    final Metric terminalAvg = getProcessorMetric("record-e2e-latency", "%s-avg", task.id().toString(), terminalNodeName, StreamsConfig.METRICS_LATEST);
    final Metric terminalMin = getProcessorMetric("record-e2e-latency", "%s-min", task.id().toString(), terminalNodeName, StreamsConfig.METRICS_LATEST);
    final Metric terminalMax = getProcessorMetric("record-e2e-latency", "%s-max", task.id().toString(), terminalNodeName, StreamsConfig.METRICS_LATEST);
    // e2e latency = 10
    task.addRecords(partition1, singletonList(getConsumerRecordWithOffsetAsTimestamp(0, 0L)));
    task.process(10L);
    assertThat(sourceAvg.metricValue(), equalTo(10.0));
    assertThat(sourceMin.metricValue(), equalTo(10.0));
    assertThat(sourceMax.metricValue(), equalTo(10.0));
    // key 0: reaches terminal node
    assertThat(terminalAvg.metricValue(), equalTo(10.0));
    assertThat(terminalMin.metricValue(), equalTo(10.0));
    assertThat(terminalMax.metricValue(), equalTo(10.0));
    // e2e latency = 15
    task.addRecords(partition1, singletonList(getConsumerRecordWithOffsetAsTimestamp(1, 0L)));
    task.process(15L);
    assertThat(sourceAvg.metricValue(), equalTo(12.5));
    assertThat(sourceMin.metricValue(), equalTo(10.0));
    assertThat(sourceMax.metricValue(), equalTo(15.0));
    // key 1: stops at source, doesn't affect terminal node metrics
    assertThat(terminalAvg.metricValue(), equalTo(10.0));
    assertThat(terminalMin.metricValue(), equalTo(10.0));
    assertThat(terminalMax.metricValue(), equalTo(10.0));
    // e2e latency = 23
    task.addRecords(partition1, singletonList(getConsumerRecordWithOffsetAsTimestamp(2, 0L)));
    task.process(23L);
    assertThat(sourceAvg.metricValue(), equalTo(16.0));
    assertThat(sourceMin.metricValue(), equalTo(10.0));
    assertThat(sourceMax.metricValue(), equalTo(23.0));
    // key 2: reaches terminal node
    assertThat(terminalAvg.metricValue(), equalTo(16.5));
    assertThat(terminalMin.metricValue(), equalTo(10.0));
    assertThat(terminalMax.metricValue(), equalTo(23.0));
    // e2e latency = 5
    task.addRecords(partition1, singletonList(getConsumerRecordWithOffsetAsTimestamp(3, 0L)));
    task.process(5L);
    assertThat(sourceAvg.metricValue(), equalTo(13.25));
    assertThat(sourceMin.metricValue(), equalTo(5.0));
    assertThat(sourceMax.metricValue(), equalTo(23.0));
    // key 3: stops at source, doesn't affect terminal node metrics
    assertThat(terminalAvg.metricValue(), equalTo(16.5));
    assertThat(terminalMin.metricValue(), equalTo(10.0));
    assertThat(terminalMax.metricValue(), equalTo(23.0));
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) Metrics(org.apache.kafka.common.metrics.Metrics) MockSourceNode(org.apache.kafka.test.MockSourceNode) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Record(org.apache.kafka.streams.processor.api.Record) Metric(org.apache.kafka.common.Metric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 12 with Metric

use of org.apache.kafka.common.Metric in project kafka by apache.

the class MetricsIntegrationTest method checkMetricByName.

private void checkMetricByName(final List<Metric> listMetric, final String metricName, final int numMetric) {
    final List<Metric> metrics = listMetric.stream().filter(m -> m.metricName().name().equals(metricName)).collect(Collectors.toList());
    Assert.assertEquals("Size of metrics of type:'" + metricName + "' must be equal to " + numMetric + " but it's equal to " + metrics.size(), numMetric, metrics.size());
    for (final Metric m : metrics) {
        Assert.assertNotNull("Metric:'" + m.metricName() + "' must be not null", m.metricValue());
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) StreamsConfig(org.apache.kafka.streams.StreamsConfig) MockTime(org.apache.kafka.common.utils.MockTime) BeforeClass(org.junit.BeforeClass) Produced(org.apache.kafka.streams.kstream.Produced) SessionWindows(org.apache.kafka.streams.kstream.SessionWindows) Stores(org.apache.kafka.streams.state.Stores) IntegrationTest(org.apache.kafka.test.IntegrationTest) WindowStore(org.apache.kafka.streams.state.WindowStore) ArrayList(java.util.ArrayList) IntegrationTestUtils.safeUniqueTestName(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) TestName(org.junit.rules.TestName) After(org.junit.After) Duration(java.time.Duration) Metric(org.apache.kafka.common.Metric) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) SessionStore(org.apache.kafka.streams.state.SessionStore) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) BufferConfig(org.apache.kafka.streams.kstream.Suppressed.BufferConfig) Before(org.junit.Before) Sensor(org.apache.kafka.common.metrics.Sensor) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) TestUtils(org.apache.kafka.test.TestUtils) Consumed(org.apache.kafka.streams.kstream.Consumed) KeyValue(org.apache.kafka.streams.KeyValue) Suppressed(org.apache.kafka.streams.kstream.Suppressed) Test(org.junit.Test) IOException(java.io.IOException) State(org.apache.kafka.streams.KafkaStreams.State) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) Bytes(org.apache.kafka.common.utils.Bytes) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) List(java.util.List) Rule(org.junit.Rule) TimeWindows(org.apache.kafka.streams.kstream.TimeWindows) Materialized(org.apache.kafka.streams.kstream.Materialized) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Assert(org.junit.Assert) Collections(java.util.Collections) Topology(org.apache.kafka.streams.Topology) Metric(org.apache.kafka.common.Metric)

Example 13 with Metric

use of org.apache.kafka.common.Metric in project kafka by apache.

the class ActiveTaskCreatorTest method addMetric.

private void addMetric(final MockProducer<?, ?> producer, final String name, final double value) {
    final MetricName metricName = metricName(name);
    producer.setMockMetrics(metricName, new Metric() {

        @Override
        public MetricName metricName() {
            return metricName;
        }

        @Override
        public Object metricValue() {
            return value;
        }
    });
}
Also used : MetricName(org.apache.kafka.common.MetricName) Metric(org.apache.kafka.common.Metric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric)

Example 14 with Metric

use of org.apache.kafka.common.Metric 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)

Example 15 with Metric

use of org.apache.kafka.common.Metric in project kafka by apache.

the class ActiveTaskCreatorTest method shouldConstructThreadProducerMetric.

private void shouldConstructThreadProducerMetric() {
    createTasks();
    final MetricName testMetricName = new MetricName("test_metric", "", "", new HashMap<>());
    final Metric testMetric = new KafkaMetric(new Object(), testMetricName, (Measurable) (config, now) -> 0, null, new MockTime());
    mockClientSupplier.producers.get(0).setMockMetrics(testMetricName, testMetric);
    assertThat(mockClientSupplier.producers.size(), is(1));
    final Map<MetricName, Metric> producerMetrics = activeTaskCreator.producerMetrics();
    assertThat(producerMetrics.size(), is(1));
    assertThat(producerMetrics.get(testMetricName), is(testMetric));
}
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

Metric (org.apache.kafka.common.Metric)42 MetricName (org.apache.kafka.common.MetricName)24 Test (org.junit.Test)18 MockTime (org.apache.kafka.common.utils.MockTime)14 StreamsConfig (org.apache.kafka.streams.StreamsConfig)13 Map (java.util.Map)11 Test (org.junit.jupiter.api.Test)11 Metrics (org.apache.kafka.common.metrics.Metrics)10 Collections (java.util.Collections)9 HashMap (java.util.HashMap)9 Properties (java.util.Properties)9 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)9 StreamsMetricsImpl (org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)9 ArrayList (java.util.ArrayList)8 TaskId (org.apache.kafka.streams.processor.TaskId)8 List (java.util.List)7 TopicPartition (org.apache.kafka.common.TopicPartition)7 StreamsException (org.apache.kafka.streams.errors.StreamsException)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Utils.mkMap (org.apache.kafka.common.utils.Utils.mkMap)6