Apache Kafka Metrics

Apache Kafka is an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications.

Below you can find an example of how to instrument Apache Kafka with Micrometer.

Setting up instrumentation with Apache Kafka on the consumer side.

// Setting up and binding instrumentation
Properties consumerConfigs = new Properties();
consumerConfigs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());
consumerConfigs.put(ConsumerConfig.GROUP_ID_CONFIG, "test");
Consumer<String, String> consumer = new KafkaConsumer<>(consumerConfigs, new StringDeserializer(),
        new StringDeserializer());

KafkaClientMetrics consumerKafkaMetrics = new KafkaClientMetrics(consumer);
consumerKafkaMetrics.bindTo(registry);

Setting up instrumentation with Apache Kafka on the producer side.

// Setting up and binding instrumentation
Properties producerConfigs = new Properties();
producerConfigs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());
Producer<String, String> producer = new KafkaProducer<>(producerConfigs, new StringSerializer(),
        new StringSerializer());

KafkaClientMetrics producerKafkaMetrics = new KafkaClientMetrics(producer);
producerKafkaMetrics.bindTo(registry);

Setting up instrumentation with Apache Kafka using Kafka Streams.

// Setting up, binding instrumentation and usage example
try (KafkaStreams kafkaStreams = createStreams()) {
    metrics = new KafkaStreamsMetrics(kafkaStreams);
    MeterRegistry registry = new SimpleMeterRegistry();

    metrics.bindTo(registry);
    assertThat(registry.getMeters()).hasSizeGreaterThan(0)
        .extracting(meter -> meter.getId().getName())
        .allMatch(s -> s.startsWith(METRIC_NAME_PREFIX));
}