Skip to content

Commit

Permalink
fix:fix log framework conflict. (#204)
Browse files Browse the repository at this point in the history
* feat:update example with log4j2.

* fix:fix log framework conflict.

* fix:fix log framework conflict.

* fix:fix log framework conflict.

* fix:fix log framework conflict.
  • Loading branch information
SkyeBeFreeman authored Jul 31, 2024
1 parent 9b12412 commit 8637a80
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@

<dependencies>
<!-- 简单的 Spring Cloud Web 依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -61,10 +77,6 @@
<artifactId>spring-cloud-plugin-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-agent-core-extension</artifactId>
Expand Down Expand Up @@ -153,6 +165,16 @@
<!-- <artifactId>connector-polaris-grpc</artifactId>-->
<!-- </exclusion>-->

<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-nacos</artifactId>
</exclusion>

<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-consul</artifactId>
</exclusion>

<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-composite</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package cn.polarismesh.agent.plugin.spring.cloud;

import java.security.ProtectionDomain;

import cn.polarismesh.agent.core.extension.AgentPlugin;
import cn.polarismesh.agent.core.extension.PluginContext;
import cn.polarismesh.agent.core.extension.instrument.InstrumentClass;
Expand All @@ -33,91 +31,93 @@
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.RegisterBeanInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.SpringFactoriesLoaderInterceptor;

import java.security.ProtectionDomain;

/**
* Polaris Spring Cloud hoxton Plugin
*
* @author shuhanliu
*/
public class MainPlugin implements AgentPlugin {

public void init(PluginContext context) {
System.setProperty(Constant.AGENT_CONF_PATH, context.getAgentDirPath());
TransformOperations operations = context.getTransformOperations();
addPolarisTransformers(operations);
}

/**
* add polaris transformers
*/
private void addPolarisTransformers(TransformOperations operations) {

// 注入默认配置
operations.transform(Constant.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class);

// 注入bootstrap的bean定义
operations.transform(Constant.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class);

// 注入bean定义的调整设置
operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class);

// 注入JNI定义
operations.transform(Constant.SPRING_FACTORIES_LOADER, SpringFactoriesLoaderTransform.class);
}

public static class ConfigurationParserTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("parse", "java.util.Set");
if (constructMethod != null) {
constructMethod.addInterceptor(ConfigurationParserInterceptor.class);
}

return target.toBytecode();
}
}

public static class ConfigurationPostProcessorTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("processConfigBeanDefinitions", "org.springframework.beans.factory.support.BeanDefinitionRegistry");
if (constructMethod != null) {
constructMethod.addInterceptor(ConfigurationPostProcessorInterceptor.class);
}

return target.toBytecode();
}
}

public static class RegisterBeanDefinitionTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("registerBeanDefinition", "java.lang.String", "org.springframework.beans.factory.config.BeanDefinition");
if (constructMethod != null) {
constructMethod.addInterceptor(RegisterBeanInterceptor.class);
}

return target.toBytecode();
}
}

public static class SpringFactoriesLoaderTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("loadSpringFactories", "java.lang.ClassLoader");
if (constructMethod != null) {
constructMethod.addInterceptor(SpringFactoriesLoaderInterceptor.class);
}

return target.toBytecode();
}
}
public void init(PluginContext context) {
System.setProperty(Constant.AGENT_CONF_PATH, context.getAgentDirPath());
TransformOperations operations = context.getTransformOperations();
addPolarisTransformers(operations);
}

/**
* add polaris transformers
*/
private void addPolarisTransformers(TransformOperations operations) {

// 注入默认配置
operations.transform(Constant.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class);

// 注入bootstrap的bean定义
operations.transform(Constant.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class);

// 注入bean定义的调整设置
operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class);

// 注入JNI定义
operations.transform(Constant.SPRING_FACTORIES_LOADER, SpringFactoriesLoaderTransform.class);
}

public static class ConfigurationParserTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("parse", "java.util.Set");
if (constructMethod != null) {
constructMethod.addInterceptor(ConfigurationParserInterceptor.class);
}

return target.toBytecode();
}
}

public static class ConfigurationPostProcessorTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("processConfigBeanDefinitions", "org.springframework.beans.factory.support.BeanDefinitionRegistry");
if (constructMethod != null) {
constructMethod.addInterceptor(ConfigurationPostProcessorInterceptor.class);
}

