com.undefinedlabs.scope.rules.OkHttp3ClientScopeAgentRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scope-rule-okhttp3 Show documentation
Show all versions of scope-rule-okhttp3 Show documentation
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.
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 super TypeDescription> typeMatcher() {
return named("okhttp3.OkHttpClient");
}
@Override
protected ElementMatcher classLoaderMatcher() {
return hasClassesNamed("okhttp3.OkHttpClient");
}
@Override
protected Map extends ElementMatcher super MethodDescription>, 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);
}
}
}
}