content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.okhttp.v2_2;
  
  import static io.opentelemetry.javaagent.instrumentation.okhttp.v2_2.OkHttp2Singletons.tracingInterceptor;
  import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
  import static net.bytebuddy.matcher.ElementMatchers.named;
  
  import com.squareup.okhttp.Interceptor;
  import com.squareup.okhttp.OkHttpClient;
  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 OkHttpClientInstrumentation implements TypeInstrumentation {
    @Override
    public ElementMatcher<TypeDescription> typeMatcher() {
      return named("com.squareup.okhttp.OkHttpClient");
    }
  
    @Override
    public void transform(TypeTransformer transformer) {
      transformer.applyAdviceToMethod(
          isConstructor(), this.getClass().getName() + "$ConstructorAdvice");
    }
  
    @SuppressWarnings("unused")
    public static class ConstructorAdvice {
  
      @Advice.OnMethodExit
      public static void addTracingInterceptor(@Advice.This OkHttpClient client) {
        for (Interceptor interceptor : client.interceptors()) {
          if (interceptor instanceof TracingInterceptor) {
            return;
          }
        }
  
        client.interceptors().add(tracingInterceptor());
      }
    }
  }
  