Skip to content

Commit

Permalink
[pinpoint-apm#117] Implementing plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
lioolli committed Jan 20, 2015
1 parent c097b52 commit b77a2c0
Show file tree
Hide file tree
Showing 53 changed files with 1,678 additions and 636 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;

import java.lang.instrument.Instrumentation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -28,6 +26,10 @@
import java.security.PrivilegedAction;
import java.util.concurrent.Callable;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin;
import com.navercorp.pinpoint.common.plugin.Plugins;

/**
* @author emeroad
*/
Expand Down Expand Up @@ -69,16 +71,16 @@ public void setBootClass(String bootClass) {
this.bootClass = bootClass;
}

public void boot(final String agentPath, final String agentArgs, final Instrumentation instrumentation, final ProfilerConfig profilerConfig) {
public void boot(final String agentArgs, final Instrumentation instrumentation, final ProfilerConfig profilerConfig, final Plugins<ProfilerPlugin> plugins) {

final Class<?> bootStrapClazz = getBootStrapClass();

final Object agent = executeTemplate.execute(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
Constructor<?> constructor = bootStrapClazz.getConstructor(String.class, String.class, Instrumentation.class, ProfilerConfig.class);
return constructor.newInstance(agentPath, agentArgs, instrumentation, profilerConfig);
Constructor<?> constructor = bootStrapClazz.getConstructor(String.class, Instrumentation.class, ProfilerConfig.class, Plugins.class);
return constructor.newInstance(agentArgs, instrumentation, profilerConfig, plugins);
} catch (InstantiationException e) {
throw new BootStrapException("boot create failed. Error:" + e.getMessage(), e);
} catch (IllegalAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ public String getAgentLibPath() {
public String getAgentLogFilePath() {
return this.agentDirPath + File.separator + "log";
}

public String getAgentPluginPath() {
return this.agentDirPath + File.separator + "plugin";
}

public List<URL> resolveLib() {
String agentLibPath = getAgentLibPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,29 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.ProductInfo;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.bootstrap.util.IdValidateUtils;
import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.util.BytesUtils;

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.net.URL;
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;
import java.util.logging.Logger;

import com.navercorp.pinpoint.ProductInfo;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin;
import com.navercorp.pinpoint.bootstrap.util.IdValidateUtils;
import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.ServiceTypeInitializer;
import com.navercorp.pinpoint.common.plugin.PluginLoader;
import com.navercorp.pinpoint.common.plugin.Plugins;
import com.navercorp.pinpoint.common.plugin.ServiceTypeProvider;
import com.navercorp.pinpoint.common.util.BytesUtils;

/**
* @author emeroad
* @author netspider
Expand All @@ -49,6 +57,9 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
if (agentArgs != null) {
logger.info(ProductInfo.CAMEL_NAME + " agentArgs:" + agentArgs);
}

Map<String, String> argMap = parseAgentArgs(agentArgs);

final boolean duplicated = checkDuplicateLoadState();
if (duplicated) {
logPinpointAgentLoadFail();
Expand Down Expand Up @@ -86,6 +97,9 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
logPinpointAgentLoadFail();
return;
}

loadServiceTypeProviders(classPathResolver.getAgentPluginPath());
Plugins<ProfilerPlugin> plugins = PluginLoader.load(ProfilerPlugin.class, classPathResolver.getAgentPluginPath());

String configPath = getConfigPath(classPathResolver);
if (configPath == null) {
Expand All @@ -103,9 +117,10 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
// this is the library list that must be loaded
List<URL> libUrlList = resolveLib(classPathResolver);
AgentClassLoader agentClassLoader = new AgentClassLoader(libUrlList.toArray(new URL[libUrlList.size()]));
agentClassLoader.setBootClass(BOOT_CLASS);
logger.info("pinpoint agent starting...");
agentClassLoader.boot(classPathResolver.getAgentDirPath(), agentArgs, instrumentation, profilerConfig);
String bootClass = argMap.containsKey("bootClass") ? argMap.get("bootClass") : BOOT_CLASS;
agentClassLoader.setBootClass(bootClass);
logger.info("pinpoint agent [" + bootClass + "] starting...");
agentClassLoader.boot(agentArgs, instrumentation, profilerConfig, plugins);
logger.info("pinpoint agent started normally.");
} catch (Exception e) {
// unexpected exception that did not be checked above
Expand All @@ -114,6 +129,31 @@ public static void premain(String agentArgs, Instrumentation instrumentation) {
}

}

private static Map<String, String> parseAgentArgs(String str) {
Map<String, String> map = new HashMap<String, String>();

Scanner scanner = new Scanner(str);
scanner.useDelimiter("\\s*,\\s*");

while (scanner.hasNext()) {
String token = scanner.next();
int assign = token.indexOf('=');

if (assign == -1) {
map.put(token, "");
} else {
map.put(token.substring(0, assign), token.substring(assign + 1));
}
}

return map;
}

private static void loadServiceTypeProviders(String pluginPath) {
List<ServiceTypeProvider> providers = PluginLoader.load(ServiceTypeProvider.class, pluginPath).getPlugins();
ServiceTypeInitializer.load(providers);
}

private static JarFile getBootStrapJarFile(String bootStrapCoreJar) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ public interface InstrumentClass {
InstrumentClass getNestedClass(String className);

void addGetter(String getterName, String fieldName, String fieldType) throws InstrumentException;

void addGetter(Class<?> interfaceType, String fieldName) throws InstrumentException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2014 NAVER Corp.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.bootstrap.plugin;

import com.navercorp.pinpoint.bootstrap.Agent;
import com.navercorp.pinpoint.common.ServiceType;

/**
* @author Jongho Moon <jongho.moon@navercorp.com>
*
*/

public interface ApplicationServerProfilerPlugin extends ProfilerPlugin {
/**
* If true, current process is an instance of the application server profiled by this plugin.
*
* FIXME better name?
*/
public boolean isInstance();

public ServiceType getServerType();

public String[] getClassPath();

/**
* If true, this plugin must handle agent lifecycle.
* In other words, it have to invoke {@link Agent#start()} and {@link Agent#stop()}
*/
public boolean isPinpointAgentLifeCycleController();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,18 @@

public class BasicClassEditor implements DedicatedClassEditor {
private final String targetClassName;
private final List<MetadataInjector> metadataInjectors;
private final List<InterceptorInjector> interceptorInjectors;
private final List<Injector> injectors;


public BasicClassEditor(String targetClassName, List<MetadataInjector> metadataInjectors, List<InterceptorInjector> interceptorInjectors) {
public BasicClassEditor(String targetClassName, List<Injector> injectors) {
this.targetClassName = targetClassName;
this.metadataInjectors = metadataInjectors;
this.interceptorInjectors = interceptorInjectors;
this.injectors = injectors;
}

@Override
public byte[] edit(ClassLoader classLoader, InstrumentClass target) {
try {
for (MetadataInjector injector : metadataInjectors) {
injector.inject(classLoader, target);
}

for (InterceptorInjector injector : interceptorInjectors) {
for (Injector injector : injectors) {
injector.inject(classLoader, target);
}

Expand All @@ -54,6 +48,4 @@ public byte[] edit(ClassLoader classLoader, InstrumentClass target) {
public String getTargetClassName() {
return targetClassName;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.instrument.ByteCodeInstrumentor;
import com.navercorp.pinpoint.bootstrap.instrument.MethodFilter;
import com.navercorp.pinpoint.bootstrap.interceptor.tracevalue.TraceValue;
import com.navercorp.pinpoint.bootstrap.plugin.MetadataInitializationStrategy.ByConstructor;

public class ClassEditorBuilder {
private final ByteCodeInstrumentor instrumentor;
private final TraceContext traceContext;

private final List<InterceptorBuilder> interceptorBuilders = new ArrayList<InterceptorBuilder>();
private final List<MetadataBuilder> metadataBuilders = new ArrayList<MetadataBuilder>();
private final List<InjectorBuilder> injectorBuilders = new ArrayList<InjectorBuilder>();

private String targetClassName;
private Condition condition;
Expand All @@ -49,30 +49,30 @@ public void when(Condition condition) {

public InterceptorBuilder newInterceptorBuilder() {
InterceptorBuilder interceptorBuilder = new InterceptorBuilder();
interceptorBuilders.add(interceptorBuilder);
injectorBuilders.add(interceptorBuilder);
return interceptorBuilder;
}

public MetadataBuilder newMetadataBuilder() {
MetadataBuilder metadataBuilder = new MetadataBuilder();
metadataBuilders.add(metadataBuilder);
injectorBuilders.add(metadataBuilder);
return metadataBuilder;
}

public FieldSnooperBuilder newFieldAccessorBuilder() {
FieldSnooperBuilder fieldAccessorBuilder = new FieldSnooperBuilder();
injectorBuilders.add(fieldAccessorBuilder);
return fieldAccessorBuilder;
}

public DedicatedClassEditor build() {
List<MetadataInjector> metadataInjectors = new ArrayList<MetadataInjector>(metadataBuilders.size());

for (MetadataBuilder builder : metadataBuilders) {
metadataInjectors.add(builder.build());
}
List<Injector> injectors = new ArrayList<Injector>(injectorBuilders.size());

List<InterceptorInjector> interceptorInjectors = new ArrayList<InterceptorInjector>(interceptorBuilders.size());

for (InterceptorBuilder builder : interceptorBuilders) {
interceptorInjectors.add(builder.build());
for (InjectorBuilder builder : injectorBuilders) {
injectors.add(builder.build());
}

DedicatedClassEditor editor = new BasicClassEditor(targetClassName, metadataInjectors, interceptorInjectors);
DedicatedClassEditor editor = new BasicClassEditor(targetClassName, injectors);

if (condition != null) {
editor = new ConditionalClassEditor(condition, editor);
Expand All @@ -81,7 +81,11 @@ public DedicatedClassEditor build() {
return editor;
}

public class InterceptorBuilder {
private static abstract class InjectorBuilder {
abstract Injector build();
}

public class InterceptorBuilder extends InjectorBuilder {
private String methodName;
private String[] parameterNames;
private MethodFilter filter;
Expand Down Expand Up @@ -125,10 +129,11 @@ public void when(Condition condition) {
this.condition = condition;
}

private InterceptorInjector build() {
@Override
Injector build() {
InterceptorFactory interceptorFactory = new DefaultInterceptorFactory(instrumentor, traceContext, interceptorClassName, constructorArguments, scopeName);

InterceptorInjector injector;
Injector injector;

if (filter != null) {
injector = new FilteringInterceptorInjector(filter, interceptorFactory, singleton);
Expand All @@ -146,20 +151,40 @@ private InterceptorInjector build() {
}
}

public static class MetadataBuilder {
private String metadataAccessorTypeName;
public static class MetadataBuilder extends InjectorBuilder {
private Class<? extends TraceValue> metadataAccessorType;
private MetadataInitializationStrategy initializationStrategy;

public void inject(String metadataAccessorTypeName) {
this.metadataAccessorTypeName = metadataAccessorTypeName;
public void inject(Class<? extends TraceValue> metadataAccessorType) {
this.metadataAccessorType = metadataAccessorType;
}

public void initializeWithDefaultConstructorOf(String className) {
this.initializationStrategy = new ByConstructor(className);
}

private MetadataInjector build() {
return new DefaultMetadataInjector(metadataAccessorTypeName, initializationStrategy);
@Override
Injector build() {
return new MetadataInjector(metadataAccessorType, initializationStrategy);
}
}


public static class FieldSnooperBuilder extends InjectorBuilder {
private Class<? extends Snooper> snooperType;
private String fieldName;

public void inject(Class<? extends Snooper> snooperType) {
this.snooperType = snooperType;
}

public void toAccess(String fieldName) {
this.fieldName = fieldName;
}

@Override
Injector build() {
return new FieldSnooperInjector(snooperType, fieldName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;

public class ConditionalInterceptorInjector implements InterceptorInjector {
public class ConditionalInterceptorInjector implements Injector {
private final Condition condition;
private final InterceptorInjector delegate;
private final Injector delegate;

public ConditionalInterceptorInjector(Condition condition, InterceptorInjector delegate) {
public ConditionalInterceptorInjector(Condition condition, Injector delegate) {
this.condition = condition;
this.delegate = delegate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.navercorp.pinpoint.bootstrap.instrument.MethodInfo;
import com.navercorp.pinpoint.bootstrap.interceptor.Interceptor;

public class ConstructorInterceptorInjector implements InterceptorInjector {
public class ConstructorInterceptorInjector implements Injector {
private final String[] targetParameterTypes;
private final InterceptorFactory factory;

Expand Down
Loading

0 comments on commit b77a2c0

Please # to comment.