Skip to content

Commit

Permalink
Complete the agent of springboot1.x version in config mode. (opengoof…
Browse files Browse the repository at this point in the history
  • Loading branch information
yanrongzhen authored May 4, 2023
1 parent e188855 commit f132105
Show file tree
Hide file tree
Showing 55 changed files with 1,322 additions and 59 deletions.
6 changes: 6 additions & 0 deletions hippo4j-agent/hippo4j-agent-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
<artifactId>jmh-generator-annprocess</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hippo4j</groupId>
<artifactId>hippo4j-common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import cn.hippo4j.agent.core.logging.api.ILog;
import cn.hippo4j.agent.core.logging.api.LogManager;
import cn.hippo4j.agent.core.util.ConfigInitializer;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashSet;
Expand All @@ -38,17 +39,21 @@ public class SpringBootConfigInitializer {

private static long PROPERTIES_LOAD_TIME;

private static Properties SPRING_PROPERTIES = null;
public static Properties SPRING_PROPERTIES = null;

private SpringBootConfigInitializer() {

}

public static boolean isSpringPropertiesEmpty() {
return SPRING_PROPERTIES == null || SPRING_PROPERTIES.isEmpty();
}

public static synchronized void initializeConfig(SpringBootConfig springBootConfig) {
if (SPRING_PROPERTIES != null) {
try {
LOG.info("initialize Spring Config Class {}.", springBootConfig.root());
ConfigInitializer.initialize(SPRING_PROPERTIES, springBootConfig.root());
ConfigInitializer.initialize(SPRING_PROPERTIES, springBootConfig.root(), true);
} catch (Throwable e) {
LOG.error(e, "Failed to set the agent settings {} to Config={} ", SPRING_PROPERTIES, springBootConfig.root());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package cn.hippo4j.agent.core.conf;

import cn.hippo4j.agent.core.boot.SpringBootConfig;
import cn.hippo4j.agent.core.logging.core.LogLevel;
import cn.hippo4j.agent.core.logging.core.LogOutput;
import cn.hippo4j.agent.core.logging.core.ResolverType;
Expand Down Expand Up @@ -358,6 +359,31 @@ public static class Plugin {
* Mount the folders of the plugins. The folder path is relative to agent.jar.
*/
public static List<String> MOUNT = Arrays.asList("plugins", "activations");

public static class ThreadPool {

public static List<String> EXCLUDE_PACKAGE_PREFIX = Arrays.asList(
"java", "sun", "okhttp3", "retrofit2", "reactor",
"org.apache", "io.netty", "org.springframework", "com.ctrip", "com.google",
"io.undertow", "org.xnio", "org.jboss", "com.zaxxer", "org.redisson", "com.alibaba",
"com.netflix", "com.mysql", "rx.internal", "io.shardingjdbc", "org.drools", "org.elasticsearch",
"ch.qos.logback", "net.sf.ehcache");
}

public static class Apollo {
public static class App {

public static String ID;
}
public static String META;

public static class BootStrap {

public static boolean ENABLED = false;

public static List<String> NAMESPACES;
}
}
}

public static class Correlation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ public class Constants {
public static String EVENT_LAYER_NAME = "GENERAL";

public static int NULL_VALUE = 0;

public static String SPRING_BOOT_CONFIG_PREFIX = "spring.dynamic.thread-pool";

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public static void initializeCoreConfig(String agentOptions) {
Config.Agent.CLUSTER);
}
}
if (StringUtil.isEmpty(Config.Collector.BACKEND_SERVICE)) {
throw new ExceptionInInitializerError("`collector.backend_service` is missing.");
}
// if (StringUtil.isEmpty(Config.Collector.BACKEND_SERVICE)) {
// throw new ExceptionInInitializerError("`collector.backend_service` is missing.");
// }
if (Config.Plugin.PEER_MAX_LENGTH <= 3) {
LOGGER.warn(
"PEER_MAX_LENGTH configuration:{} error, the default value of 200 will be used.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 cn.hippo4j.agent.core.registry;

import cn.hippo4j.agent.core.logging.api.ILog;
import cn.hippo4j.agent.core.logging.api.LogManager;
import cn.hippo4j.common.config.ExecutorProperties;
import lombok.Data;

import java.util.concurrent.ThreadPoolExecutor;

@Data
public class AgentThreadPoolExecutorHolder {

private static final ILog LOGGER = LogManager.getLogger(AgentThreadPoolExecutorHolder.class);

public static final AgentThreadPoolExecutorHolder EMPTY = new AgentThreadPoolExecutorHolder();

private String executorName;

private ThreadPoolExecutor executor;

private ExecutorProperties properties;

public AgentThreadPoolExecutorHolder() {
}

public AgentThreadPoolExecutorHolder(String executorName, ThreadPoolExecutor executor, ExecutorProperties properties) {
this.executorName = executorName;
this.executor = executor;
this.properties = properties;
}

public boolean isEmpty() {
return this == EMPTY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 cn.hippo4j.agent.core.registry;

import cn.hippo4j.agent.core.logging.api.ILog;
import cn.hippo4j.agent.core.logging.api.LogManager;
import cn.hippo4j.common.config.ExecutorProperties;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

public class AgentThreadPoolInstanceRegistry {

private static final ILog LOGGER = LogManager.getLogger(AgentThreadPoolInstanceRegistry.class);

private final Map<String, AgentThreadPoolExecutorHolder> holderMap = new ConcurrentHashMap<>();

private volatile static AgentThreadPoolInstanceRegistry INSTANCE;

private AgentThreadPoolInstanceRegistry() {
}

public static AgentThreadPoolInstanceRegistry getInstance() {
if (INSTANCE == null) {
synchronized (AgentThreadPoolInstanceRegistry.class) {
if (INSTANCE == null) {
INSTANCE = new AgentThreadPoolInstanceRegistry();
}
}
}
return INSTANCE;
}

public Map<String, AgentThreadPoolExecutorHolder> getHolderMap() {
return holderMap;
}

public void putHolder(String executorName, ThreadPoolExecutor executor, ExecutorProperties properties) {
AgentThreadPoolExecutorHolder holder = new AgentThreadPoolExecutorHolder(executorName, executor, properties);
holderMap.put(executorName, holder);
}

public AgentThreadPoolExecutorHolder getHolder(String executorName) {
return Optional.ofNullable(holderMap.get(executorName)).orElse(AgentThreadPoolExecutorHolder.EMPTY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 cn.hippo4j.agent.core.util;

public interface AgentThreadPoolConstants {

String TOMCAT_NAME_PREFIX = "namePrefix";
String DUBBO_NAME_PREFIX = "mPrefix";
String DUBBO_THREAD_NAME = "DubboServerHandler";
String THREAD_POOL_NAME_DUBBO = "dubbo";

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ public static String toString(final Map<String, String[]> map) {
public static boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}

/**
* Is empty.
*
* @param map
* @return
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}

/**
* Is not empty.
*
* @param map
* @return
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@
public class ConfigInitializer {

public static void initialize(Properties properties, Class<?> rootConfigType) throws IllegalAccessException {
initNextLevel(properties, rootConfigType, new ConfigDesc());
initNextLevel(properties, rootConfigType, new ConfigDesc(), false);
}

public static void initialize(Properties properties, Class<?> rootConfigType, boolean isSpringProperties) throws IllegalAccessException {
initNextLevel(properties, rootConfigType, new ConfigDesc(), isSpringProperties);
}

private static void initNextLevel(Properties properties, Class<?> recentConfigType,
ConfigDesc parentDesc) throws IllegalArgumentException, IllegalAccessException {
ConfigDesc parentDesc, boolean isSpringProperties) throws IllegalArgumentException, IllegalAccessException {
for (Field field : recentConfigType.getFields()) {
if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
String configKey = (parentDesc + "." + field.getName()).toLowerCase();
String configKey = (parentDesc + "." + (isSpringProperties ? field.getName().replace("_", "-") : field.getName())).toLowerCase();
Class<?> type = field.getType();

if (type.equals(Map.class)) {
Expand Down Expand Up @@ -78,8 +82,10 @@ private static void initNextLevel(Properties properties, Class<?> recentConfigTy
}
}
for (Class<?> innerConfiguration : recentConfigType.getClasses()) {
parentDesc.append(innerConfiguration.getSimpleName());
initNextLevel(properties, innerConfiguration, parentDesc);
String simpleName = innerConfiguration.getSimpleName();
String description = isSpringProperties ? simpleName.replace("_", "-") : simpleName;
parentDesc.append(description);
initNextLevel(properties, innerConfiguration, parentDesc, isSpringProperties);
parentDesc.removeLastDesc();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 cn.hippo4j.agent.core.util;

import cn.hippo4j.agent.core.logging.api.ILog;
import cn.hippo4j.agent.core.logging.api.LogManager;

import java.lang.reflect.Field;

public class ExecutorNameUtil {

private static final ILog LOGGER = LogManager.getLogger(ExecutorNameUtil.class);

public static boolean isTomcatExecutor(Object threadFactory) {
try {
if ("org.apache.tomcat.util.threads.TaskThreadFactory".equals(threadFactory.getClass().getName())) {
Field namePrefixField = threadFactory.getClass().getDeclaredField(AgentThreadPoolConstants.TOMCAT_NAME_PREFIX);
namePrefixField.setAccessible(true);
String namePrefix = (String) namePrefixField.get(threadFactory);
if (RegexUtil.isTomcatNameMatch(namePrefix)) {
return true;
}
}
} catch (Throwable t) {
LOGGER.error("Fail to put tomcat executor", t);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 cn.hippo4j.agent.core.util;

import cn.hippo4j.agent.core.logging.api.ILog;
import cn.hippo4j.agent.core.logging.api.LogManager;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class ReflectUtil {

private static final ILog LOGGER = LogManager.getLogger(ReflectUtil.class);

public static List<Field> getStaticFieldsFromType(Class<?> clazz, Class<?> declaredType) {
Field[] fields = clazz.getFields();
List<Field> result = new ArrayList<>();
for (Field field : fields) {
if (field.getType().isAssignableFrom(declaredType) &&
Modifier.isStatic(field.getModifiers())) {
result.add(field);
}
}
return result;
}
}
Loading

0 comments on commit f132105

Please sign in to comment.