Skip to content

Commit

Permalink
[#117] Added InterceptorGroup to clarify Scope concept
Browse files Browse the repository at this point in the history
  • Loading branch information
lioolli committed Apr 3, 2015
1 parent 5ea9642 commit e172954
Show file tree
Hide file tree
Showing 65 changed files with 1,038 additions and 862 deletions.
Expand Up @@ -24,6 +24,7 @@
import org.junit.runner.RunWith;

import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier.SpanType;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.test.plugin.JvmVersion;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
Expand All @@ -43,12 +44,14 @@ public void test() throws Exception {
connection.getHeaderFields();

PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printApis(System.out);
verifier.printSpans(System.out);

Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
Method getInputStream = targetClass.getMethod("getInputStream");

verifier.verifySpanCount(1);
verifier.verifySpanEvent("JDK_HTTPURLCONNECTOR", getInputStream, null, null, "www.naver.com", annotation("http.url", "http://www.naver.com"));
verifier.verifySpan(SpanType.SPAN_EVENT, "JDK_HTTPURLCONNECTOR", getInputStream, null, null, null, "www.naver.com", annotation("http.url", "http://www.naver.com"));
}

@Test
Expand All @@ -66,7 +69,7 @@ public void testConnectTwice() throws Exception {
Method connect = targetClass.getMethod("connect");

verifier.verifySpanCount(1);
verifier.verifySpanEvent("JDK_HTTPURLCONNECTOR", connect, null, null, "www.naver.com", annotation("http.url", "http://www.naver.com"));
verifier.verifySpan(SpanType.SPAN_EVENT, "JDK_HTTPURLCONNECTOR", connect, null, null, null, "www.naver.com", annotation("http.url", "http://www.naver.com"));
}

}
Expand Up @@ -16,12 +16,13 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;

import java.lang.instrument.Instrumentation;
import java.net.URL;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.service.AnnotationKeyRegistryService;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;

/**
* @author emeroad
*/
Expand All @@ -38,4 +39,6 @@ public interface AgentOption {
String getBootStrapJarPath();

ServiceTypeRegistryService getServiceTypeRegistryService();

AnnotationKeyRegistryService getAnnotationKeyRegistryService();
}
Expand Up @@ -16,12 +16,13 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;

import java.lang.instrument.Instrumentation;
import java.net.URL;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.service.AnnotationKeyRegistryService;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;

/**
* @author emeroad
*/
Expand All @@ -32,8 +33,9 @@ public class DefaultAgentOption implements AgentOption {
private final URL[] pluginJars;
private final String bootStrapJarPath;
private final ServiceTypeRegistryService serviceTypeRegistryService;
private final AnnotationKeyRegistryService annoationKeyRegistryService;

public DefaultAgentOption(final String agentArgs, final Instrumentation instrumentation, final ProfilerConfig profilerConfig, final URL[] pluginJars, final String bootStrapJarPath, final ServiceTypeRegistryService serviceTypeRegistryService) {
public DefaultAgentOption(final String agentArgs, final Instrumentation instrumentation, final ProfilerConfig profilerConfig, final URL[] pluginJars, final String bootStrapJarPath, final ServiceTypeRegistryService serviceTypeRegistryService, final AnnotationKeyRegistryService annotationKeyRegistryService) {
if (instrumentation == null) {
throw new NullPointerException("instrumentation must not be null");
}
Expand All @@ -46,6 +48,9 @@ public DefaultAgentOption(final String agentArgs, final Instrumentation instrume
// if (bootStrapJarPath == null) {
// throw new NullPointerException("bootStrapJarPath must not be null");
// }
if (annotationKeyRegistryService == null) {
throw new NullPointerException("annotationKeyRegistryService must not be null");
}
if (serviceTypeRegistryService == null) {
throw new NullPointerException("serviceTypeRegistryService must not be null");
}
Expand All @@ -55,7 +60,7 @@ public DefaultAgentOption(final String agentArgs, final Instrumentation instrume
this.pluginJars = pluginJars;
this.bootStrapJarPath = bootStrapJarPath;
this.serviceTypeRegistryService = serviceTypeRegistryService;

this.annoationKeyRegistryService = annotationKeyRegistryService;
}

@Override
Expand Down Expand Up @@ -87,4 +92,9 @@ public ProfilerConfig getProfilerConfig() {
public ServiceTypeRegistryService getServiceTypeRegistryService() {
return this.serviceTypeRegistryService;
}

@Override
public AnnotationKeyRegistryService getAnnotationKeyRegistryService() {
return this.annoationKeyRegistryService;
}
}
Expand Up @@ -19,7 +19,10 @@
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarFile;
import java.util.logging.Level;
Expand All @@ -30,11 +33,16 @@
import com.navercorp.pinpoint.bootstrap.util.IdValidateUtils;
import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.common.service.AnnotationKeyRegistryService;
import com.navercorp.pinpoint.common.service.DefaultAnnotationKeyRegistryService;
import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService;
import com.navercorp.pinpoint.common.service.DefaultTypeLoaderService;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.service.TypeLoaderService;
import com.navercorp.pinpoint.common.util.*;
import com.navercorp.pinpoint.common.util.BytesUtils;
import com.navercorp.pinpoint.common.util.PinpointThreadFactory;
import com.navercorp.pinpoint.common.util.SimpleProperty;
import com.navercorp.pinpoint.common.util.SystemProperty;

/**
* @author emeroad
Expand Down Expand Up @@ -98,8 +106,10 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
}

URL[] pluginJars = classPathResolver.resolvePlugins();
ServiceTypeRegistryService serviceTypeRegistryService = loadServiceTypeProviders(pluginJars);

TypeLoaderService typeLoaderService = new DefaultTypeLoaderService(pluginJars);
ServiceTypeRegistryService serviceTypeRegistryService = new DefaultServiceTypeRegistryService(typeLoaderService);
AnnotationKeyRegistryService annotationKeyRegistryService = new DefaultAnnotationKeyRegistryService(typeLoaderService);

String configPath = getConfigPath(classPathResolver);
if (configPath == null) {
logPinpointAgentLoadFail();
Expand All @@ -122,7 +132,7 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
agentClassLoader.setBootClass(bootClass);
logger.info("pinpoint agent [" + bootClass + "] starting...");

AgentOption option = createAgentOption(agentArgs, instrumentation, profilerConfig, pluginJars, bootStrapCoreJar, serviceTypeRegistryService);
AgentOption option = createAgentOption(agentArgs, instrumentation, profilerConfig, pluginJars, bootStrapCoreJar, serviceTypeRegistryService, annotationKeyRegistryService);
Agent pinpointAgent = agentClassLoader.boot(option);
pinpointAgent.start();
registerShutdownHook(pinpointAgent);
Expand All @@ -134,9 +144,9 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
}
}

private static AgentOption createAgentOption(String agentArgs, Instrumentation instrumentation, ProfilerConfig profilerConfig, URL[] pluginJars, String bootStrapJarPath, ServiceTypeRegistryService serviceTypeRegistryService) {
private static AgentOption createAgentOption(String agentArgs, Instrumentation instrumentation, ProfilerConfig profilerConfig, URL[] pluginJars, String bootStrapJarPath, ServiceTypeRegistryService serviceTypeRegistryService, AnnotationKeyRegistryService annotaionKeyRegistryService) {

return new DefaultAgentOption(agentArgs, instrumentation, profilerConfig, pluginJars, bootStrapJarPath, serviceTypeRegistryService);
return new DefaultAgentOption(agentArgs, instrumentation, profilerConfig, pluginJars, bootStrapJarPath, serviceTypeRegistryService, annotaionKeyRegistryService);
}

private static void registerShutdownHook(final Agent pinpointAgent) {
Expand Down Expand Up @@ -175,12 +185,6 @@ private static Map<String, String> parseAgentArgs(String str) {
return map;
}

private static ServiceTypeRegistryService loadServiceTypeProviders(URL[] pluginJars) {
TypeLoaderService typeLoaderService = new DefaultTypeLoaderService(pluginJars);
ServiceTypeRegistryService serviceTypeRegistryService = new DefaultServiceTypeRegistryService(typeLoaderService);
return serviceTypeRegistryService;
}

private static JarFile getBootStrapJarFile(String bootStrapCoreJar) {
try {
return new JarFile(bootStrapCoreJar);
Expand Down
Expand Up @@ -19,6 +19,6 @@
/**
* @author emeroad
*/
public interface AttachmentFactory<T> {
T createAttachment();
public interface AttachmentFactory {
Object createAttachment();
}
Expand Up @@ -22,29 +22,19 @@
public class DefaultScopeDefinition implements ScopeDefinition {

private final String name;
private final Type type;

public DefaultScopeDefinition(String name, Type type) {
public DefaultScopeDefinition(String name) {
if (name == null) {
throw new NullPointerException("name must not be null");
}
if (type == null) {
throw new NullPointerException("scopeType must not be null");
}
this.name = name;
this.type = type;
}

@Override
public String getName() {
return name;
}

@Override
public Type getType() {
return type;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -53,15 +43,13 @@ public boolean equals(Object o) {
DefaultScopeDefinition that = (DefaultScopeDefinition) o;

if (!name.equals(that.name)) return false;
if (type != that.type) return false;

return true;
}

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + type.hashCode();
return result;
}
}
Expand Up @@ -108,6 +108,8 @@ public interface InstrumentClass {
public boolean isInterceptable();

boolean hasDeclaredMethod(String methodName, String[] args);

boolean hasConstructor(String[] parameterTypeArray);

boolean hasMethod(String methodName, String[] parameterTypeArray, String returnType);

Expand Down
Expand Up @@ -16,19 +16,20 @@

package com.navercorp.pinpoint.bootstrap.instrument;

import com.navercorp.pinpoint.bootstrap.plugin.interceptor.ExecutionPoint;

/**
* @author emeroad
*/
public interface Scope {

int ZERO = 0;

int push();

int depth();

int pop() ;

public interface Scope {
String getName();

boolean tryBefore(ExecutionPoint point);
boolean tryAfter(ExecutionPoint point);
boolean isIn();

Object setAttachment(Object attachment);
Object getAttachment();
Object getOrCreateAttachment(AttachmentFactory factory);
Object removeAttachment();
}
Expand Up @@ -20,11 +20,5 @@
* @author emeroad
*/
public interface ScopeDefinition {
enum Type {
SIMPLE, ATTACHMENT
}

String getName();

Type getType();
}
Expand Up @@ -18,6 +18,7 @@
import com.navercorp.pinpoint.bootstrap.MetadataAccessor;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.instrument.ByteCodeInstrumentor;
import com.navercorp.pinpoint.bootstrap.plugin.interceptor.InterceptorGroup;

/**
* Provides attributes and objects to interceptors.
Expand Down Expand Up @@ -76,4 +77,6 @@ public interface ProfilerPluginContext {
* @return {@link ByteCodeInstrumentor}
*/
public ByteCodeInstrumentor getByteCodeInstrumentor();

public InterceptorGroup getInterceptorGroup(String name);
}
Expand Up @@ -19,6 +19,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.navercorp.pinpoint.bootstrap.interceptor.Interceptor;
import com.navercorp.pinpoint.bootstrap.plugin.interceptor.ExecutionPoint;

/**
* Indicates that the annotated {@link Interceptor} should in a scope.
*
Expand All @@ -43,4 +46,6 @@
* scope name
*/
public String value();

public ExecutionPoint executionPoint() default ExecutionPoint.BOUNDARY;
}
Expand Up @@ -19,8 +19,6 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.navercorp.pinpoint.bootstrap.plugin.editor.ClassEditorBuilder;

/**
* Indicates that the annotated interceptor have to be singleton.
*
Expand Down
Expand Up @@ -14,12 +14,14 @@
*/
package com.navercorp.pinpoint.bootstrap.plugin.editor;

import com.navercorp.pinpoint.bootstrap.plugin.interceptor.InterceptorBuilder;

/**
* @author Jongho Moon
*
*/
public interface BaseMethodEditorBuilder {
void injectInterceptor(String interceptorClassName, Object... constructorArguments);
InterceptorBuilder injectInterceptor(String interceptorClassName, Object... constructorArguments);
void exceptionHandler(MethodEditorExceptionHandler handler);
void property(MethodEditorProperty... properties);
}
Expand Up @@ -39,6 +39,10 @@ public static ClassCondition hasDeclaredMethod(String name, String... paramTypes
return new HasDeclaredMethod(name, paramTypes);
}

public static ClassCondition hasConstructor(String... paramTypes) {
return new HasConstructor(paramTypes);
}


private static class HasField implements ClassCondition {
private final String name;
Expand Down Expand Up @@ -90,4 +94,17 @@ public boolean check(ProfilerPluginContext context, ClassLoader classLoader, Ins
return target.hasDeclaredMethod(name, paramTypes);
}
}

private static class HasConstructor implements ClassCondition {
private final String[] paramTypes;

public HasConstructor(String[] paramTypes) {
this.paramTypes = paramTypes;
}

@Override
public boolean check(ProfilerPluginContext context, ClassLoader classLoader, InstrumentClass target) {
return target.hasConstructor(paramTypes);
}
}
}

0 comments on commit e172954

Please sign in to comment.