Skip to content

Commit

Permalink
应用部署在发行版tomcat的支持
Browse files Browse the repository at this point in the history
Signed-off-by: Yaxx19 <[email protected]>
  • Loading branch information
Yaxx19 committed Jun 6, 2024
1 parent a972311 commit 6ff250d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 52 deletions.
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
@@ -1,2 +1,44 @@
package io.sermant.core.plugin.agent.enhance;public class AbstractClassLoaderInterceptor {
package io.sermant.core.plugin.agent.enhance;

Check failure on line 1 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 请检查版权声明中其实年份与@SInCE中年份是否一致 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java:1:0: error: 请检查版权声明中其实年份与@SInCE中年份是否一致 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck)

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) {

Check failure on line 26 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java:26:5: error: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
for (String prefix : essentialPackage) {
if (name.startsWith(prefix)) {
return true;
}
}
return false;
}

protected boolean isSermantResource(String path) {

Check failure on line 35 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java:35:5: error: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
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
Expand Up @@ -3,14 +3,15 @@
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 增强
* 发行版Tomcat ClassLoader 增强
*
* @author Yaxx19
* @since 2024-06-04
*/
public class TomcatClassLoaderDeclarer extends AbstractPluginDeclarer {
public class WebappClassLoaderDeclarer extends AbstractPluginDeclarer {

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

Expand All @@ -21,6 +22,10 @@ public ClassMatcher getClassMatcher() {

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[0];
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
@@ -1,2 +1,50 @@
package io.sermant.core.plugin.agent.enhance;public class WebappClassLoaderGetResourceAsStreamInterceptor {
package io.sermant.core.plugin.agent.enhance;

Check failure on line 1 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/WebappClassLoaderGetResourceAsStreamInterceptor.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 请检查版权声明中其实年份与@SInCE中年份是否一致 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/WebappClassLoaderGetResourceAsStreamInterceptor.java:1:0: error: 请检查版权声明中其实年份与@SInCE中年份是否一致 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck)

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 增强
*
* @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;
}
}

0 comments on commit 6ff250d

Please sign in to comment.