io.opentelemetry.instrumentation.kafka.internal.TracingIterable 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.
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.kafka.internal;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import java.util.Iterator;
import java.util.function.BooleanSupplier;
import org.apache.kafka.clients.consumer.ConsumerRecord;
/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public class TracingIterable implements Iterable> {
private final Iterable> delegate;
private final Instrumenter instrumenter;
private final BooleanSupplier wrappingEnabled;
private final KafkaConsumerContext consumerContext;
private boolean firstIterator = true;
protected TracingIterable(
Iterable> delegate,
Instrumenter instrumenter,
BooleanSupplier wrappingEnabled,
KafkaConsumerContext consumerContext) {
this.delegate = delegate;
this.instrumenter = instrumenter;
this.wrappingEnabled = wrappingEnabled;
this.consumerContext = consumerContext;
}
public static Iterable> wrap(
Iterable> delegate,
Instrumenter instrumenter,
BooleanSupplier wrappingEnabled,
KafkaConsumerContext consumerContext) {
if (wrappingEnabled.getAsBoolean()) {
return new TracingIterable<>(delegate, instrumenter, wrappingEnabled, consumerContext);
}
return delegate;
}
@Override
public Iterator> iterator() {
Iterator> it;
// We should only return one iterator with tracing.
// However, this is not thread-safe, but usually the first (hopefully only) traversal of
// ConsumerRecords is performed in the same thread that called poll()
if (firstIterator) {
it =
TracingIterator.wrap(delegate.iterator(), instrumenter, wrappingEnabled, consumerContext);
firstIterator = false;
} else {
it = delegate.iterator();
}
return it;
}
}