return target.toBytecode();
}
}

public static class RegisterBeanDefinitionTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getDeclaredMethod("registerBeanDefinition", "java.lang.String", "org.springframework.beans.factory.config.BeanDefinition");
if (constructMethod != null) {
constructMethod.addInterceptor(RegisterBeanInterceptor.class);
}

return target.toBytecode();
}
}

public static class SpringFactoriesLoaderTransform implements TransformCallback {

@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer);
InstrumentMethod constructMethod = target.getConstructor("java.lang.ClassLoader", "java.util.Map");
if (constructMethod != null) {
constructMethod.addInterceptor(SpringFactoriesLoaderInterceptor.class);
}

return target.toBytecode();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@

package cn.polarismesh.agent.plugin.spring.cloud.interceptor;

import cn.polarismesh.agent.core.common.utils.ReflectionUtils;
import cn.polarismesh.agent.core.extension.interceptor.Interceptor;
import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector;
import cn.polarismesh.agent.plugin.spring.cloud.inject.*;
import com.tencent.polaris.api.utils.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

public class SpringFactoriesLoaderInterceptor implements Interceptor {

Expand Down Expand Up @@ -55,38 +54,36 @@ public SpringFactoriesLoaderInterceptor() {
@SuppressWarnings("unchecked")
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
if (args[0] == null) {
return;
Map<String, List<String>> oldFactories = (Map<String, List<String>>) ReflectionUtils.getObjectByFieldName(target, "factories");
if (CollectionUtils.isEmpty(oldFactories)) {
oldFactories = new HashMap<>();
}
Map<String, List<String>> newFactories = new HashMap<>();
for (Map.Entry<String, List<String>> entry : oldFactories.entrySet()) {
newFactories.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
ClassLoader classLoader = (ClassLoader) args[0];
parsedClasses.computeIfAbsent(classLoader, new Function<ClassLoader, Boolean>() {
@Override
public Boolean apply(ClassLoader classLoader) {
Map<String, List<String>> loadedClasses = (Map<String, List<String>>) result;

for (BeanInjector beanInjector : beanInjectors) {
LOGGER.info("[PolarisJavaAgent] start to inject JNI definition in module {}", beanInjector.getModule());
Map<String, List<String>> classNames = beanInjector.getClassNameForType();
if (classNames.isEmpty()) {
continue;
}
for (Map.Entry<String, List<String>> entry : classNames.entrySet()) {
List<String> existsValues = loadedClasses.get(entry.getKey());
List<String> toAddValues = entry.getValue();
if (null != existsValues) {
for (String toAddValue : toAddValues) {
if (existsValues.contains(toAddValue)) {
continue;
}
existsValues.add(toAddValue);
}
} else {
classNames.put(entry.getKey(), toAddValues);
for (BeanInjector beanInjector : beanInjectors) {
LOGGER.info("[PolarisJavaAgent] start to inject JNI definition in module {}", beanInjector.getModule());
Map<String, List<String>> classNames = beanInjector.getClassNameForType();
if (CollectionUtils.isEmpty(classNames)) {
continue;
}
for (Map.Entry<String, List<String>> entry : classNames.entrySet()) {
List<String> existsValues = newFactories.get(entry.getKey());
List<String> toAddValues = entry.getValue();
if (null != existsValues) {
for (String toAddValue : toAddValues) {
if (existsValues.contains(toAddValue)) {
continue;
}
existsValues.add(toAddValue);
}
} else {
classNames.put(entry.getKey(), toAddValues);
}
return true;
}
});
}
ReflectionUtils.setValueByFieldName(target, "factories", Collections.unmodifiableMap(newFactories));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -61,10 +78,6 @@
<artifactId>spring-cloud-plugin-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-agent-core-extension</artifactId>
Expand Down Expand Up @@ -153,6 +166,16 @@
<!-- <artifactId>connector-polaris-grpc</artifactId>-->
<!-- </exclusion>-->

<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-nacos</artifactId>
</exclusion>

<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-consul</artifactId>
</exclusion>

<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-composite</artifactId>
Expand Down
Loading

0 comments on commit 8637a80

Please sign in to comment.