forked from opengoofy/hippo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bruceyan/issue monitor 20230624 (opengoofy#1382)
- Loading branch information
1 parent
d3e76ff
commit f773440
Showing
77 changed files
with
611 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...n/java/cn/hippo4j/agent/plugin/spring/common/define/SpringApplicationInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.plugin.spring.common.define; | ||
|
||
import cn.hippo4j.agent.core.plugin.interceptor.ConstructorInterceptPoint; | ||
import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; | ||
import cn.hippo4j.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; | ||
import cn.hippo4j.agent.core.plugin.match.ClassMatch; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
import static cn.hippo4j.agent.core.plugin.match.NameMatch.byName; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
public class SpringApplicationInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { | ||
|
||
private static final String ENHANCE_CLASS = "org.springframework.boot.SpringApplication"; | ||
|
||
private static final String RUN_INTERCEPTOR = "cn.hippo4j.agent.plugin.spring.common.interceptor.SpringApplicationRunInterceptor"; | ||
|
||
@Override | ||
protected ClassMatch enhanceClass() { | ||
return byName(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { | ||
return new ConstructorInterceptPoint[0]; | ||
} | ||
|
||
@Override | ||
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { | ||
return new InstanceMethodsInterceptPoint[]{ | ||
new InstanceMethodsInterceptPoint() { | ||
|
||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named("run"); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return RUN_INTERCEPTOR; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
protected String[] witnessClasses() { | ||
return new String[]{"org.springframework.boot.SpringApplication"}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
infra/common/src/main/java/cn/hippo4j/common/handler/DynamicThreadPoolAdapterChoose.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.common.handler; | ||
|
||
import cn.hippo4j.common.api.DynamicThreadPoolAdapter; | ||
import cn.hippo4j.common.extension.spi.ServiceLoaderRegistry; | ||
import cn.hippo4j.common.toolkit.CollectionUtil; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
/** | ||
* Dynamic thread pool adapter choose. | ||
*/ | ||
public class DynamicThreadPoolAdapterChoose { | ||
|
||
private static final List<DynamicThreadPoolAdapter> DYNAMIC_THREAD_POOL_ADAPTERS = new ArrayList<>(); | ||
|
||
static { | ||
DYNAMIC_THREAD_POOL_ADAPTERS.add(new TransmittableThreadLocalExecutorAdapter()); | ||
DYNAMIC_THREAD_POOL_ADAPTERS.add(new TransmittableThreadLocalExecutorServiceAdapter()); | ||
DYNAMIC_THREAD_POOL_ADAPTERS.add(new ZipkinExecutorAdapter()); | ||
loadCustomerAdapter(); | ||
} | ||
|
||
/** | ||
* Check if the object contains thread pool information. | ||
* | ||
* @param executor objects where there may be instances | ||
* of dynamic thread pools | ||
* @return matching results | ||
*/ | ||
public static boolean match(Object executor) { | ||
return DYNAMIC_THREAD_POOL_ADAPTERS.stream().anyMatch(each -> each.match(executor)); | ||
} | ||
|
||
/** | ||
* Get the dynamic thread pool reference in the object. | ||
* | ||
* @param executor objects where there may be instances | ||
* of dynamic thread pools | ||
* @return get the real dynamic thread pool instance | ||
*/ | ||
public static ThreadPoolExecutor unwrap(Object executor) { | ||
Optional<DynamicThreadPoolAdapter> dynamicThreadPoolAdapterOptional = DYNAMIC_THREAD_POOL_ADAPTERS.stream().filter(each -> each.match(executor)).findFirst(); | ||
return dynamicThreadPoolAdapterOptional.map(each -> each.unwrap(executor)).orElse(null); | ||
} | ||
|
||
/** | ||
* If the {@link DynamicThreadPoolAdapter#match(Object)} conditions are met, | ||
* the thread pool is replaced with a dynamic thread pool. | ||
* | ||
* @param executor objects where there may be instances | ||
* of dynamic thread pools | ||
* @param dynamicThreadPoolExecutor dynamic thread-pool executor | ||
*/ | ||
public static void replace(Object executor, Executor dynamicThreadPoolExecutor) { | ||
Optional<DynamicThreadPoolAdapter> dynamicThreadPoolAdapterOptional = DYNAMIC_THREAD_POOL_ADAPTERS.stream().filter(each -> each.match(executor)).findFirst(); | ||
dynamicThreadPoolAdapterOptional.ifPresent(dynamicThreadPoolAdapter -> dynamicThreadPoolAdapter.replace(executor, dynamicThreadPoolExecutor)); | ||
} | ||
|
||
/** | ||
* Load SPI customer adapter. | ||
*/ | ||
private static void loadCustomerAdapter() { | ||
ServiceLoaderRegistry.register(DynamicThreadPoolAdapter.class); | ||
Collection<DynamicThreadPoolAdapter> instances = ServiceLoaderRegistry.getSingletonServiceInstances(DynamicThreadPoolAdapter.class); | ||
if (CollectionUtil.isEmpty(instances)) { | ||
return; | ||
} | ||
for (DynamicThreadPoolAdapter instance : instances) { | ||
if (instance != null) { | ||
DYNAMIC_THREAD_POOL_ADAPTERS.add(instance); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...mmon/src/main/java/cn/hippo4j/common/handler/TransmittableThreadLocalExecutorAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.common.handler; | ||
|
||
import cn.hippo4j.common.api.DynamicThreadPoolAdapter; | ||
import cn.hippo4j.common.toolkit.ReflectUtil; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
/** | ||
* Transmittable thread local executor adapter. | ||
*/ | ||
public class TransmittableThreadLocalExecutorAdapter implements DynamicThreadPoolAdapter { | ||
|
||
private static final String MATCH_CLASS_NAME = "ExecutorTtlWrapper"; | ||
|
||
private static final String FIELD_NAME = "executor"; | ||
|
||
@Override | ||
public boolean match(Object executor) { | ||
return Objects.equals(MATCH_CLASS_NAME, executor.getClass().getSimpleName()); | ||
} | ||
|
||
@Override | ||
public ThreadPoolExecutor unwrap(Object executor) { | ||
return (ThreadPoolExecutor) ReflectUtil.getFieldValue(executor, FIELD_NAME); | ||
} | ||
|
||
@Override | ||
public void replace(Object executor, Executor dynamicThreadPoolExecutor) { | ||
ReflectUtil.setFieldValue(executor, FIELD_NAME, dynamicThreadPoolExecutor); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...c/main/java/cn/hippo4j/common/handler/TransmittableThreadLocalExecutorServiceAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.common.handler; | ||
|
||
import cn.hippo4j.common.api.DynamicThreadPoolAdapter; | ||
import cn.hippo4j.common.toolkit.ReflectUtil; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
/** | ||
* Transmittable thread local executor service adapter. | ||
*/ | ||
public class TransmittableThreadLocalExecutorServiceAdapter implements DynamicThreadPoolAdapter { | ||
|
||
private static final String MATCH_CLASS_NAME = "ExecutorServiceTtlWrapper"; | ||
|
||
private static final String FIELD_NAME = "executorService"; | ||
|
||
@Override | ||
public boolean match(Object executor) { | ||
return Objects.equals(MATCH_CLASS_NAME, executor.getClass().getSimpleName()); | ||
} | ||
|
||
@Override | ||
public ThreadPoolExecutor unwrap(Object executor) { | ||
return (ThreadPoolExecutor) ReflectUtil.getFieldValue(executor, FIELD_NAME); | ||
} | ||
|
||
@Override | ||
public void replace(Object executor, Executor dynamicThreadPoolExecutor) { | ||
ReflectUtil.setFieldValue(executor, FIELD_NAME, dynamicThreadPoolExecutor); | ||
} | ||
} |
Oops, something went wrong.