Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import io.sermant.core.plugin.agent.collector.PluginCollector;
import io.sermant.core.plugin.agent.declarer.PluginDescription;
import io.sermant.core.plugin.agent.enhance.ClassLoaderDeclarer;
import io.sermant.core.plugin.agent.enhance.WebappClassLoaderDeclarer;
import io.sermant.core.service.ServiceConfig;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.agent.builder.AgentBuilder.RedefinitionStrategy;
import net.bytebuddy.agent.builder.AgentBuilder.RedefinitionStrategy.BatchAllocator.ForTotal;
Expand Down Expand Up @@ -119,6 +119,7 @@ private static void enhanceForFramework() {
private static void enhanceForInjectService() {
if (ConfigManager.getConfig(ServiceConfig.class).isInjectEnable()) {
builder.addEnhance(new ClassLoaderDeclarer());
builder.addEnhance(new WebappClassLoaderDeclarer());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.sermant.core.plugin.agent.enhance;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import io.sermant.core.config.ConfigManager;
import io.sermant.core.plugin.agent.interceptor.Interceptor;
import io.sermant.core.service.inject.config.InjectConfig;

import java.util.Set;

/**
* ClassLoader 增强抽象类
*
* @author Yaxx19
* @since 2024-06-04
*/
public abstract class AbstractClassLoaderInterceptor implements Interceptor {

private final Set<String> essentialPackage;

/**
* constructor
*/
public AbstractClassLoaderInterceptor() {
essentialPackage = ConfigManager.getConfig(InjectConfig.class).getEssentialPackage();
}

protected boolean isSermantClass(String name) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Javadoc comment for isSermantClass and isSermantResource

for (String prefix : essentialPackage) {
if (name.startsWith(prefix)) {
return true;
}
}
return false;
}

protected boolean isSermantResource(String path) {
String name = path.replace('/', '.');
for (String prefix : essentialPackage) {
if (name.startsWith(prefix)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@

import io.sermant.core.classloader.ClassLoaderManager;
import io.sermant.core.common.LoggerFactory;
import io.sermant.core.config.ConfigManager;
import io.sermant.core.plugin.agent.entity.ExecuteContext;
import io.sermant.core.plugin.agent.interceptor.Interceptor;
import io.sermant.core.service.inject.config.InjectConfig;

import java.net.URL;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -35,18 +31,9 @@
* @author luanwenfei
* @since 2023-05-08
*/
public class ClassLoaderFindResourceInterceptor implements Interceptor {
public class ClassLoaderFindResourceInterceptor extends AbstractClassLoaderInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger();

private final Set<String> essentialPackage;

/**
* constructor
*/
public ClassLoaderFindResourceInterceptor() {
essentialPackage = ConfigManager.getConfig(InjectConfig.class).getEssentialPackage();
}

@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
return context;
Expand Down Expand Up @@ -77,13 +64,4 @@ public ExecuteContext onThrow(ExecuteContext context) throws Exception {
return context;
}

private boolean isSermantResource(String path) {
String name = path.replace('/', '.');
for (String prefix : essentialPackage) {
if (name.startsWith(prefix)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@

import io.sermant.core.classloader.ClassLoaderManager;
import io.sermant.core.common.LoggerFactory;
import io.sermant.core.config.ConfigManager;
import io.sermant.core.plugin.agent.entity.ExecuteContext;
import io.sermant.core.plugin.agent.interceptor.Interceptor;
import io.sermant.core.service.inject.config.InjectConfig;

import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -33,18 +29,9 @@
* @author luanwenfei
* @since 2023-04-28
*/
public class ClassLoaderLoadClassInterceptor implements Interceptor {
public class ClassLoaderLoadClassInterceptor extends AbstractClassLoaderInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger();

private final Set<String> essentialPackage;

/**
* constructor
*/
public ClassLoaderLoadClassInterceptor() {
essentialPackage = ConfigManager.getConfig(InjectConfig.class).getEssentialPackage();
}

@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
return context;
Expand All @@ -70,13 +57,4 @@ public ExecuteContext onThrow(ExecuteContext context) throws Exception {
}
return context;
}

private boolean isSermantClass(String name) {
for (String prefix : essentialPackage) {
if (name.startsWith(prefix)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.sermant.core.plugin.agent.enhance;

import io.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import io.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import io.sermant.core.plugin.agent.matcher.ClassMatcher;
import io.sermant.core.plugin.agent.matcher.MethodMatcher;

/**
* 发行版Tomcat ClassLoader 增强
*
* @author Yaxx19
* @since 2024-06-04
*/
public class WebappClassLoaderDeclarer extends AbstractPluginDeclarer {

private static final String TOMCAT_CLASS_LOADER = "org.apache.catalina.loader.WebappClassLoaderBase";

@Override
public ClassMatcher getClassMatcher() {
return ClassMatcher.nameEquals(TOMCAT_CLASS_LOADER);
}

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[] {
InterceptDeclarer.build(MethodMatcher.nameEquals("loadClass"),
new ClassLoaderLoadClassInterceptor()),
InterceptDeclarer.build(MethodMatcher.nameEquals("getResourceAsStream"),
new WebappClassLoaderGetResourceAsStreamInterceptor())};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.sermant.core.plugin.agent.enhance;

import io.sermant.core.classloader.ClassLoaderManager;
import io.sermant.core.common.LoggerFactory;
import io.sermant.core.plugin.agent.entity.ExecuteContext;

import java.net.URL;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 发行版Tomcat ClassLoader getResourceAsStream 增强
Copy link
Copy Markdown
Collaborator

@lilai23 lilai23 Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i hope all comments could be in english, As well as PR description and commit message

*
* @author Yaxx19
* @since 2024-06-04
*/
public class WebappClassLoaderGetResourceAsStreamInterceptor extends AbstractClassLoaderInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger();

@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
return context;
}

@Override
public ExecuteContext after(ExecuteContext context) throws Exception {
if (context.getResult() != null) {
return context;
}

String path = (String) context.getArguments()[0];
if (isSermantResource(path)) {
Optional<URL> url = ClassLoaderManager.getPluginClassFinder().findSermantResource(path);
if (!url.isPresent()) {
LOGGER.log(Level.WARNING, "Can not get resource stream [{0}] by sermant.And then find by {1}. ",
new Object[]{path, context.getObject()});
} else {
context.changeResult(url.get().openStream());
LOGGER.log(Level.INFO, "Get resource stream: {0} successfully by sermant.", path);
}
}
return context;
}

@Override
public ExecuteContext onThrow(ExecuteContext context) throws Exception {
return context;
}
}