io.opentelemetry.instrumentation.kafka.internal.KafkaProducerRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-kafka-clients-common Show documentation
Show all versions of opentelemetry-kafka-clients-common Show documentation
Instrumentation of Java libraries using OpenTelemetry.
The newest version!
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.kafka.internal;
import java.util.Iterator;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class KafkaProducerRequest {
private final ProducerRecord, ?> record;
@Nullable private final String clientId;
public static KafkaProducerRequest create(ProducerRecord, ?> record, Producer, ?> producer) {
return create(record, extractClientId(producer));
}
public static KafkaProducerRequest create(ProducerRecord, ?> record, String clientId) {
return new KafkaProducerRequest(record, clientId);
}
private KafkaProducerRequest(ProducerRecord, ?> record, String clientId) {
this.record = record;
this.clientId = clientId;
}
public ProducerRecord, ?> getRecord() {
return record;
}
public String getClientId() {
return clientId;
}
private static String extractClientId(Producer, ?> producer) {
try {
Map metrics = producer.metrics();
Iterator metricIterator = metrics.keySet().iterator();
return metricIterator.hasNext() ? metricIterator.next().tags().get("client-id") : null;
} catch (RuntimeException exception) {
// ExceptionHandlingTest uses a Producer that throws exception on every method call
return null;
}
}
}