|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.jmxscraper.target_systems.kafka; |
| 7 | + |
| 8 | +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute; |
| 9 | +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup; |
| 10 | +import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeWithAnyValue; |
| 11 | +import static io.opentelemetry.contrib.jmxscraper.target_systems.kafka.KafkaContainerFactory.createKafkaConsumerContainer; |
| 12 | +import static io.opentelemetry.contrib.jmxscraper.target_systems.kafka.KafkaContainerFactory.createKafkaContainer; |
| 13 | +import static io.opentelemetry.contrib.jmxscraper.target_systems.kafka.KafkaContainerFactory.createKafkaProducerContainer; |
| 14 | +import static io.opentelemetry.contrib.jmxscraper.target_systems.kafka.KafkaContainerFactory.createZookeeperContainer; |
| 15 | + |
| 16 | +import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer; |
| 17 | +import io.opentelemetry.contrib.jmxscraper.target_systems.MetricsVerifier; |
| 18 | +import io.opentelemetry.contrib.jmxscraper.target_systems.TargetSystemIntegrationTest; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collection; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.testcontainers.containers.GenericContainer; |
| 24 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 25 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 26 | + |
| 27 | +public class KafkaConsumerIntegrationTest extends TargetSystemIntegrationTest { |
| 28 | + |
| 29 | + @Override |
| 30 | + protected Collection<GenericContainer<?>> createPrerequisiteContainers() { |
| 31 | + GenericContainer<?> zookeeper = |
| 32 | + createZookeeperContainer() |
| 33 | + .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("zookeeper"))) |
| 34 | + .withNetworkAliases("zookeeper"); |
| 35 | + |
| 36 | + GenericContainer<?> kafka = |
| 37 | + createKafkaContainer() |
| 38 | + .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("kafka"))) |
| 39 | + .withNetworkAliases("kafka") |
| 40 | + .dependsOn(zookeeper); |
| 41 | + |
| 42 | + GenericContainer<?> kafkaProducer = |
| 43 | + createKafkaProducerContainer() |
| 44 | + .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("kafka-producer"))) |
| 45 | + .withNetworkAliases("kafka-producer") |
| 46 | + .dependsOn(kafka); |
| 47 | + |
| 48 | + return Arrays.asList(zookeeper, kafka, kafkaProducer); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected GenericContainer<?> createTargetContainer(int jmxPort) { |
| 53 | + return createKafkaConsumerContainer() |
| 54 | + .withEnv("JMX_PORT", Integer.toString(jmxPort)) |
| 55 | + .withExposedPorts(jmxPort) |
| 56 | + .waitingFor(Wait.forListeningPorts(jmxPort)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected JmxScraperContainer customizeScraperContainer( |
| 61 | + JmxScraperContainer scraper, GenericContainer<?> target, Path tempDir) { |
| 62 | + return scraper.withTargetSystem("kafka-consumer"); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected MetricsVerifier createMetricsVerifier() { |
| 67 | + return MetricsVerifier.create() |
| 68 | + .add( |
| 69 | + "kafka.consumer.fetch-rate", |
| 70 | + metric -> |
| 71 | + metric |
| 72 | + .hasDescription("The number of fetch requests for all topics per second") |
| 73 | + .hasUnit("{request}") |
| 74 | + .isGauge() |
| 75 | + .hasDataPointsWithOneAttribute( |
| 76 | + attributeWithAnyValue("client.id"))) // changed to follow semconv |
| 77 | + .add( |
| 78 | + "kafka.consumer.records-lag-max", |
| 79 | + metric -> |
| 80 | + metric |
| 81 | + .hasDescription("Number of messages the consumer lags behind the producer") |
| 82 | + .hasUnit("{message}") |
| 83 | + .isGauge() |
| 84 | + .hasDataPointsWithOneAttribute(attributeWithAnyValue("client.id"))) |
| 85 | + .add( |
| 86 | + "kafka.consumer.total.bytes-consumed-rate", |
| 87 | + metric -> |
| 88 | + metric |
| 89 | + .hasDescription( |
| 90 | + "The average number of bytes consumed for all topics per second") |
| 91 | + .hasUnit("By") |
| 92 | + .isGauge() |
| 93 | + .hasDataPointsWithOneAttribute(attributeWithAnyValue("client.id"))) |
| 94 | + .add( |
| 95 | + "kafka.consumer.total.fetch-size-avg", |
| 96 | + metric -> |
| 97 | + metric |
| 98 | + .hasDescription( |
| 99 | + "The average number of bytes fetched per request for all topics") |
| 100 | + .hasUnit("By") |
| 101 | + .isGauge() |
| 102 | + .hasDataPointsWithOneAttribute(attributeWithAnyValue("client.id"))) |
| 103 | + .add( |
| 104 | + "kafka.consumer.total.records-consumed-rate", |
| 105 | + metric -> |
| 106 | + metric |
| 107 | + .hasDescription( |
| 108 | + "The average number of records consumed for all topics per second") |
| 109 | + .hasUnit("{record}") |
| 110 | + .isGauge() |
| 111 | + .hasDataPointsWithOneAttribute(attributeWithAnyValue("client.id"))) |
| 112 | + .add( |
| 113 | + "kafka.consumer.bytes-consumed-rate", |
| 114 | + metric -> |
| 115 | + metric |
| 116 | + .hasDescription("The average number of bytes consumed per second") |
| 117 | + .hasUnit("By") |
| 118 | + .isGauge() |
| 119 | + .hasDataPointsWithAttributes( |
| 120 | + attributeGroup( |
| 121 | + attributeWithAnyValue("client.id"), |
| 122 | + attribute("topic", "test-topic-1")))) |
| 123 | + .add( |
| 124 | + "kafka.consumer.fetch-size-avg", |
| 125 | + metric -> |
| 126 | + metric |
| 127 | + .hasDescription("The average number of bytes fetched per request") |
| 128 | + .hasUnit("By") |
| 129 | + .isGauge() |
| 130 | + .hasDataPointsWithAttributes( |
| 131 | + attributeGroup( |
| 132 | + attributeWithAnyValue("client.id"), |
| 133 | + attribute("topic", "test-topic-1")))) |
| 134 | + .add( |
| 135 | + "kafka.consumer.records-consumed-rate", |
| 136 | + metric -> |
| 137 | + metric |
| 138 | + .hasDescription("The average number of records consumed per second") |
| 139 | + .hasUnit("{record}") |
| 140 | + .isGauge() |
| 141 | + .hasDataPointsWithAttributes( |
| 142 | + attributeGroup( |
| 143 | + attributeWithAnyValue("client.id"), |
| 144 | + attribute("topic", "test-topic-1")))); |
| 145 | + } |
| 146 | +} |
0 commit comments