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

com.undefinedlabs.scope.rules.OkHttp3ClientScopeAgentRule Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument OkHttp3 library.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules;

import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.undefinedlabs.scope.logger.ScopeLogger;
import com.undefinedlabs.scope.logger.ScopeLoggerResolver;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import okhttp3.OkHttpClient;

public class OkHttp3ClientScopeAgentRule extends AbstractScopeAgentRule {

  public static final ScopeLogger LOGGER = ScopeLoggerResolver.INSTANCE.get();

  @Override
  protected ElementMatcher typeMatcher() {
    return named("okhttp3.OkHttpClient");
  }

  @Override
  protected ElementMatcher classLoaderMatcher() {
    return hasClassesNamed("okhttp3.OkHttpClient");
  }

  @Override
  protected Map, String> transformers() {
    final Map, String> transformers = new HashMap<>();
    transformers.put(
        isConstructor(), OkHttp3ClientScopeAgentRule.class.getName() + "$OkHttp3ClientBuildAdvice");
    return transformers;
  }

  public static class OkHttp3ClientBuildAdvice {

    @Advice.OnMethodEnter
    public static void enter(@Advice.AllArguments final Object[] args) {
      if (args.length == 0) {
        return;
      }

      try {
        ((OkHttpClient.Builder) args[0])
            .addInterceptor(OkHttp3ClientScopeAgentRuleInterceptor.SCOPE_OKHTTP_INTERCEPTOR);
      } catch (final Throwable throwable) {
        LOGGER.error("Could not add interceptor to OkHttpClient.Builder. Exception: " + throwable);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy