content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.kafkastreams;
  
  import static net.bytebuddy.matcher.ElementMatchers.named;
  
  import io.opentelemetry.javaagent.bootstrap.kafka.KafkaClientsConsumerProcessTracing;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
  import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
  import net.bytebuddy.asm.Advice;
  import net.bytebuddy.description.type.TypeDescription;
  import net.bytebuddy.matcher.ElementMatcher;
  
  public class StreamThreadInstrumentation implements TypeInstrumentation {
  
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("org.apache.kafka.streams.processor.internals.StreamThread");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(named("runLoop"), this.getClass().getName() + "$RunLoopAdvice");
    }
  
    // this advice suppresses the CONSUMER spans created by the kafka-clients instrumentation
    @SuppressWarnings("unused")
    public static class RunLoopAdvice {
      @Advice.OnMethodEnter(suppress = Throwable.class)
      public static boolean onEnter() {
        return KafkaClientsConsumerProcessTracing.setEnabled(false);
      }
  
      @Advice.OnMethodExit(suppress = Throwable.class)
      public static void onExit(@Advice.Enter boolean previousValue) {
        KafkaClientsConsumerProcessTracing.setEnabled(previousValue);
      }
    }
  }
  