Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kafka client metrics #6138

Merged
merged 17 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package io.opentelemetry.instrumentation.kafkaclients

import io.opentelemetry.instrumentation.test.InstrumentationSpecification
import org.testcontainers.utility.DockerImageName

import java.time.Duration
import org.apache.kafka.clients.admin.AdminClient
import org.apache.kafka.clients.admin.NewTopic
Expand Down Expand Up @@ -46,7 +48,7 @@ abstract class KafkaClientBaseTest extends InstrumentationSpecification {
static TopicPartition topicPartition = new TopicPartition(SHARED_TOPIC, 0)

def setupSpec() {
kafka = new KafkaContainer()
kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.4.3"))
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
.withLogConsumer(new Slf4jLogConsumer(logger))
.waitingFor(Wait.forLogMessage(".*started \\(kafka.server.KafkaServer\\).*", 1))
.withStartupTimeout(Duration.ofMinutes(1))
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ dependencies {
testImplementation("com.fasterxml.jackson.core:jackson-databind:2.10.2")

testImplementation("org.testcontainers:kafka")
testImplementation("org.testcontainers:junit-jupiter")

testCompileOnly("com.google.auto.value:auto-value-annotations")
testAnnotationProcessor("com.google.auto.value:auto-value")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.kafka.internal.KafkaConsumerRecordGetter;
import io.opentelemetry.instrumentation.kafka.internal.KafkaHeadersSetter;
import io.opentelemetry.instrumentation.kafka.internal.OpenTelemetryMetricsReporter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.function.BiFunction;
import java.util.logging.Logger;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.metrics.MetricsReporter;

public final class KafkaTelemetry {
private static final Logger logger = Logger.getLogger(KafkaTelemetry.class.getName());
Expand Down Expand Up @@ -76,6 +84,41 @@ public <K, V> Consumer<K, V> wrap(Consumer<K, V> consumer) {
return new TracingConsumer<>(consumer, this);
}

/**
* Produces a set of kafka client config properties (consumer or producer) to register a {@link
* MetricsReporter} that records metrics to an {@code openTelemetry} instance. Add these resulting
* properties to the configuration map used to initialize a {@link KafkaConsumer} or {@link
* KafkaProducer}.
*
* <p>For producers:
*
* <pre>{@code
* // Map<String, Object> config = new HashMap<>();
* // config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, ...);
* // config.putAll(kafkaTelemetry.metricConfigProperties());
* // try (KafkaProducer<?, ?> producer = new KafkaProducer<>(config)) { ... }
* }</pre>
*
* <p>For consumers:
*
* <pre>{@code
* // Map<String, Object> config = new HashMap<>();
* // config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, ...);
* // config.putAll(kafkaTelemetry.metricConfigProperties());
* // try (KafkaConsumer<?, ?> consumer = new KafkaConsumer<>(config)) { ... }
* }</pre>
*
* @return the kafka client properties
*/
public Map<String, ?> metricConfigProperties() {
Map<String, Object> config = new HashMap<>();
config.put(
CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
OpenTelemetryMetricsReporter.class.getName());
config.put(OpenTelemetryMetricsReporter.CONFIG_KEY_OPENTELEMETRY_INSTANCE, openTelemetry);
return Collections.unmodifiableMap(config);
}

/**
* Build and inject span into record.
*
Expand Down
Loading