All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.instrumentation.kafka.internal.TracingIterable Maven / Gradle / Ivy

There is a newer version: 2.9.0-alpha
Show newest version
/*
 * 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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy